Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support DynamoDB TTL #691

Merged
merged 1 commit into from
Sep 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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