Skip to content

Commit 22168b1

Browse files
authored
feat(dynamodb): add seed capacity property to support changing table billing mode (#27734)
This PR adds `seedCapacity` as a property on the `AutoscaledCapacityOptions` interface. `seedCapacity` must be provided for each autoscaled resource when changing a table's billing from on-demand to provisioned or from provisioned to on-demand. Closes #27735. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 857ab7d commit 22168b1

File tree

8 files changed

+61
-5
lines changed

8 files changed

+61
-5
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.table-v2-global.js.snapshot/aws-cdk-global-table.assets.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.table-v2-global.js.snapshot/aws-cdk-global-table.template.json

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"WriteCapacityAutoScalingSettings": {
5050
"MaxCapacity": 20,
5151
"MinCapacity": 1,
52+
"SeedCapacity": 10,
5253
"TargetTrackingScalingPolicyConfiguration": {
5354
"TargetValue": 60
5455
}
@@ -251,6 +252,7 @@
251252
"WriteCapacityAutoScalingSettings": {
252253
"MaxCapacity": 20,
253254
"MinCapacity": 1,
255+
"SeedCapacity": 10,
254256
"TargetTrackingScalingPolicyConfiguration": {
255257
"TargetValue": 60
256258
}

packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.table-v2-global.js.snapshot/manifest.json

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.table-v2-global.js.snapshot/tree.json

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-dynamodb/test/integ.table-v2-global.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class TestStack extends Stack {
1616
sortKey: { name: 'sk', type: AttributeType.NUMBER },
1717
billing: Billing.provisioned({
1818
readCapacity: Capacity.fixed(10),
19-
writeCapacity: Capacity.autoscaled({ maxCapacity: 20, targetUtilizationPercent: 60 }),
19+
writeCapacity: Capacity.autoscaled({ maxCapacity: 20, targetUtilizationPercent: 60, seedCapacity: 10 }),
2020
}),
2121
encryption: TableEncryptionV2.awsManagedKey(),
2222
contributorInsights: true,

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

+12
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,18 @@ const globalTable = new dynamodb.TableV2(stack, 'GlobalTable', {
201201
});
202202
```
203203

204+
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:
205+
206+
```ts
207+
const globalTable = new dynamodb.TableV2(this, 'Table', {
208+
partitionKey: { name: 'pk', type: dynamodb.AttributeType.STRING },
209+
billing: dynamodb.Billing.provisioned({
210+
readCapacity: dynamodb.Capacity.fixed(10),
211+
writeCapacity: dynamodb.Capacity.autoscaled({ maxCapacity: 10, seedCapacity: 20 }),
212+
}),
213+
});
214+
```
215+
204216
Further reading:
205217
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadWriteCapacityMode.html
206218

packages/aws-cdk-lib/aws-dynamodb/lib/capacity.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,16 @@ export interface AutoscaledCapacityOptions {
3939
*
4040
* @default 70
4141
*/
42-
readonly targetUtilizationPercent?: number
42+
readonly targetUtilizationPercent?: number;
43+
44+
/**
45+
* If you want to switch a table's billing mode from on-demand to provisioned or
46+
* from provisioned to on-demand, you must specify a value for this property for
47+
* each autoscaled resource.
48+
*
49+
* @default no seed capacity
50+
*/
51+
readonly seedCapacity?: number;
4352
}
4453

4554
/**
@@ -85,6 +94,10 @@ export abstract class Capacity {
8594
if (options.targetUtilizationPercent !== undefined && (options.targetUtilizationPercent < 20 || options.targetUtilizationPercent > 90)) {
8695
throw new Error('`targetUtilizationPercent` cannot be less than 20 or greater than 90');
8796
}
97+
98+
if (options.seedCapacity !== undefined && (options.seedCapacity < 1)) {
99+
throw new Error(`'seedCapacity' cannot be less than 1 - received ${options.seedCapacity}`);
100+
}
88101
}
89102

90103
public _renderReadCapacity() {
@@ -103,6 +116,7 @@ export abstract class Capacity {
103116
return {
104117
minCapacity: options.minCapacity ?? 1,
105118
maxCapacity: options.maxCapacity,
119+
seedCapacity: options.seedCapacity,
106120
targetTrackingScalingPolicyConfiguration: {
107121
targetValue: options.targetUtilizationPercent ?? 70,
108122
},

packages/aws-cdk-lib/aws-dynamodb/test/capacity.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,23 @@ describe('autoscaled capacity', () => {
9090
});
9191
});
9292

93+
test('can specify seed capacity', () => {
94+
// GIVEN
95+
const capacity = Capacity.autoscaled({ maxCapacity: 10, seedCapacity: 20 });
96+
97+
// WHEN / THEN
98+
expect(capacity._renderReadCapacity()).toEqual({
99+
readCapacityAutoScalingSettings: {
100+
minCapacity: 1,
101+
maxCapacity: 10,
102+
seedCapacity: 20,
103+
targetTrackingScalingPolicyConfiguration: {
104+
targetValue: 70,
105+
},
106+
},
107+
});
108+
});
109+
93110
test('capacity mode is AUTOSCALED', () => {
94111
// GIVEN
95112
const capacity = Capacity.autoscaled({ minCapacity: 1, maxCapacity: 10 });
@@ -118,4 +135,11 @@ describe('autoscaled capacity', () => {
118135
Capacity.autoscaled({ maxCapacity: 10, targetUtilizationPercent: 91 });
119136
}).toThrow('`targetUtilizationPercent` cannot be less than 20 or greater than 90');
120137
});
138+
139+
test('throws if seed capacity is less than 1', () => {
140+
// GIVEN / WHEN / THEN
141+
expect(() => {
142+
Capacity.autoscaled({ maxCapacity: 10, seedCapacity: 0 });
143+
}).toThrow("'seedCapacity' cannot be less than 1 - received 0");
144+
});
121145
});

0 commit comments

Comments
 (0)