Skip to content

Commit 73a889e

Browse files
authored
feat(dynamodb): allow setting TableClass for a Table (#18719)
Support already exists in CloudFormation, but hasn't been implemented in CDK. Closes #18718 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 9380885 commit 73a889e

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,23 @@ const table = new dynamodb.Table(this, 'Table', {
5858
Further reading:
5959
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadWriteCapacityMode.
6060

61+
## Table Class
62+
63+
DynamoDB supports two table classes:
64+
65+
* STANDARD - the default mode, and is recommended for the vast majority of workloads.
66+
* STANDARD_INFREQUENT_ACCESS - optimized for tables where storage is the dominant cost.
67+
68+
```ts
69+
const table = new dynamodb.Table(this, 'Table', {
70+
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
71+
tableClass: dynamodb.TableClass.STANDARD_INFREQUENT_ACCESS,
72+
});
73+
```
74+
75+
Further reading:
76+
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.TableClasses.html
77+
6178
## Configure AutoScaling for your table
6279

6380
You can have DynamoDB automatically raise and lower the read and write capacities

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ export interface TableOptions extends SchemaOptions {
199199
*/
200200
readonly serverSideEncryption?: boolean;
201201

202+
/**
203+
* Specify the table class.
204+
* @default STANDARD
205+
*/
206+
readonly tableClass?: TableClass;
207+
202208
/**
203209
* Whether server-side encryption with an AWS managed customer master key is enabled.
204210
*
@@ -1182,6 +1188,7 @@ export class Table extends TableBase {
11821188
},
11831189
sseSpecification,
11841190
streamSpecification,
1191+
tableClass: props.tableClass,
11851192
timeToLiveSpecification: props.timeToLiveAttribute ? { attributeName: props.timeToLiveAttribute, enabled: true } : undefined,
11861193
contributorInsightsSpecification: props.contributorInsightsEnabled !== undefined ? { enabled: props.contributorInsightsEnabled } : undefined,
11871194
kinesisStreamSpecification: props.kinesisStream ? { streamArn: props.kinesisStream.streamArn } : undefined,
@@ -1773,6 +1780,19 @@ export enum StreamViewType {
17731780
KEYS_ONLY = 'KEYS_ONLY'
17741781
}
17751782

1783+
/**
1784+
* DynamoDB's table class.
1785+
*
1786+
* @see https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.TableClasses.html
1787+
*/
1788+
export enum TableClass {
1789+
/** Default table class for DynamoDB. */
1790+
STANDARD = 'STANDARD',
1791+
1792+
/** Table class for DynamoDB that reduces storage costs compared to existing DynamoDB Standard tables. */
1793+
STANDARD_INFREQUENT_ACCESS = 'STANDARD_INFREQUENT_ACCESS',
1794+
}
1795+
17761796
/**
17771797
* Just a convenient way to keep track of both attributes
17781798
*/

packages/@aws-cdk/aws-dynamodb/test/dynamodb.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
ProjectionType,
1818
StreamViewType,
1919
Table,
20+
TableClass,
2021
TableEncryption,
2122
Operation,
2223
CfnTable,
@@ -719,6 +720,47 @@ test('if an encryption key is included, encrypt/decrypt permissions are added to
719720
});
720721
});
721722

723+
test('when specifying STANDARD_INFREQUENT_ACCESS table class', () => {
724+
const stack = new Stack();
725+
new Table(stack, CONSTRUCT_NAME, {
726+
partitionKey: TABLE_PARTITION_KEY,
727+
tableClass: TableClass.STANDARD_INFREQUENT_ACCESS,
728+
});
729+
730+
Template.fromStack(stack).hasResourceProperties('AWS::DynamoDB::Table',
731+
{
732+
TableClass: 'STANDARD_INFREQUENT_ACCESS',
733+
},
734+
);
735+
});
736+
737+
test('when specifying STANDARD table class', () => {
738+
const stack = new Stack();
739+
new Table(stack, CONSTRUCT_NAME, {
740+
partitionKey: TABLE_PARTITION_KEY,
741+
tableClass: TableClass.STANDARD,
742+
});
743+
744+
Template.fromStack(stack).hasResourceProperties('AWS::DynamoDB::Table',
745+
{
746+
TableClass: 'STANDARD',
747+
},
748+
);
749+
});
750+
751+
test('when specifying no table class', () => {
752+
const stack = new Stack();
753+
new Table(stack, CONSTRUCT_NAME, {
754+
partitionKey: TABLE_PARTITION_KEY,
755+
});
756+
757+
Template.fromStack(stack).hasResourceProperties('AWS::DynamoDB::Table',
758+
{
759+
TableClass: Match.absent(),
760+
},
761+
);
762+
});
763+
722764
test('when specifying PAY_PER_REQUEST billing mode', () => {
723765
const stack = new Stack();
724766
new Table(stack, CONSTRUCT_NAME, {

0 commit comments

Comments
 (0)