Skip to content

Commit 3df4d12

Browse files
committed
Add KMS alias and removal policy for S3 bucket KMS key
1 parent 69f1ef4 commit 3df4d12

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

packages/cdk/constructs/S3Bucket.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
BlockPublicAccess,
77
ObjectOwnership
88
} from "aws-cdk-lib/aws-s3"
9-
import {Key} from "aws-cdk-lib/aws-kms"
9+
import {Key, Alias} from "aws-cdk-lib/aws-kms"
1010

1111
export interface S3BucketProps {
1212
readonly bucketName: string
@@ -16,13 +16,21 @@ export interface S3BucketProps {
1616
export class S3Bucket extends Construct {
1717
public readonly bucket: Bucket
1818
public readonly kmsKey: Key
19+
public readonly kmsAlias: Alias
1920

2021
constructor(scope: Construct, id: string, props: S3BucketProps) {
2122
super(scope, id)
2223

2324
this.kmsKey = new Key(this, "BucketKey", {
2425
enableKeyRotation: true,
25-
description: `KMS key for ${props.bucketName} S3 bucket encryption`
26+
description: `KMS key for ${props.bucketName} S3 bucket encryption`,
27+
removalPolicy: RemovalPolicy.DESTROY
28+
})
29+
30+
this.kmsAlias = new Alias(this, "BucketKeyAlias", {
31+
aliasName: `alias/${props.bucketName}-s3-key`,
32+
targetKey: this.kmsKey,
33+
removalPolicy: RemovalPolicy.DESTROY
2634
})
2735

2836
this.bucket = new Bucket(this, props.bucketName, {

0 commit comments

Comments
 (0)