Skip to content

(aws-s3-deployment): bucket deployment fails when toolkit stack uses Customer KMS #25100

@jaecktec

Description

@jaecktec

Describe the bug

When initialising the toolkit stack with --bootstrap-kms-key-id the bucket deployment fails.

Expected Behavior

Bucket deployment copies the ressources to the desired destination

Current Behavior

The deployment fails and the logs read:

download failed: s3://cdk-<qualifier>-assets-<account>-<region>/<asset-hash>.zip to <tmp-folder> An error occurred (AccessDenied) when calling the GetObject operation: The ciphertext refers to a customer master key that does not exist, does not exist in this region, or you are not allowed to access.

Reproduction Steps

Bootstrap with KMS:

npx cdk bootstrap aws://$AWS_ACCOUNT_ID/$AWS_REGION --bootstrap-kms-key-id $KMS_KEY_ID

create a stack with a bucket deployment

import * as cdk from 'aws-cdk-lib';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as s3deploy from 'aws-cdk-lib/aws-s3-deployment';

export class MyStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // Create an S3 bucket
    const bucket = new s3.Bucket(this, 'MyBucket', {
      bucketName: 'my-bucket-name'
    });

    // Deploy a text file to the bucket using BucketDeployment
    new s3deploy.BucketDeployment(this, 'DeployTextFile', {
      sources: [s3deploy.Source.asset('./path/to/text/file.txt')],
      destinationBucket: bucket,
      destinationKeyPrefix: 'path/to/deployed/text/file.txt'
    });
  }
}

Possible Solution

The handlerRole of the BucketDeployment construct needs to have something like this:

    Key.fromKeyArn(this, 'cdk-toolkit-encryption-key', Stack.of(this).formatArn({
      region: Aws.REGION,
      service: 'kms',
      account: Aws.ACCOUNT_ID,
      resource: 'key',
      resourceName: Fn.importValue(`CdkBootstrap-${this.node.tryGetContext('@aws-cdk/core:bootstrapQualifier')}-FileAssetKeyArn`),
    }))
      // @ts-ignore
      .grantEncryptDecrypt(bucketDeployment.handlerRole);

Additional Information/Context

Bootstrap Stack is updated to the most recent version

CDK CLI Version

2.73.0

Framework Version

No response

Node.js Version

16.20.0

OS

macos 13.2.1

Language

Typescript

Language Version

4.9.5

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions