(aws-cdk/aws-s3): bucket.grantRead does nothing when bucket is imported and grantee identity policy is absent
See original GitHub issueDescribe the bug
I created an S3 bucket in Stack1. In Stack 2, referencing this bucket using bucket.fromAttributes
methods. In Stack2 I also created a CloudFrontDistribution and OriginAccessIdentity. Then grant access to OriginAccessIdentity. After deploying these stacks, no updates made to Bucket policy. Also no errors thrown during deployment.
Expected Behavior
OriginAccessIdentity should be granted Read access on S3 Bucket.
Current Behavior
It doesn’t grant read access.
Reproduction Steps
Code for Stack1
export default class StaticAssetsStack extends Stack {
public readonly staticAssetsBucket : IBucket;
constructor(parent: App, name: string, props: StaticAssetsStackProps) {
super(parent, name, props);
this.staticAssetsBucket = new Bucket(this, `AssetBucket`, {
versioned: false,
bucketName: `website-assets`.toLowerCase(),
removalPolicy: RemovalPolicy.RETAIN,
enforceSSL: true,
encryption: BucketEncryption.S3_MANAGED,
});
}
}
Code for Stack2
export default class WebsiteStack extends Stack {
constructor(parent: App, name: string, props: WebsiteStackProps) {
super(parent, name, props);
const staticAssetsBucket = Bucket.fromBucketAttributes(this, `staticAssetsBucket`, {
bucketName: props.staticAssetsBucketName,
region: props.staticAssetsBucketRegion
});
const originAccessIdentity =
new OriginAccessIdentity(this, `${props.name}-OAI`);
staticAssetsBucket.grantRead(originAccessIdentity);
const s3Origin = new S3Origin(staticAssetsBucket, {
originAccessIdentity,
});
Possible Solution
No response
Additional Information/Context
No response
CDK CLI Version
CDKv2
Framework Version
No response
Node.js Version
14.x
OS
Linux
Language
Typescript
Language Version
No response
Other information
No response
Issue Analytics
- State:
- Created a year ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
[aws-s3] Bucket.grantRead do nothing with an imported ec2 ...
A new policy is added to the EC2 instance's role (i.e. principal) or to the bucket (i.e. resource). What actually happened? A new...
Read more >Permissions - AWS Cloud Development Kit (AWS CDK) v2
Every construct that represents a resource that can be accessed, such as an Amazon S3 bucket or Amazon DynamoDB table, has methods that...
Read more >class Bucket (construct) · AWS CDK
Implements IConstruct , IDependable , IResource , IBucket. An S3 bucket with associated policy objects. This bucket does not yet have all features...
Read more >aws-cdk/aws-s3 module - AWS Documentation
If you try to add a policy statement to an existing bucket, this method will not do anything: const bucket = s3.Bucket.fromBucketName(this, 'existingBucket' ......
Read more >Troubleshooting common AWS CDK issues
When deploying my AWS CDK stack, I receive a NoSuchBucket error. Your AWS environment has not been bootstrapped, and so does not have...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Well, that’s not quite true, at least now all the way. One example can be found in my app, where I augmented legacy instance role with necessary permissions. So in other words; I had imported construct and was able to modify it. Point taken; that was indirect modification via
ManagedPolicies
where role is attached.Just wanted to clarify that a bit; that some resources have a way to support that while others don’t.
My problem: https://github.com/aws/aws-cdk/issues/22047 is maybe not related but at the very least have the same ancestry.
If you had transfer a bucket between stacks via reference i.e. creating implicit
Fn::Export
andFn::Import
things would work. On the other hand, if you are importing bucket that way I am pretty sure thataddToResourcePolicy
calls will be simply be ignored. Maybe not ignored, but imported bucket has nopolicy
that is required to be set. To generalize that, if you import a resource it really doesn’t matter (if I am right) if that’s a bucket, a key or a dynamo db table as long as you’re interested in seeing those resources’ policies updated during anygrant*
operation or by hand.All in all, I feel like the proper response here is to fail the synth instead of silently ignoring the fact. However, you can verify things on your own i.e:
There’s this
iam.AddToResourcePolicyResult
thingy that containsstatementAdded
property.