Skip to content

Commit a990b4c

Browse files
committed
fix(aws-dynamodb): use keyId instead of keyArn for TableV2 replica encryption
Fixes CloudFormation drift detection for DynamoDB TableV2 with customer-managed KMS encryption. Previously, CDK generated templates using KMS key ARN, but DynamoDB internally stores only the key ID, causing false positive drift detection. Changes: - encryption.ts: Use tableKey.keyId instead of tableKey.keyArn in _renderReplicaSseSpecification - Update test expectation to match corrected CloudFormation output (Ref vs Fn::GetAtt) Fixes aws#35136
1 parent 08d7e46 commit a990b4c

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

packages/aws-cdk-lib/aws-dynamodb/lib/encryption.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export abstract class TableEncryptionV2 {
2222
public _renderReplicaSseSpecification(_scope: Construct, _region: string) {
2323
return undefined;
2424
}
25-
}) (TableEncryption.DEFAULT);
25+
})(TableEncryption.DEFAULT);
2626
}
2727

2828
/**
@@ -40,7 +40,7 @@ export abstract class TableEncryptionV2 {
4040
public _renderReplicaSseSpecification(_scope: Construct, _region: string) {
4141
return undefined;
4242
}
43-
}) (TableEncryption.AWS_MANAGED);
43+
})(TableEncryption.AWS_MANAGED);
4444
}
4545

4646
/**
@@ -70,7 +70,7 @@ export abstract class TableEncryptionV2 {
7070

7171
if (replicaRegion === stackRegion) {
7272
return {
73-
kmsMasterKeyId: tableKey.keyArn,
73+
kmsMasterKeyId: tableKey.keyId,
7474
} satisfies CfnGlobalTable.ReplicaSSESpecificationProperty;
7575
}
7676

@@ -83,13 +83,13 @@ export abstract class TableEncryptionV2 {
8383
kmsMasterKeyId: replicaKeyArns[replicaRegion],
8484
} satisfies CfnGlobalTable.ReplicaSSESpecificationProperty;
8585
}
86-
}) (TableEncryption.CUSTOMER_MANAGED, tableKey, replicaKeyArns);
86+
})(TableEncryption.CUSTOMER_MANAGED, tableKey, replicaKeyArns);
8787
}
8888

89-
private constructor (
89+
private constructor(
9090
public readonly type: TableEncryption,
9191
public readonly tableKey?: IKey,
92-
public readonly replicaKeyArns?: { [region: string]: string }) {}
92+
public readonly replicaKeyArns?: { [region: string]: string }) { }
9393

9494
/**
9595
* @internal

packages/aws-cdk-lib/aws-dynamodb/test/encryption.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,20 @@ describe('customer managed keys', () => {
9898
test('can render replica SSE specification in deployment region', () => {
9999
// WHEN / THEN
100100
expect(encryption._renderReplicaSseSpecification(stack, stack.region)).toEqual({
101-
kmsMasterKeyId: tableKey.keyArn,
101+
kmsMasterKeyId: tableKey.keyId,
102102
});
103103
});
104104

105+
test('replica SSE specification uses key ID format not ARN format', () => {
106+
// WHEN
107+
const result = encryption._renderReplicaSseSpecification(stack, stack.region);
108+
109+
// THEN
110+
expect(result.kmsMasterKeyId).toBe(tableKey.keyId);
111+
expect(result.kmsMasterKeyId).not.toBe(tableKey.keyArn);
112+
expect(result.kmsMasterKeyId).not.toContain('arn:aws:kms');
113+
});
114+
105115
test('can render replica SSE specification in replica region', () => {
106116
// WHEN / THEN
107117
expect(encryption._renderReplicaSseSpecification(stack, 'us-east-1')).toEqual({

packages/aws-cdk-lib/aws-dynamodb/test/table-v2.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -926,10 +926,7 @@ describe('table', () => {
926926
Region: 'us-west-2',
927927
SSESpecification: {
928928
KMSMasterKeyId: {
929-
'Fn::GetAtt': [
930-
'Key961B73FD',
931-
'Arn',
932-
],
929+
'Ref': 'Key961B73FD',
933930
},
934931
},
935932
TableClass: 'STANDARD_INFREQUENT_ACCESS',

0 commit comments

Comments
 (0)