Skip to content

Commit

Permalink
feat(aws-dynamism): Support DynamoDB TTL (#691)
Browse files Browse the repository at this point in the history
This patch supports DynamoDB TTL.
  • Loading branch information
jungseoklee authored and Elad Ben-Israel committed Sep 12, 2018
1 parent b222731 commit 35b6206
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
9 changes: 8 additions & 1 deletion packages/@aws-cdk/aws-dynamodb/lib/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
},
"StreamSpecification": {
"StreamViewType": "KEYS_ONLY"
},
"TimeToLiveSpecification": {
"AttributeName": "timeToLive",
"Enabled": true
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-dynamodb/test/integ.dynamodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
32 changes: 31 additions & 1 deletion packages/@aws-cdk/aws-dynamodb/test/test.dynamodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down Expand Up @@ -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);
Expand All @@ -256,6 +285,7 @@ export = {
SSESpecification: { SSEEnabled: true },
StreamSpecification: { StreamViewType: 'KEYS_ONLY' },
TableName: 'MyTable',
TimeToLiveSpecification: { AttributeName: 'timeToLive', Enabled: true }
}
}
}
Expand Down

0 comments on commit 35b6206

Please sign in to comment.