-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathstream.ts
912 lines (810 loc) · 32.9 KB
/
stream.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
import { Construct } from 'constructs';
import { KinesisMetrics } from './kinesis-fixed-canned-metrics';
import { CfnStream } from './kinesis.generated';
import * as cloudwatch from '../../aws-cloudwatch';
import * as iam from '../../aws-iam';
import * as kms from '../../aws-kms';
import { ArnFormat, Aws, CfnCondition, Duration, Fn, IResolvable, IResource, RemovalPolicy, Resource, Stack, Token } from '../../core';
const READ_OPERATIONS = [
'kinesis:DescribeStreamSummary',
'kinesis:GetRecords',
'kinesis:GetShardIterator',
'kinesis:ListShards',
'kinesis:SubscribeToShard',
'kinesis:DescribeStream',
'kinesis:ListStreams',
'kinesis:DescribeStreamConsumer',
];
const WRITE_OPERATIONS = [
'kinesis:ListShards',
'kinesis:PutRecord',
'kinesis:PutRecords',
];
/**
* A Kinesis Stream
*/
export interface IStream extends IResource {
/**
* The ARN of the stream.
*
* @attribute
*/
readonly streamArn: string;
/**
* The name of the stream
*
* @attribute
*/
readonly streamName: string;
/**
* Optional KMS encryption key associated with this stream.
*/
readonly encryptionKey?: kms.IKey;
/**
* Grant read permissions for this stream and its contents to an IAM
* principal (Role/Group/User).
*
* If an encryption key is used, permission to ues the key to decrypt the
* contents of the stream will also be granted.
*/
grantRead(grantee: iam.IGrantable): iam.Grant;
/**
* Grant write permissions for this stream and its contents to an IAM
* principal (Role/Group/User).
*
* If an encryption key is used, permission to ues the key to encrypt the
* contents of the stream will also be granted.
*/
grantWrite(grantee: iam.IGrantable): iam.Grant;
/**
* Grants read/write permissions for this stream and its contents to an IAM
* principal (Role/Group/User).
*
* If an encryption key is used, permission to use the key for
* encrypt/decrypt will also be granted.
*/
grantReadWrite(grantee: iam.IGrantable): iam.Grant;
/**
* Grant the indicated permissions on this stream to the provided IAM principal.
*/
grant(grantee: iam.IGrantable, ...actions: string[]): iam.Grant;
/**
* Return stream metric based from its metric name
*
* @param metricName name of the stream metric
* @param props properties of the metric
*/
metric(metricName: string, props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* The number of bytes retrieved from the Kinesis stream, measured over the specified time period. Minimum, Maximum,
* and Average statistics represent the bytes in a single GetRecords operation for the stream in the specified time
* period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
metricGetRecordsBytes(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* The age of the last record in all GetRecords calls made against a Kinesis stream, measured over the specified time
* period. Age is the difference between the current time and when the last record of the GetRecords call was written
* to the stream. The Minimum and Maximum statistics can be used to track the progress of Kinesis consumer
* applications. A value of zero indicates that the records being read are completely caught up with the stream.
*
* The metric defaults to maximum over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
metricGetRecordsIteratorAgeMilliseconds(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* The time taken per GetRecords operation, measured over the specified time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
metricGetRecordsLatency(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* The number of records retrieved from the shard, measured over the specified time period. Minimum, Maximum, and
* Average statistics represent the records in a single GetRecords operation for the stream in the specified time
* period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
metricGetRecords(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* The number of successful GetRecords operations per stream, measured over the specified time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
metricGetRecordsSuccess(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* The number of bytes successfully put to the Kinesis stream over the specified time period. This metric includes
* bytes from PutRecord and PutRecords operations. Minimum, Maximum, and Average statistics represent the bytes in a
* single put operation for the stream in the specified time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
metricIncomingBytes(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* The number of records successfully put to the Kinesis stream over the specified time period. This metric includes
* record counts from PutRecord and PutRecords operations. Minimum, Maximum, and Average statistics represent the
* records in a single put operation for the stream in the specified time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
metricIncomingRecords(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* The number of bytes put to the Kinesis stream using the PutRecord operation over the specified time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
metricPutRecordBytes(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* The time taken per PutRecord operation, measured over the specified time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
metricPutRecordLatency(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* The number of successful PutRecord operations per Kinesis stream, measured over the specified time period. Average
* reflects the percentage of successful writes to a stream.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
metricPutRecordSuccess(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* The number of bytes put to the Kinesis stream using the PutRecords operation over the specified time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
metricPutRecordsBytes(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* The time taken per PutRecords operation, measured over the specified time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
metricPutRecordsLatency(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* The number of PutRecords operations where at least one record succeeded, per Kinesis stream, measured over the
* specified time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
metricPutRecordsSuccess(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* The total number of records sent in a PutRecords operation per Kinesis data stream, measured over the specified
* time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
metricPutRecordsTotalRecords(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* The number of successful records in a PutRecords operation per Kinesis data stream, measured over the specified
* time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
metricPutRecordsSuccessfulRecords(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* The number of records rejected due to internal failures in a PutRecords operation per Kinesis data stream,
* measured over the specified time period. Occasional internal failures are to be expected and should be retried.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
metricPutRecordsFailedRecords(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* The number of records rejected due to throttling in a PutRecords operation per Kinesis data stream, measured over
* the specified time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
metricPutRecordsThrottledRecords(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* The number of GetRecords calls throttled for the stream over the specified time period. The most commonly used
* statistic for this metric is Average.
*
* When the Minimum statistic has a value of 1, all records were throttled for the stream during the specified time
* period.
*
* When the Maximum statistic has a value of 0 (zero), no records were throttled for the stream during the specified
* time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties
*
* @param props properties of the metric
*
*/
metricReadProvisionedThroughputExceeded(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
/**
* The number of records rejected due to throttling for the stream over the specified time period. This metric
* includes throttling from PutRecord and PutRecords operations.
*
* When the Minimum statistic has a non-zero value, records were being throttled for the stream during the specified
* time period.
*
* When the Maximum statistic has a value of 0 (zero), no records were being throttled for the stream during the
* specified time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
metricWriteProvisionedThroughputExceeded(props?: cloudwatch.MetricOptions): cloudwatch.Metric;
}
/**
* A reference to a stream. The easiest way to instantiate is to call
* `stream.export()`. Then, the consumer can use `Stream.import(this, ref)` and
* get a `Stream`.
*/
export interface StreamAttributes {
/**
* The ARN of the stream.
*/
readonly streamArn: string;
/**
* The KMS key securing the contents of the stream if encryption is enabled.
*
* @default - No encryption
*/
readonly encryptionKey?: kms.IKey;
}
/**
* Represents a Kinesis Stream.
*/
abstract class StreamBase extends Resource implements IStream {
/**
* The ARN of the stream.
*/
public abstract readonly streamArn: string;
/**
* The name of the stream
*/
public abstract readonly streamName: string;
/**
* Optional KMS encryption key associated with this stream.
*/
public abstract readonly encryptionKey?: kms.IKey;
/**
* Grant read permissions for this stream and its contents to an IAM
* principal (Role/Group/User).
*
* If an encryption key is used, permission to ues the key to decrypt the
* contents of the stream will also be granted.
*/
public grantRead(grantee: iam.IGrantable) {
const ret = this.grant(grantee, ...READ_OPERATIONS);
if (this.encryptionKey) {
this.encryptionKey.grantDecrypt(grantee);
}
return ret;
}
/**
* Grant write permissions for this stream and its contents to an IAM
* principal (Role/Group/User).
*
* If an encryption key is used, permission to ues the key to encrypt the
* contents of the stream will also be granted.
*/
public grantWrite(grantee: iam.IGrantable) {
const ret = this.grant(grantee, ...WRITE_OPERATIONS);
this.encryptionKey?.grantEncrypt(grantee);
return ret;
}
/**
* Grants read/write permissions for this stream and its contents to an IAM
* principal (Role/Group/User).
*
* If an encryption key is used, permission to use the key for
* encrypt/decrypt will also be granted.
*/
public grantReadWrite(grantee: iam.IGrantable) {
const ret = this.grant(grantee, ...Array.from(new Set([...READ_OPERATIONS, ...WRITE_OPERATIONS])));
this.encryptionKey?.grantEncryptDecrypt(grantee);
return ret;
}
/**
* Grant the indicated permissions on this stream to the given IAM principal (Role/Group/User).
*/
public grant(grantee: iam.IGrantable, ...actions: string[]) {
return iam.Grant.addToPrincipal({
grantee,
actions,
resourceArns: [this.streamArn],
scope: this,
});
}
/**
* Return stream metric based from its metric name
*
* @param metricName name of the stream metric
* @param props properties of the metric
*/
public metric(metricName: string, props?: cloudwatch.MetricOptions) {
return new cloudwatch.Metric({
namespace: 'AWS/Kinesis',
metricName,
dimensionsMap: {
StreamName: this.streamName,
},
...props,
}).attachTo(this);
}
/**
* The number of bytes retrieved from the Kinesis stream, measured over the specified time period. Minimum, Maximum,
* and Average statistics represent the bytes in a single GetRecords operation for the stream in the specified time
* period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
public metricGetRecordsBytes(props?: cloudwatch.MetricOptions) {
return this.metricFromCannedFunction(KinesisMetrics.getRecordsBytesAverage, props);
}
/**
* The age of the last record in all GetRecords calls made against a Kinesis stream, measured over the specified time
* period. Age is the difference between the current time and when the last record of the GetRecords call was written
* to the stream. The Minimum and Maximum statistics can be used to track the progress of Kinesis consumer
* applications. A value of zero indicates that the records being read are completely caught up with the stream.
*
* The metric defaults to maximum over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
public metricGetRecordsIteratorAgeMilliseconds(props?: cloudwatch.MetricOptions) {
return this.metricFromCannedFunction(KinesisMetrics.getRecordsIteratorAgeMillisecondsMaximum, props);
}
/**
* The number of successful GetRecords operations per stream, measured over the specified time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
public metricGetRecordsSuccess(props?: cloudwatch.MetricOptions) {
return this.metricFromCannedFunction(KinesisMetrics.getRecordsSuccessAverage, props);
}
/**
* The number of records retrieved from the shard, measured over the specified time period. Minimum, Maximum, and
* Average statistics represent the records in a single GetRecords operation for the stream in the specified time
* period.
*
* average
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
public metricGetRecords(props?: cloudwatch.MetricOptions) {
return this.metricFromCannedFunction(KinesisMetrics.getRecordsRecordsAverage, props);
}
/**
* The number of successful GetRecords operations per stream, measured over the specified time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
public metricGetRecordsLatency(props?: cloudwatch.MetricOptions) {
return this.metricFromCannedFunction(KinesisMetrics.getRecordsLatencyAverage, props);
}
/**
* The number of bytes put to the Kinesis stream using the PutRecord operation over the specified time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
public metricPutRecordBytes(props?: cloudwatch.MetricOptions) {
return this.metricFromCannedFunction(KinesisMetrics.putRecordBytesAverage, props);
}
/**
* The time taken per PutRecord operation, measured over the specified time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
metricPutRecordLatency(props?: cloudwatch.MetricOptions) {
return this.metricFromCannedFunction(KinesisMetrics.putRecordLatencyAverage, props);
}
/**
* The number of successful PutRecord operations per Kinesis stream, measured over the specified time period. Average
* reflects the percentage of successful writes to a stream.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
public metricPutRecordSuccess(props?: cloudwatch.MetricOptions) {
return this.metricFromCannedFunction(KinesisMetrics.putRecordSuccessAverage, props);
}
/**
* The number of bytes put to the Kinesis stream using the PutRecords operation over the specified time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
public metricPutRecordsBytes(props?: cloudwatch.MetricOptions) {
return this.metricFromCannedFunction(KinesisMetrics.putRecordsBytesAverage, props);
}
/**
* The time taken per PutRecords operation, measured over the specified time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
public metricPutRecordsLatency(props?: cloudwatch.MetricOptions) {
return this.metricFromCannedFunction(KinesisMetrics.putRecordsLatencyAverage, props);
}
/**
* The number of PutRecords operations where at least one record succeeded, per Kinesis stream, measured over the
* specified time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
public metricPutRecordsSuccess(props?: cloudwatch.MetricOptions) {
return this.metricFromCannedFunction(KinesisMetrics.putRecordsSuccessAverage, props);
}
/**
* The total number of records sent in a PutRecords operation per Kinesis data stream, measured over the specified
* time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
public metricPutRecordsTotalRecords(props?: cloudwatch.MetricOptions) {
return this.metricFromCannedFunction(KinesisMetrics.putRecordsTotalRecordsAverage, props);
}
/**
* The number of successful records in a PutRecords operation per Kinesis data stream, measured over the specified
* time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
public metricPutRecordsSuccessfulRecords(props?: cloudwatch.MetricOptions) {
return this.metricFromCannedFunction(KinesisMetrics.putRecordsSuccessfulRecordsAverage, props);
}
/**
* The number of records rejected due to internal failures in a PutRecords operation per Kinesis data stream,
* measured over the specified time period. Occasional internal failures are to be expected and should be retried.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
public metricPutRecordsFailedRecords(props?: cloudwatch.MetricOptions) {
return this.metricFromCannedFunction(KinesisMetrics.putRecordsFailedRecordsAverage, props);
}
/**
* The number of records rejected due to throttling in a PutRecords operation per Kinesis data stream, measured over
* the specified time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
public metricPutRecordsThrottledRecords(props?: cloudwatch.MetricOptions) {
return this.metricFromCannedFunction(KinesisMetrics.putRecordsThrottledRecordsAverage, props);
}
/**
* The number of bytes successfully put to the Kinesis stream over the specified time period. This metric includes
* bytes from PutRecord and PutRecords operations. Minimum, Maximum, and Average statistics represent the bytes in a
* single put operation for the stream in the specified time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
public metricIncomingBytes(props?: cloudwatch.MetricOptions) {
return this.metricFromCannedFunction(KinesisMetrics.incomingBytesAverage, props);
}
/**
* The number of records successfully put to the Kinesis stream over the specified time period. This metric includes
* record counts from PutRecord and PutRecords operations. Minimum, Maximum, and Average statistics represent the
* records in a single put operation for the stream in the specified time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
public metricIncomingRecords(props?: cloudwatch.MetricOptions) {
return this.metricFromCannedFunction(KinesisMetrics.incomingRecordsAverage, props);
}
/**
* The number of GetRecords calls throttled for the stream over the specified time period. The most commonly used
* statistic for this metric is Average.
*
* When the Minimum statistic has a value of 1, all records were throttled for the stream during the specified time
* period.
*
* When the Maximum statistic has a value of 0 (zero), no records were throttled for the stream during the specified
* time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties
*
* @param props properties of the metric
*
*/
public metricReadProvisionedThroughputExceeded(props?: cloudwatch.MetricOptions) {
return this.metricFromCannedFunction(KinesisMetrics.readProvisionedThroughputExceededAverage, props);
}
/**
* The number of records rejected due to throttling for the stream over the specified time period. This metric
* includes throttling from PutRecord and PutRecords operations.
*
* When the Minimum statistic has a non-zero value, records were being throttled for the stream during the specified
* time period.
*
* When the Maximum statistic has a value of 0 (zero), no records were being throttled for the stream during the
* specified time period.
*
* The metric defaults to average over 5 minutes, it can be changed by passing `statistic` and `period` properties.
*
* @param props properties of the metric
*/
public metricWriteProvisionedThroughputExceeded(props?: cloudwatch.MetricOptions) {
return this.metricFromCannedFunction(KinesisMetrics.writeProvisionedThroughputExceededAverage, props);
}
// create metrics based on generated KinesisMetrics static methods
private metricFromCannedFunction(
createCannedProps: (dimensions: { StreamName: string }) => cloudwatch.MetricProps,
props?: cloudwatch.MetricOptions): cloudwatch.Metric {
return new cloudwatch.Metric({
...createCannedProps({ StreamName: this.streamName }),
...props,
}).attachTo(this);
}
}
/**
* Properties for a Kinesis Stream
*/
export interface StreamProps {
/**
* Enforces a particular physical stream name.
* @default <generated>
*/
readonly streamName?: string;
/**
* The number of hours for the data records that are stored in shards to remain accessible.
* @default Duration.hours(24)
*/
readonly retentionPeriod?: Duration;
/**
* The number of shards for the stream.
*
* Can only be provided if streamMode is Provisioned.
*
* @default 1
*/
readonly shardCount?: number;
/**
* The kind of server-side encryption to apply to this stream.
*
* If you choose KMS, you can specify a KMS key via `encryptionKey`. If
* encryption key is not specified, a key will automatically be created.
*
* @default - StreamEncryption.KMS if encrypted Streams are supported in the region
* or StreamEncryption.UNENCRYPTED otherwise.
* StreamEncryption.KMS if an encryption key is supplied through the encryptionKey property
*/
readonly encryption?: StreamEncryption;
/**
* External KMS key to use for stream encryption.
*
* The 'encryption' property must be set to "Kms".
*
* @default - Kinesis Data Streams master key ('/alias/aws/kinesis').
* If encryption is set to StreamEncryption.KMS and this property is undefined, a new KMS key
* will be created and associated with this stream.
*/
readonly encryptionKey?: kms.IKey;
/**
* The capacity mode of this stream.
*
* @default StreamMode.PROVISIONED
*/
readonly streamMode?: StreamMode;
/**
* Policy to apply when the stream is removed from the stack.
*
* @default RemovalPolicy.RETAIN
*/
readonly removalPolicy?: RemovalPolicy;
}
/**
* A Kinesis stream. Can be encrypted with a KMS key.
*/
export class Stream extends StreamBase {
/**
* Import an existing Kinesis Stream provided an ARN
*
* @param scope The parent creating construct (usually `this`).
* @param id The construct's name
* @param streamArn Stream ARN (i.e. arn:aws:kinesis:<region>:<account-id>:stream/Foo)
*/
public static fromStreamArn(scope: Construct, id: string, streamArn: string): IStream {
return Stream.fromStreamAttributes(scope, id, { streamArn });
}
/**
* Creates a Stream construct that represents an external stream.
*
* @param scope The parent creating construct (usually `this`).
* @param id The construct's name.
* @param attrs Stream import properties
*/
public static fromStreamAttributes(scope: Construct, id: string, attrs: StreamAttributes): IStream {
class Import extends StreamBase {
public readonly streamArn = attrs.streamArn;
public readonly streamName = Stack.of(scope).splitArn(attrs.streamArn, ArnFormat.SLASH_RESOURCE_NAME).resourceName!;
public readonly encryptionKey = attrs.encryptionKey;
}
return new Import(scope, id, {
environmentFromArn: attrs.streamArn,
});
}
public readonly streamArn: string;
public readonly streamName: string;
public readonly encryptionKey?: kms.IKey;
private readonly stream: CfnStream;
constructor(scope: Construct, id: string, props: StreamProps = {}) {
super(scope, id, {
physicalName: props.streamName,
});
let shardCount = props.shardCount;
const streamMode = props.streamMode;
if (streamMode === StreamMode.ON_DEMAND && shardCount !== undefined) {
throw new Error(`streamMode must be set to ${StreamMode.PROVISIONED} (default) when specifying shardCount`);
}
if ( (streamMode === StreamMode.PROVISIONED || streamMode === undefined) && shardCount === undefined) {
shardCount = 1;
}
const retentionPeriodHours = props.retentionPeriod?.toHours() ?? 24;
if (!Token.isUnresolved(retentionPeriodHours)) {
if (retentionPeriodHours < 24 || retentionPeriodHours > 8760) {
throw new Error(`retentionPeriod must be between 24 and 8760 hours. Received ${retentionPeriodHours}`);
}
}
const { streamEncryption, encryptionKey } = this.parseEncryption(props);
this.stream = new CfnStream(this, 'Resource', {
name: this.physicalName,
retentionPeriodHours,
shardCount,
streamEncryption,
...(props.streamMode !== undefined
? {
streamModeDetails: { streamMode: props.streamMode },
}
: undefined),
});
this.stream.applyRemovalPolicy(props.removalPolicy);
this.streamArn = this.getResourceArnAttribute(this.stream.attrArn, {
service: 'kinesis',
resource: 'stream',
resourceName: this.physicalName,
});
this.streamName = this.getResourceNameAttribute(this.stream.ref);
this.encryptionKey = encryptionKey;
}
/**
* Set up key properties and return the Stream encryption property from the
* user's configuration.
*/
private parseEncryption(props: StreamProps): {
streamEncryption?: CfnStream.StreamEncryptionProperty | IResolvable;
encryptionKey?: kms.IKey;
} {
// if encryption properties are not set, default to KMS in regions where KMS is available
if (!props.encryption && !props.encryptionKey) {
const conditionName = 'AwsCdkKinesisEncryptedStreamsUnsupportedRegions';
const existing = Stack.of(this).node.tryFindChild(conditionName);
// create a single condition for the Stack
if (!existing) {
new CfnCondition(Stack.of(this), conditionName, {
expression: Fn.conditionOr(
Fn.conditionEquals(Aws.REGION, 'cn-north-1'),
Fn.conditionEquals(Aws.REGION, 'cn-northwest-1'),
),
});
}
return {
streamEncryption: Fn.conditionIf(conditionName,
Aws.NO_VALUE,
{ EncryptionType: 'KMS', KeyId: 'alias/aws/kinesis' }),
};
}
// default based on whether encryption key is specified
const encryptionType = props.encryption ??
(props.encryptionKey ? StreamEncryption.KMS : StreamEncryption.UNENCRYPTED);
// if encryption key is set, encryption must be set to KMS.
if (encryptionType !== StreamEncryption.KMS && props.encryptionKey) {
throw new Error(`encryptionKey is specified, so 'encryption' must be set to KMS (value: ${encryptionType})`);
}
if (encryptionType === StreamEncryption.UNENCRYPTED) {
return { };
}
if (encryptionType === StreamEncryption.MANAGED) {
const encryption = { encryptionType: 'KMS', keyId: 'alias/aws/kinesis' };
return { streamEncryption: encryption };
}
if (encryptionType === StreamEncryption.KMS) {
const encryptionKey = props.encryptionKey || new kms.Key(this, 'Key', {
description: `Created by ${this.node.path}`,
});
const streamEncryption: CfnStream.StreamEncryptionProperty = {
encryptionType: 'KMS',
keyId: encryptionKey.keyArn,
};
return { encryptionKey, streamEncryption };
}
throw new Error(`Unexpected 'encryptionType': ${encryptionType}`);
}
}
/**
* What kind of server-side encryption to apply to this stream
*/
export enum StreamEncryption {
/**
* Records in the stream are not encrypted.
*/
UNENCRYPTED = 'NONE',
/**
* Server-side encryption with a KMS key managed by the user.
* If `encryptionKey` is specified, this key will be used, otherwise, one will be defined.
*/
KMS = 'KMS',
/**
* Server-side encryption with a master key managed by Amazon Kinesis
*/
MANAGED = 'MANAGED',
}
/**
* Specifies the capacity mode to apply to this stream.
*/
export enum StreamMode {
/**
* Specify the provisioned capacity mode. The stream will have `shardCount` shards unless
* modified and will be billed according to the provisioned capacity.
*/
PROVISIONED = 'PROVISIONED',
/**
* Specify the on-demand capacity mode. The stream will autoscale and be billed according to the
* volume of data ingested and retrieved.
*/
ON_DEMAND = 'ON_DEMAND',
}