awslabs / awslabs/aws-solutions-constructs
CloudFrontToS3 additionalBehaviors default origin to s3BucketInterface
- Dominant language
- TypeScript
- Stars
- 1.4k
- Forks
- 268
- Avg merge
- 5h 18m
- Merged PRs (30d)
- 5
Description
Currently we can add a default behavior which implicitly is attached to the S3 bucket origin
```typescript
const dist = new CloudFrontToS3(this, 'my-distribution', {
cloudFrontDistributionProps: {
defaultBehavior: {
allowedMethods: AllowedMethods.ALLOW_GET_HEAD_OPTIONS,
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS
}
}
});
```
If we want to add an additional behavior for that same origin, we can't simply do:
```typescript
const dist = new CloudFrontToS3(this, 'my-distribution', {
cloudFrontDistributionProps: {
defaultBehavior: {
allowedMethods: AllowedMethods.ALLOW_GET_HEAD_OPTIONS,
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS
},
additionalBehaviors: {
'some-path/*': {
allowedMethods: AllowedMethods.ALLOW_GET_HEAD_OPTIONS,
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS
}
}
}
});
```
As it will fail with `TypeError: Cannot read properties of undefined (reading 'bind')`
Instead, we have to do the following:
```typescript
const dist = new CloudFrontToS3(this, 'my-distribution', {
cloudFrontDistributionProps: {
defaultBehavior: {
allowedMethods: AllowedMethods.ALLOW_GET_HEAD_OPTIONS,
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS
}
}
});
dist.cloudFrontWebDistribution.addBehavior(
'some-path/*',
S3BucketOrigin.withOriginAccessControl(dist.s3BucketInterface),
{
allowedMethods: AllowedMethods.ALLOW_GET_HEAD_OPTIONS,
viewerProtocolPolicy: ViewerProtocolPolicy.REDIRECT_TO_HTTPS
}
);
```
### Use Case
I think it's generally useful to be able to add any extra behaviors on the distribution in-place rather than assigning and having to create an extra origin with `S3BucketOrigin.withOriginAccessControl(dist.s3BucketInterface)`
### Proposed Solution
Make it optional in `BehaviorOptions` and default to s3BucketInterface, users may override with different origins if they need to.
### Other
* [ ] :wave: I may be able to implement this feature request
* [ ] :warning: This feature might incur a breaking change
---
This is a :rocket: Feature Request
Contributor guide
Research direction
Start at the CloudFrontToS3 construct and the cloudFrontDistributionProps handling for additionalBehaviors and BehaviorOptions. Reproduce the reported TypeError, then verify that an additional behavior defaults to the S3 bucket interface while still allowing an explicitly supplied origin; confirm the existing default behavior remains valid.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, typescript
- Domain
- cloud
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100