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

feat(dynamodb): add seed capacity property to support changing table billing mode #27734

Merged
merged 4 commits into from
Nov 1, 2023
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"WriteCapacityAutoScalingSettings": {
"MaxCapacity": 20,
"MinCapacity": 1,
"SeedCapacity": 10,
"TargetTrackingScalingPolicyConfiguration": {
"TargetValue": 60
}
Expand Down Expand Up @@ -251,6 +252,7 @@
"WriteCapacityAutoScalingSettings": {
"MaxCapacity": 20,
"MinCapacity": 1,
"SeedCapacity": 10,
"TargetTrackingScalingPolicyConfiguration": {
"TargetValue": 60
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TestStack extends Stack {
sortKey: { name: 'sk', type: AttributeType.NUMBER },
billing: Billing.provisioned({
readCapacity: Capacity.fixed(10),
writeCapacity: Capacity.autoscaled({ maxCapacity: 20, targetUtilizationPercent: 60 }),
writeCapacity: Capacity.autoscaled({ maxCapacity: 20, targetUtilizationPercent: 60, seedCapacity: 10 }),
}),
encryption: TableEncryptionV2.awsManagedKey(),
contributorInsights: true,
Expand Down
12 changes: 12 additions & 0 deletions packages/aws-cdk-lib/aws-dynamodb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,18 @@ const globalTable = new dynamodb.TableV2(stack, 'GlobalTable', {
});
```

When changing the billing for a table from provisioned to on-demand or from on-demand to provisioned, `seedCapacity` must be configured for each autoscaled resource:

```ts
const globalTable = new dynamodb.TableV2(this, 'Table', {
partitionKey: { name: 'pk', type: dynamodb.AttributeType.STRING },
billing: dynamodb.Billing.provisioned({
readCapacity: dynamodb.Capacity.fixed(10),
writeCapacity: dynamodb.Capacity.autoscaled({ maxCapacity: 10, seedCapacity: 20 }),
}),
});
```

Further reading:
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadWriteCapacityMode.html

Expand Down
16 changes: 15 additions & 1 deletion packages/aws-cdk-lib/aws-dynamodb/lib/capacity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,16 @@ export interface AutoscaledCapacityOptions {
*
* @default 70
*/
readonly targetUtilizationPercent?: number
readonly targetUtilizationPercent?: number;

/**
* If you want to switch a table's billing mode from on-demand to provisioned or
* from provisioned to on-demand, you must specify a value for this property for
* each autoscaled resource.
*
* @default no seed capacity
*/
readonly seedCapacity?: number;
}

/**
Expand Down Expand Up @@ -85,6 +94,10 @@ export abstract class Capacity {
if (options.targetUtilizationPercent !== undefined && (options.targetUtilizationPercent < 20 || options.targetUtilizationPercent > 90)) {
throw new Error('`targetUtilizationPercent` cannot be less than 20 or greater than 90');
}

if (options.seedCapacity !== undefined && (options.seedCapacity < 1)) {
throw new Error(`'seedCapacity' cannot be less than 1 - received ${options.seedCapacity}`);
}
}

public _renderReadCapacity() {
Expand All @@ -103,6 +116,7 @@ export abstract class Capacity {
return {
minCapacity: options.minCapacity ?? 1,
maxCapacity: options.maxCapacity,
seedCapacity: options.seedCapacity,
targetTrackingScalingPolicyConfiguration: {
targetValue: options.targetUtilizationPercent ?? 70,
},
Expand Down
24 changes: 24 additions & 0 deletions packages/aws-cdk-lib/aws-dynamodb/test/capacity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,23 @@ describe('autoscaled capacity', () => {
});
});

test('can specify seed capacity', () => {
// GIVEN
const capacity = Capacity.autoscaled({ maxCapacity: 10, seedCapacity: 20 });

// WHEN / THEN
expect(capacity._renderReadCapacity()).toEqual({
readCapacityAutoScalingSettings: {
minCapacity: 1,
maxCapacity: 10,
seedCapacity: 20,
targetTrackingScalingPolicyConfiguration: {
targetValue: 70,
},
},
});
});

test('capacity mode is AUTOSCALED', () => {
// GIVEN
const capacity = Capacity.autoscaled({ minCapacity: 1, maxCapacity: 10 });
Expand Down Expand Up @@ -118,4 +135,11 @@ describe('autoscaled capacity', () => {
Capacity.autoscaled({ maxCapacity: 10, targetUtilizationPercent: 91 });
}).toThrow('`targetUtilizationPercent` cannot be less than 20 or greater than 90');
});

test('throws if seed capacity is less than 1', () => {
// GIVEN / WHEN / THEN
expect(() => {
Capacity.autoscaled({ maxCapacity: 10, seedCapacity: 0 });
}).toThrow("'seedCapacity' cannot be less than 1 - received 0");
});
});
Loading