amazon-web-servicesamazon-s3aws-cloudformationamazon-iamaws-cdk

How to add S3 BucketPolicy with AWS CDK?


I wanna translate this CloudFormation piece into CDK:

Type: AWS::S3::BucketPolicy
Properties:
  Bucket:
    Ref: S3BucketImageUploadBuffer
  PolicyDocument:
    Version: "2012-10-17"
    Statement:
      Action:
        - s3:PutObject
        - s3:PutObjectAcl
      Effect: Allow
      Resource:
        - ...

Looking at the documentation here, I don't see a way to provide the policy document itself.


Solution

  • This is an example from a working CDK-Stack:

       artifactBucket.addToResourcePolicy(
          new PolicyStatement({
            resources: [
              this.pipeline.artifactBucket.arnForObjects("*"), 
              this.pipeline.artifactBucket.bucketArn],
            ],
            actions: ["s3:List*", "s3:Get*"],
            principals: [new ArnPrincipal(this.deploymentRole.roleArn)]
          })
        );