Skip to content

Commit cac11bb

Browse files
authored
fix(aws-kinesis): remove default shard count when stream mode is on-demand and set default mode to provisioned (#18221)
Change the default Kinesis Data Stream's stream mode to provisioned from undefined to make the active configuration more explicit in the resulting CloudFormation templates. Fix an issue whereby the shard count is always set when the stream mode is set to on-demand, which is invalid. Shard count still defaults to `1` in provisioned mode, but is left undefined in on-demand mode. Add validation for the above so that an error is thrown from CDK when specifying on-demand mode with a shard count. Fixes #18139 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 7868869 commit cac11bb

File tree

9 files changed

+162
-14
lines changed

9 files changed

+162
-14
lines changed

packages/@aws-cdk/aws-dynamodb/test/integ.dynamodb.kinesis-stream.expected.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"Type": "AWS::Kinesis::Stream",
55
"Properties": {
66
"ShardCount": 1,
7+
"StreamModeDetails": {
8+
"StreamMode": "PROVISIONED"
9+
},
710
"RetentionPeriodHours": 24,
811
"StreamEncryption": {
912
"Fn::If": [
@@ -73,4 +76,4 @@
7376
]
7477
}
7578
}
76-
}
79+
}

packages/@aws-cdk/aws-events-targets/test/kinesis/integ.kinesis-stream.expected.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"Type": "AWS::Kinesis::Stream",
55
"Properties": {
66
"ShardCount": 1,
7+
"StreamModeDetails": {
8+
"StreamMode": "PROVISIONED"
9+
},
710
"RetentionPeriodHours": 24,
811
"StreamEncryption": {
912
"Fn::If": [
@@ -115,4 +118,4 @@
115118
]
116119
}
117120
}
118-
}
121+
}

packages/@aws-cdk/aws-kinesis/lib/stream.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,9 @@ export interface StreamProps {
673673

674674
/**
675675
* The number of shards for the stream.
676+
*
677+
* Can only be provided if streamMode is Provisioned.
678+
*
676679
* @default 1
677680
*/
678681
readonly shardCount?: number;
@@ -752,9 +755,15 @@ export class Stream extends StreamBase {
752755
physicalName: props.streamName,
753756
});
754757

755-
const shardCount = props.shardCount || 1;
758+
let shardCount = props.shardCount;
759+
const streamMode = props.streamMode ?? StreamMode.PROVISIONED;
756760

757-
const streamMode = props.streamMode;
761+
if (streamMode === StreamMode.ON_DEMAND && shardCount !== undefined) {
762+
throw new Error(`streamMode must be set to ${StreamMode.PROVISIONED} (default) when specifying shardCount`);
763+
}
764+
if (streamMode === StreamMode.PROVISIONED && shardCount === undefined) {
765+
shardCount = 1;
766+
}
758767

759768
const retentionPeriodHours = props.retentionPeriod?.toHours() ?? 24;
760769
if (!Token.isUnresolved(retentionPeriodHours)) {

packages/@aws-cdk/aws-kinesis/test/integ.stream-dashboard.expected.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"Type": "AWS::Kinesis::Stream",
55
"Properties": {
66
"ShardCount": 1,
7+
"StreamModeDetails": {
8+
"StreamMode": "PROVISIONED"
9+
},
710
"RetentionPeriodHours": 24,
811
"StreamEncryption": {
912
"Fn::If": [
@@ -203,4 +206,4 @@
203206
]
204207
}
205208
}
206-
}
209+
}

packages/@aws-cdk/aws-kinesis/test/integ.stream.expected.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@
7272
"Type": "AWS::Kinesis::Stream",
7373
"Properties": {
7474
"ShardCount": 1,
75+
"StreamModeDetails": {
76+
"StreamMode": "PROVISIONED"
77+
},
7578
"RetentionPeriodHours": 24,
7679
"StreamEncryption": {
7780
"Fn::If": [
@@ -110,4 +113,4 @@
110113
]
111114
}
112115
}
113-
}
116+
}

packages/@aws-cdk/aws-kinesis/test/stream.test.ts

Lines changed: 123 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ describe('Kinesis data streams', () => {
2222
Type: 'AWS::Kinesis::Stream',
2323
Properties: {
2424
ShardCount: 1,
25+
StreamModeDetails: {
26+
StreamMode: StreamMode.PROVISIONED,
27+
},
2528
RetentionPeriodHours: 24,
2629
StreamEncryption: {
2730
'Fn::If': [
@@ -75,6 +78,9 @@ describe('Kinesis data streams', () => {
7578
Type: 'AWS::Kinesis::Stream',
7679
Properties: {
7780
ShardCount: 1,
81+
StreamModeDetails: {
82+
StreamMode: StreamMode.PROVISIONED,
83+
},
7884
RetentionPeriodHours: 24,
7985
StreamEncryption: {
8086
'Fn::If': [
@@ -94,6 +100,9 @@ describe('Kinesis data streams', () => {
94100
Type: 'AWS::Kinesis::Stream',
95101
Properties: {
96102
ShardCount: 1,
103+
StreamModeDetails: {
104+
StreamMode: StreamMode.PROVISIONED,
105+
},
97106
RetentionPeriodHours: 24,
98107
StreamEncryption: {
99108
'Fn::If': [
@@ -158,6 +167,9 @@ describe('Kinesis data streams', () => {
158167
Type: 'AWS::Kinesis::Stream',
159168
Properties: {
160169
ShardCount: 2,
170+
StreamModeDetails: {
171+
StreamMode: StreamMode.PROVISIONED,
172+
},
161173
RetentionPeriodHours: 24,
162174
StreamEncryption: {
163175
'Fn::If': [
@@ -212,6 +224,9 @@ describe('Kinesis data streams', () => {
212224
Type: 'AWS::Kinesis::Stream',
213225
Properties: {
214226
ShardCount: 1,
227+
StreamModeDetails: {
228+
StreamMode: StreamMode.PROVISIONED,
229+
},
215230
RetentionPeriodHours: 168,
216231
StreamEncryption: {
217232
'Fn::If': [
@@ -283,6 +298,9 @@ describe('Kinesis data streams', () => {
283298
Type: 'AWS::Kinesis::Stream',
284299
Properties: {
285300
ShardCount: 1,
301+
StreamModeDetails: {
302+
StreamMode: StreamMode.PROVISIONED,
303+
},
286304
RetentionPeriodHours: 24,
287305
StreamEncryption: {
288306
EncryptionType: 'KMS',
@@ -319,6 +337,9 @@ describe('Kinesis data streams', () => {
319337
// THEN
320338
expect(stack).toHaveResource('AWS::Kinesis::Stream', {
321339
ShardCount: 1,
340+
StreamModeDetails: {
341+
StreamMode: StreamMode.PROVISIONED,
342+
},
322343
RetentionPeriodHours: 24,
323344
StreamEncryption: {
324345
EncryptionType: 'KMS',
@@ -365,31 +386,90 @@ describe('Kinesis data streams', () => {
365386
});
366387

367388
expect(stack).toHaveResource('AWS::Kinesis::Stream', {
368-
RetentionPeriodHours: 24,
369389
ShardCount: 1,
390+
StreamModeDetails: {
391+
StreamMode: StreamMode.PROVISIONED,
392+
},
393+
RetentionPeriodHours: 24,
370394
StreamEncryption: {
371395
EncryptionType: 'KMS',
372396
KeyId: stack.resolve(explicitKey.keyArn),
373397
},
374398
});
375399
}),
376400

377-
test.each([StreamMode.ON_DEMAND, StreamMode.PROVISIONED])('uses explicit capacity mode %s', (mode: StreamMode) => {
401+
test('uses explicit provisioned streamMode', () => {
378402
const stack = new Stack();
379403

380404
new Stream(stack, 'MyStream', {
381-
streamMode: mode,
405+
streamMode: StreamMode.PROVISIONED,
382406
});
383407

384408
expect(stack).toMatchTemplate({
385409
Resources: {
386410
MyStream5C050E93: {
387411
Type: 'AWS::Kinesis::Stream',
388412
Properties: {
413+
RetentionPeriodHours: 24,
389414
ShardCount: 1,
415+
StreamModeDetails: {
416+
StreamMode: StreamMode.PROVISIONED,
417+
},
418+
StreamEncryption: {
419+
'Fn::If': [
420+
'AwsCdkKinesisEncryptedStreamsUnsupportedRegions',
421+
{
422+
Ref: 'AWS::NoValue',
423+
},
424+
{
425+
EncryptionType: 'KMS',
426+
KeyId: 'alias/aws/kinesis',
427+
},
428+
],
429+
},
430+
},
431+
},
432+
},
433+
Conditions: {
434+
AwsCdkKinesisEncryptedStreamsUnsupportedRegions: {
435+
'Fn::Or': [
436+
{
437+
'Fn::Equals': [
438+
{
439+
Ref: 'AWS::Region',
440+
},
441+
'cn-north-1',
442+
],
443+
},
444+
{
445+
'Fn::Equals': [
446+
{
447+
Ref: 'AWS::Region',
448+
},
449+
'cn-northwest-1',
450+
],
451+
},
452+
],
453+
},
454+
},
455+
});
456+
});
457+
458+
test('uses explicit on-demand streamMode', () => {
459+
const stack = new Stack();
460+
461+
new Stream(stack, 'MyStream', {
462+
streamMode: StreamMode.ON_DEMAND,
463+
});
464+
465+
expect(stack).toMatchTemplate({
466+
Resources: {
467+
MyStream5C050E93: {
468+
Type: 'AWS::Kinesis::Stream',
469+
Properties: {
390470
RetentionPeriodHours: 24,
391471
StreamModeDetails: {
392-
StreamMode: StreamMode[mode],
472+
StreamMode: StreamMode.ON_DEMAND,
393473
},
394474
StreamEncryption: {
395475
'Fn::If': [
@@ -431,6 +511,17 @@ describe('Kinesis data streams', () => {
431511
});
432512
});
433513

514+
test('throws when using shardCount with on-demand streamMode', () => {
515+
const stack = new Stack();
516+
517+
expect(() => {
518+
new Stream(stack, 'MyStream', {
519+
shardCount: 2,
520+
streamMode: StreamMode.ON_DEMAND,
521+
});
522+
}).toThrow(`streamMode must be set to ${StreamMode.PROVISIONED} (default) when specifying shardCount`);
523+
});
524+
434525
test('grantRead creates and attaches a policy with read only access to the principal', () => {
435526
const stack = new Stack();
436527
const stream = new Stream(stack, 'MyStream', {
@@ -536,6 +627,9 @@ describe('Kinesis data streams', () => {
536627
Type: 'AWS::Kinesis::Stream',
537628
Properties: {
538629
ShardCount: 1,
630+
StreamModeDetails: {
631+
StreamMode: StreamMode.PROVISIONED,
632+
},
539633
RetentionPeriodHours: 24,
540634
StreamEncryption: {
541635
EncryptionType: 'KMS',
@@ -695,6 +789,9 @@ describe('Kinesis data streams', () => {
695789
Type: 'AWS::Kinesis::Stream',
696790
Properties: {
697791
ShardCount: 1,
792+
StreamModeDetails: {
793+
StreamMode: StreamMode.PROVISIONED,
794+
},
698795
RetentionPeriodHours: 24,
699796
StreamEncryption: {
700797
EncryptionType: 'KMS',
@@ -845,8 +942,11 @@ describe('Kinesis data streams', () => {
845942
MyStream5C050E93: {
846943
Type: 'AWS::Kinesis::Stream',
847944
Properties: {
848-
RetentionPeriodHours: 24,
849945
ShardCount: 1,
946+
StreamModeDetails: {
947+
StreamMode: StreamMode.PROVISIONED,
948+
},
949+
RetentionPeriodHours: 24,
850950
StreamEncryption: {
851951
EncryptionType: 'KMS',
852952
KeyId: {
@@ -915,6 +1015,9 @@ describe('Kinesis data streams', () => {
9151015
Type: 'AWS::Kinesis::Stream',
9161016
Properties: {
9171017
ShardCount: 1,
1018+
StreamModeDetails: {
1019+
StreamMode: StreamMode.PROVISIONED,
1020+
},
9181021
RetentionPeriodHours: 24,
9191022
StreamEncryption: {
9201023
'Fn::If': [
@@ -1003,6 +1106,9 @@ describe('Kinesis data streams', () => {
10031106
Type: 'AWS::Kinesis::Stream',
10041107
Properties: {
10051108
ShardCount: 1,
1109+
StreamModeDetails: {
1110+
StreamMode: StreamMode.PROVISIONED,
1111+
},
10061112
RetentionPeriodHours: 24,
10071113
StreamEncryption: {
10081114
'Fn::If': [
@@ -1083,6 +1189,9 @@ describe('Kinesis data streams', () => {
10831189
Type: 'AWS::Kinesis::Stream',
10841190
Properties: {
10851191
ShardCount: 1,
1192+
StreamModeDetails: {
1193+
StreamMode: StreamMode.PROVISIONED,
1194+
},
10861195
RetentionPeriodHours: 24,
10871196
StreamEncryption: {
10881197
'Fn::If': [
@@ -1173,6 +1282,9 @@ describe('Kinesis data streams', () => {
11731282
Type: 'AWS::Kinesis::Stream',
11741283
Properties: {
11751284
ShardCount: 1,
1285+
StreamModeDetails: {
1286+
StreamMode: StreamMode.PROVISIONED,
1287+
},
11761288
RetentionPeriodHours: 24,
11771289
StreamEncryption: {
11781290
'Fn::If': [
@@ -1255,6 +1367,9 @@ describe('Kinesis data streams', () => {
12551367
Type: 'AWS::Kinesis::Stream',
12561368
Properties: {
12571369
ShardCount: 1,
1370+
StreamModeDetails: {
1371+
StreamMode: StreamMode.PROVISIONED,
1372+
},
12581373
RetentionPeriodHours: 24,
12591374
StreamEncryption: {
12601375
'Fn::If': [
@@ -1372,6 +1487,9 @@ describe('Kinesis data streams', () => {
13721487
Type: 'AWS::Kinesis::Stream',
13731488
Properties: {
13741489
ShardCount: 1,
1490+
StreamModeDetails: {
1491+
StreamMode: StreamMode.PROVISIONED,
1492+
},
13751493
RetentionPeriodHours: {
13761494
Ref: 'myretentionperiod',
13771495
},

packages/@aws-cdk/aws-kinesisfirehose/test/integ.delivery-stream.source-stream.expected.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@
7575
"Type": "AWS::Kinesis::Stream",
7676
"Properties": {
7777
"ShardCount": 1,
78+
"StreamModeDetails": {
79+
"StreamMode": "PROVISIONED"
80+
},
7881
"RetentionPeriodHours": 24,
7982
"StreamEncryption": {
8083
"Fn::If": [
@@ -281,4 +284,4 @@
281284
}
282285
}
283286
}
284-
}
287+
}

0 commit comments

Comments
 (0)