Skip to content

Commit d46b535

Browse files
authored
feat(dynamodb): add tagging support to TableV2 (#27649)
This change adds the ability to tag GlobalTable replicas created using the TableV2 construct. The top level "tags" will apply tags to the primary table while tags for each replica can be specified in the respective replica definition. Closes #27146. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 1433ff2 commit d46b535

File tree

6 files changed

+138
-7
lines changed

6 files changed

+138
-7
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.table-v2-global.js.snapshot/aws-cdk-global-table.template.json

+21-3
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,13 @@
143143
}
144144
},
145145
"Region": "us-east-2",
146-
"TableClass": "STANDARD_INFREQUENT_ACCESS"
146+
"TableClass": "STANDARD_INFREQUENT_ACCESS",
147+
"Tags": [
148+
{
149+
"Key": "USE2ReplicaTagKey",
150+
"Value": "USE2ReplicaTagValue"
151+
}
152+
]
147153
},
148154
{
149155
"ContributorInsightsSpecification": {
@@ -173,7 +179,13 @@
173179
"ReadCapacityUnits": 10
174180
},
175181
"Region": "us-west-2",
176-
"TableClass": "STANDARD"
182+
"TableClass": "STANDARD",
183+
"Tags": [
184+
{
185+
"Key": "USW2ReplicaTagKey",
186+
"Value": "USW2ReplicaTagValue"
187+
}
188+
]
177189
},
178190
{
179191
"ContributorInsightsSpecification": {
@@ -214,7 +226,13 @@
214226
"ReadCapacityUnits": 10
215227
},
216228
"Region": "us-east-1",
217-
"TableClass": "STANDARD_INFREQUENT_ACCESS"
229+
"TableClass": "STANDARD_INFREQUENT_ACCESS",
230+
"Tags": [
231+
{
232+
"Key": "primaryTableTagKey",
233+
"Value": "primaryTableTagValue"
234+
}
235+
]
218236
}
219237
],
220238
"SSESpecification": {

packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.table-v2-global.js.snapshot/tree.json

+21-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.table-v2-global.ts

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class TestStack extends Stack {
5252
contributorInsights: false,
5353
},
5454
},
55+
tags: [{ key: 'USE2ReplicaTagKey', value: 'USE2ReplicaTagValue' }],
5556
},
5657
{
5758
region: 'us-west-2',
@@ -62,8 +63,10 @@ class TestStack extends Stack {
6263
readCapacity: Capacity.fixed(15),
6364
},
6465
},
66+
tags: [{ key: 'USW2ReplicaTagKey', value: 'USW2ReplicaTagValue' }],
6567
},
6668
],
69+
tags: [{ key: 'primaryTableTagKey', value: 'primaryTableTagValue' }],
6770
});
6871
}
6972
}

packages/aws-cdk-lib/aws-dynamodb/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,31 @@ const table = new dynamodb.TableV2(this, 'Table', {
588588
Further reading:
589589
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.TableClasses.html
590590

591+
## Tags
592+
593+
You can add tags to a `TableV2` in several ways. By adding the tags to the construct itself it will apply the tags to the
594+
primary table.
595+
```ts
596+
const table = new dynamodb.TableV2(this, 'Table', {
597+
partitionKey: { name: 'pk', type: dynamodb.AttributeType.STRING },
598+
tags: [{key: 'primaryTableTagKey', value: 'primaryTableTagValue'}],
599+
});
600+
```
601+
602+
You can also add tags to replica tables by specifying them within the replica table properties.
603+
604+
```ts
605+
const table = new dynamodb.TableV2(this, 'Table', {
606+
partitionKey: { name: 'pk', type: dynamodb.AttributeType.STRING },
607+
replicas: [
608+
{
609+
region: 'us-west-1',
610+
tags: [{key: 'replicaTableTagKey', value: 'replicaTableTagValue'}]
611+
}
612+
]
613+
});
614+
```
615+
591616
## Referencing Existing Global Tables
592617

593618
To reference an existing DynamoDB table in your CDK application, use the `TableV2.fromTableName`, `TableV2.fromTableArn`, or `TableV2.fromTableAttributes`

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
import { TableBaseV2, ITableV2 } from './table-v2-base';
1212
import { IStream } from '../../aws-kinesis';
1313
import { IKey, Key } from '../../aws-kms';
14-
import { ArnFormat, Lazy, PhysicalName, RemovalPolicy, Stack, Token } from '../../core';
14+
import { ArnFormat, CfnTag, Lazy, PhysicalName, RemovalPolicy, Stack, Token } from '../../core';
1515

1616
const HASH_KEY_TYPE = 'HASH';
1717
const RANGE_KEY_TYPE = 'RANGE';
@@ -114,6 +114,13 @@ export interface TableOptionsV2 {
114114
* @default - no Kinesis Data Stream
115115
*/
116116
readonly kinesisStream?: IStream;
117+
118+
/**
119+
* Tags to be applied to the table or replica table
120+
*
121+
* @default - no tags
122+
*/
123+
readonly tags?: CfnTag[];
117124
}
118125

119126
/**
@@ -612,6 +619,7 @@ export class TableV2 extends TableBaseV2 {
612619
readProvisionedThroughputSettings: props.readCapacity
613620
? props.readCapacity._renderReadCapacity()
614621
: this.readProvisioning,
622+
tags: props.tags,
615623
};
616624
}
617625

@@ -716,6 +724,7 @@ export class TableV2 extends TableBaseV2 {
716724
replicaTables.push(this.configureReplicaTable({
717725
region: this.stack.region,
718726
kinesisStream: this.tableOptions.kinesisStream,
727+
tags: this.tableOptions.tags,
719728
}));
720729

721730
return replicaTables;

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

+58
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,31 @@ describe('table', () => {
547547
});
548548
});
549549

550+
test('with tags', () => {
551+
// GIVEN
552+
const stack = new Stack(undefined, 'Stack', { env: { region: 'us-east-1' } });
553+
554+
// WHEN
555+
new TableV2(stack, 'Table', {
556+
partitionKey: { name: 'pk', type: AttributeType.STRING },
557+
replicas: [{ region: 'us-west-1' }],
558+
tags: [{ key: 'tagKey', value: 'tagValue' }],
559+
});
560+
561+
// THEN
562+
Template.fromStack(stack).hasResourceProperties('AWS::DynamoDB::GlobalTable', {
563+
Replicas: [
564+
{
565+
Region: 'us-west-1',
566+
},
567+
{
568+
Region: 'us-east-1',
569+
Tags: [{ Key: 'tagKey', Value: 'tagValue' }],
570+
},
571+
],
572+
});
573+
});
574+
550575
test('with all properties configured', () => {
551576
// GIVEN
552577
const stack = new Stack(undefined, 'Stack', { env: { region: 'us-west-2' } });
@@ -605,6 +630,7 @@ describe('table', () => {
605630
contributorInsights: false,
606631
},
607632
},
633+
tags: [{ key: 'USE1Key', value: 'USE1Value' }],
608634
},
609635
{
610636
region: 'us-east-2',
@@ -615,8 +641,10 @@ describe('table', () => {
615641
readCapacity: Capacity.fixed(15),
616642
},
617643
},
644+
tags: [{ key: 'USE2Key', value: 'USE2Value' }],
618645
},
619646
],
647+
tags: [{ key: 'USW2Key', value: 'USW2Value' }],
620648
});
621649

622650
// THEN
@@ -748,6 +776,7 @@ describe('table', () => {
748776
KMSMasterKeyId: 'arn:aws:kms:us-east-1:123456789012:key/g24efbna-az9b-42ro-m3bp-cq249l94fca6',
749777
},
750778
TableClass: 'STANDARD_INFREQUENT_ACCESS',
779+
Tags: [{ Key: 'USE1Key', Value: 'USE1Value' }],
751780
},
752781
{
753782
ContributorInsightsSpecification: {
@@ -783,6 +812,7 @@ describe('table', () => {
783812
KMSMasterKeyId: 'arn:aws:kms:us-east-2:123456789012:key/g24efbna-az9b-42ro-m3bp-cq249l94fca6',
784813
},
785814
TableClass: 'STANDARD',
815+
Tags: [{ Key: 'USE2Key', Value: 'USE2Value' }],
786816
},
787817
{
788818
ContributorInsightsSpecification: {
@@ -833,6 +863,7 @@ describe('table', () => {
833863
},
834864
},
835865
TableClass: 'STANDARD_INFREQUENT_ACCESS',
866+
Tags: [{ Key: 'USW2Key', Value: 'USW2Value' }],
836867
},
837868
],
838869
SSESpecification: {
@@ -1079,6 +1110,33 @@ describe('replica tables', () => {
10791110
});
10801111
});
10811112

1113+
test('with tags', () => {
1114+
// GIVEN
1115+
const stack = new Stack(undefined, 'Stack', { env: { region: 'us-east-1' } });
1116+
1117+
// WHEN
1118+
new TableV2(stack, 'Table', {
1119+
partitionKey: { name: 'pk', type: AttributeType.STRING },
1120+
replicas: [{
1121+
region: 'us-west-1',
1122+
tags: [{ key: 'tagKey', value: 'tagValue' }],
1123+
}],
1124+
});
1125+
1126+
// THEN
1127+
Template.fromStack(stack).hasResourceProperties('AWS::DynamoDB::GlobalTable', {
1128+
Replicas: [
1129+
{
1130+
Region: 'us-west-1',
1131+
Tags: [{ Key: 'tagKey', Value: 'tagValue' }],
1132+
},
1133+
{
1134+
Region: 'us-east-1',
1135+
},
1136+
],
1137+
});
1138+
});
1139+
10821140
test('with per-replica kinesis stream', () => {
10831141
// GIVEN
10841142
const stack = new Stack(undefined, 'Stack', { env: { region: 'us-west-2' } });

0 commit comments

Comments
 (0)