diff --git a/packages/@aws-cdk/aws-dynamodb/lib/table.ts b/packages/@aws-cdk/aws-dynamodb/lib/table.ts index 01624e7d29358..6210f24a240f9 100644 --- a/packages/@aws-cdk/aws-dynamodb/lib/table.ts +++ b/packages/@aws-cdk/aws-dynamodb/lib/table.ts @@ -39,6 +39,12 @@ export interface TableProps { */ streamSpecification?: StreamViewType; + /** + * The name of TTL attribute. + * @default undefined, TTL is disabled + */ + ttlAttributeName?: string; + /** * AutoScalingProps configuration to configure Read AutoScaling for the DynamoDB table. * This field is optional and this can be achieved via addReadAutoScaling. @@ -118,7 +124,8 @@ export class Table extends Construct { attributeDefinitions: this.attributeDefinitions, provisionedThroughput: { readCapacityUnits, writeCapacityUnits }, sseSpecification: props.sseEnabled ? { sseEnabled: props.sseEnabled } : undefined, - streamSpecification: props.streamSpecification ? { streamViewType: props.streamSpecification } : undefined + streamSpecification: props.streamSpecification ? { streamViewType: props.streamSpecification } : undefined, + timeToLiveSpecification: props.ttlAttributeName ? { attributeName: props.ttlAttributeName, enabled: true } : undefined }); if (props.tableName) { this.addMetadata('aws:cdk:hasPhysicalName', props.tableName); } diff --git a/packages/@aws-cdk/aws-dynamodb/test/integ.dynamodb.expected.json b/packages/@aws-cdk/aws-dynamodb/test/integ.dynamodb.expected.json index 5d4645a81aac3..2e3204cf87d35 100644 --- a/packages/@aws-cdk/aws-dynamodb/test/integ.dynamodb.expected.json +++ b/packages/@aws-cdk/aws-dynamodb/test/integ.dynamodb.expected.json @@ -32,6 +32,10 @@ }, "StreamSpecification": { "StreamViewType": "KEYS_ONLY" + }, + "TimeToLiveSpecification": { + "AttributeName": "timeToLive", + "Enabled": true } } } diff --git a/packages/@aws-cdk/aws-dynamodb/test/integ.dynamodb.ts b/packages/@aws-cdk/aws-dynamodb/test/integ.dynamodb.ts index 574a64773a08a..cbd34dd093899 100644 --- a/packages/@aws-cdk/aws-dynamodb/test/integ.dynamodb.ts +++ b/packages/@aws-cdk/aws-dynamodb/test/integ.dynamodb.ts @@ -7,7 +7,8 @@ const stack = new Stack(app, 'aws-cdk-dynamodb'); const table = new Table(stack, 'Table', { sseEnabled: true, - streamSpecification: StreamViewType.KeysOnly + streamSpecification: StreamViewType.KeysOnly, + ttlAttributeName: 'timeToLive' }); table.addPartitionKey('hashKey', KeyAttributeType.String); diff --git a/packages/@aws-cdk/aws-dynamodb/test/test.dynamodb.ts b/packages/@aws-cdk/aws-dynamodb/test/test.dynamodb.ts index 8dee188a8e2c4..b7721e3700aa2 100644 --- a/packages/@aws-cdk/aws-dynamodb/test/test.dynamodb.ts +++ b/packages/@aws-cdk/aws-dynamodb/test/test.dynamodb.ts @@ -116,6 +116,34 @@ export = { test.done(); }, + 'ttl is not enabled'(test: Test) { + const app = new TestApp(); + new Table(app.stack, 'MyTable') + .addPartitionKey('partitionKey', KeyAttributeType.Binary) + .addSortKey('sortKey', KeyAttributeType.Number); + const template = app.synthesizeTemplate(); + + test.deepEqual(template, { + Resources: { + MyTable794EDED1: { + Type: 'AWS::DynamoDB::Table', + Properties: { + AttributeDefinitions: [ + { AttributeName: 'partitionKey', AttributeType: 'B' }, + { AttributeName: 'sortKey', AttributeType: 'N' } + ], + KeySchema: [ + { AttributeName: 'partitionKey', KeyType: 'HASH' }, + { AttributeName: 'sortKey', KeyType: 'RANGE' } + ], + ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 }, + } + } + } + }); + + test.done(); + }, 'can specify new and old images'(test: Test) { const app = new TestApp(); const table = new Table(app.stack, 'MyTable', { @@ -230,7 +258,8 @@ export = { readCapacity: 42, writeCapacity: 1337, sseEnabled: true, - streamSpecification: StreamViewType.KeysOnly + streamSpecification: StreamViewType.KeysOnly, + ttlAttributeName: 'timeToLive' }); table.addPartitionKey('partitionKey', KeyAttributeType.String); table.addSortKey('sortKey', KeyAttributeType.Binary); @@ -256,6 +285,7 @@ export = { SSESpecification: { SSEEnabled: true }, StreamSpecification: { StreamViewType: 'KEYS_ONLY' }, TableName: 'MyTable', + TimeToLiveSpecification: { AttributeName: 'timeToLive', Enabled: true } } } }