Skip to content

Commit bd4141d

Browse files
authored
fix(rds): allow cluster from snapshot to enable encrypted storage (#19175)
Closes #17241 Tested by: ```typescript // 1. Create original cluster with unencrypted storage new DatabaseCluster(stack, 'Database', { engine: DatabaseClusterEngine.AURORA, instanceProps: { vpc }, }); // 2. Take snapshot of cluster (mySnapshot) // 3. Create cluster from snapshot with encrypted storage new DatabaseClusterFromSnapshot(stack, 'Database', { engine: DatabaseClusterEngine.AURORA, instanceProps: { vpc }, snapshotIdentifier: 'mySnapshot', storageEncryptionKey: new kms.Key(stack, 'Key'), }); // 4. Verify new cluster has encrypted storage ``` ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent cef8fec commit bd4141d

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

packages/@aws-cdk/aws-rds/lib/cluster.ts

+19-18
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,21 @@ interface DatabaseClusterBaseProps {
250250
* @default false
251251
*/
252252
readonly iamAuthentication?: boolean;
253+
254+
/**
255+
* Whether to enable storage encryption.
256+
*
257+
* @default - true if storageEncryptionKey is provided, false otherwise
258+
*/
259+
readonly storageEncrypted?: boolean
260+
261+
/**
262+
* The KMS key for storage encryption.
263+
* If specified, {@link storageEncrypted} will be set to `true`.
264+
*
265+
* @default - if storageEncrypted is true then the default master key, no key otherwise
266+
*/
267+
readonly storageEncryptionKey?: kms.IKey;
253268
}
254269

255270
/**
@@ -402,6 +417,9 @@ abstract class DatabaseClusterNew extends DatabaseClusterBase {
402417
preferredMaintenanceWindow: props.preferredMaintenanceWindow,
403418
databaseName: props.defaultDatabaseName,
404419
enableCloudwatchLogsExports: props.cloudwatchLogsExports,
420+
// Encryption
421+
kmsKeyId: props.storageEncryptionKey?.keyArn,
422+
storageEncrypted: props.storageEncryptionKey ? true : props.storageEncrypted,
405423
};
406424
}
407425
}
@@ -479,21 +497,6 @@ export interface DatabaseClusterProps extends DatabaseClusterBaseProps {
479497
*/
480498
readonly credentials?: Credentials;
481499

482-
/**
483-
* Whether to enable storage encryption.
484-
*
485-
* @default - true if storageEncryptionKey is provided, false otherwise
486-
*/
487-
readonly storageEncrypted?: boolean
488-
489-
/**
490-
* The KMS key for storage encryption.
491-
* If specified, {@link storageEncrypted} will be set to `true`.
492-
*
493-
* @default - if storageEncrypted is true then the default master key, no key otherwise
494-
*/
495-
readonly storageEncryptionKey?: kms.IKey;
496-
497500
/**
498501
* Whether to copy tags to the snapshot when a snapshot is created.
499502
*
@@ -550,9 +553,7 @@ export class DatabaseCluster extends DatabaseClusterNew {
550553
// Admin
551554
masterUsername: credentials.username,
552555
masterUserPassword: credentials.password?.toString(),
553-
// Encryption
554-
kmsKeyId: props.storageEncryptionKey?.keyArn,
555-
storageEncrypted: props.storageEncryptionKey ? true : props.storageEncrypted,
556+
// Tags
556557
copyTagsToSnapshot: props.copyTagsToSnapshot ?? true,
557558
});
558559

packages/@aws-cdk/aws-rds/test/cluster.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -1974,6 +1974,27 @@ describe('cluster', () => {
19741974
});
19751975
});
19761976

1977+
test('create a cluster from a snapshot with encrypted storage', () => {
1978+
const stack = testStack();
1979+
const vpc = new ec2.Vpc(stack, 'VPC');
1980+
1981+
// WHEN
1982+
new DatabaseClusterFromSnapshot(stack, 'Database', {
1983+
engine: DatabaseClusterEngine.aurora({ version: AuroraEngineVersion.VER_1_22_2 }),
1984+
instanceProps: {
1985+
vpc,
1986+
},
1987+
snapshotIdentifier: 'mySnapshot',
1988+
storageEncryptionKey: kms.Key.fromKeyArn(stack, 'Key', 'arn:aws:kms:us-east-1:456:key/my-key'),
1989+
});
1990+
1991+
// THEN
1992+
Template.fromStack(stack).hasResourceProperties('AWS::RDS::DBCluster', {
1993+
KmsKeyId: 'arn:aws:kms:us-east-1:456:key/my-key',
1994+
StorageEncrypted: true,
1995+
});
1996+
});
1997+
19771998
test('reuse an existing subnet group', () => {
19781999
// GIVEN
19792000
const stack = testStack();

0 commit comments

Comments
 (0)