question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(aws-cdk/aws-s3): bucket.grantRead does nothing when bucket is imported and grantee identity policy is absent

See original GitHub issue

Describe 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:open
  • Created a year ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
kornicameistercommented, Sep 16, 2022

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.

1reaction
kornicameistercommented, Sep 15, 2022

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 and Fn::Import things would work. On the other hand, if you are importing bucket that way I am pretty sure that addToResourcePolicy calls will be simply be ignored. Maybe not ignored, but imported bucket has no policy 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 any grant* 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:

if(!Bucket.fromBucketAttributes(..).grantRead().statementAdded){
   throw new Error('Failed to grant read')
}

There’s this iam.AddToResourcePolicyResult thingy that contains statementAdded property.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found