s3: can't enable bucket public access
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 83
Description
### Describe the bug
https://github.com/aws/aws-cdk/issues/25358 introduced the major changes for S3 in April 2023 but it's still unclear to customers how to setup a S3 bucket with public access enabled for some use cases like [static website hosting](https://docs.aws.amazon.com/AmazonS3/latest/userguide/WebsiteHosting.html).
### Expected Behavior
As https://github.com/aws/aws-cdk/issues/25358#issuecomment-1534455073 suggested, this should work:
```ts
const bucket = new s3.Bucket(this, 'Bucket', {
publicReadAccess: true,
blockPublicAccess: {
blockPublicPolicy: false,
blockPublicAcls: false,
ignorePublicAcls: false,
restrictPublicBuckets: false,
},
accessControl: BucketAccessControl.PUBLIC_READ,
objectOwnership: ObjectOwnership.OBJECT_WRITER,
})
```
### Current Behavior
It would fail with this error but sometimes it deploys successfully.
I guess this could be a bug from cloudformation as it does not always fail.
```
12:05:26 PM | CREATE_FAILED | AWS::S3::Bucket | Bucket83908E77
Bucket cannot have public ACLs set with BlockPublicAccess enabled (Service: Amazon S3; Status Code: 400; Error
Code: InvalidBucketAclWithBlockPublicAccessError; Request ID: 8R35HKMW941ZRN30; S3 Extended Request ID: ZK3daYi
1wkLTk++u+/3mvRPWXBbDNstauIDnp8kiL4XdQfdmzJ2jAktdUVBpRztwEumIJteAh+8=; Proxy: null)
```
Another alternative is to deploy with `accessControl: BucketAccessControl.PUBLIC_READ` commented off.
```ts
const bucket = new s3.Bucket(this, 'Bucket', {
publicReadAccess: true,
blockPublicAccess: {
blockPublicPolicy: false,
blockPublicAcls: false,
ignorePublicAcls: false,
restrictPublicBuckets: false,
},
// accessControl: BucketAccessControl.PUBLIC_READ,
objectOwnership: ObjectOwnership.OBJECT_WRITER,
});
```
And re-deploy with `accessControl` enabled. This will 100% work.
```ts
const bucket = new s3.Bucket(this, 'Bucket', {
publicReadAccess: true,
blockPublicAccess: {
blockPublicPolicy: false,
blockPublicAcls: false,
ignorePublicAcls: false,
restrictPublicBuckets: false,
},
accessControl: BucketAccessControl.PUBLIC_READ,
objectOwnership: ObjectOwnership.OBJECT_WRITER,
});
```
I guess the cloudformation handler probably can't handle this well when both `accessControl` and `objectOwnership` are enabled.
### Reproduction Steps
See current behavior.
### Possible Solution
See current behavior. This might be a CFN bug.
### Additional Information/Context
The synth output for the Bucket resource
```yaml
Resources:
Bucket83908E77:
Type: AWS::S3::Bucket
Properties:
AccessControl: PublicRead
OwnershipControls:
Rules:
- ObjectOwnership: ObjectWriter
PublicAccessBlockConfiguration:
BlockPublicAcls: false
BlockPublicPolicy: false
IgnorePublicAcls: false
RestrictPublicBuckets: false
UpdateReplacePolicy: Retain
DeletionPolicy: Retain
Metadata:
aws:cdk:path: test-stack/Bucket/Resource
BucketPolicyE9A3008A:
Type: AWS::S3::BucketPolicy
Properties:
Bucket:
Ref: Bucket83908E77
PolicyDocument:
Statement:
- Action: s3:GetObject
Effect: Allow
Principal:
AWS: "*"
Resource:
Fn::Join:
- ""
- - Fn::GetAtt:
- Bucket83908E77
- Arn
- /*
Version: "2012-10-17"
Metadata:
aws:cdk:path: test-stack/Bucket/Policy/Resource
CDKMetadata:
Type: AWS::CDK::Metadata
Properties:
Analytics: v2:deflate64:H4sIAAAAAAAA/zPSs7DQM1BMLC/WTU7J1s3JTNKrDi5JTM7WAQrFFxvrVTuVJmenlug4p+VBWRAqID8nM7kSIQzh14IE/EtLCkrBOoJSi/NLi5JTa3Xy8lNS9bKK9csMLfQMTYE2ZhVnZuoWleaVZOam6gVBaAB96glZjQAAAA==
Metadata:
aws:cdk:path: test-stack/CDKMetadata/Default
```
### CDK CLI Version
2.88.0
### Framework Version
_No response_
### Node.js Version
v18.15.0
### OS
mac os x
### Language
Typescript
### Language Version
_No response_
### Other information
_No response_
Contributor guide
Research direction
Start with the TypeScript s3.Bucket reproduction and inspect the synthesized AWS::S3::Bucket properties, especially AccessControl, OwnershipControls, and PublicAccessBlockConfiguration. Reproduce the deployment failure and determine whether the configuration can deploy consistently; document the result or correct the behavior if it is within CDK.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, typescript
- Domain
- cloud, infrastructure
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100