Package: aws-cdk.aws-kinesis
Amazon Kinesis Construct Library---AWS CDK v1 has reached End-of-Support on 2023-06-01.
This package is no longer being updated, and users should migrate to AWS CDK v2.For more information on how to migrate, see theMigrating to AWS CDK v2guide.Amazon Kinesisprovides collection and processing of largestreamsof data records in real time. Kinesis data streams can be used for rapid and continuous data
intake and aggregation.Table Of ContentsStreamsEncryptionImportPermission GrantsRead PermissionsWrite PermissionsCustom PermissionsMetricsStreamsAmazon Kinesis Data Streams ingests a large amount of data in real time, durably stores the data, and makes the data available for consumption.Using the CDK, a new Kinesis stream can be created as part of the stack using the construct's constructor. You may specify thestreamNameto give
your own identifier to the stream. If not, CloudFormation will generate a name.kinesis.Stream(self,"MyFirstStream",stream_name="my-awesome-stream")You can also specify properties such asshardCountto indicate how many shards the stream should choose and aretentionPeriodto specify how long the data in the shards should remain accessible.
Read more atCreating and Managing Streamskinesis.Stream(self,"MyFirstStream",stream_name="my-awesome-stream",shard_count=3,retention_period=Duration.hours(48))EncryptionStream encryptionenables
server-side encryption using an AWS KMS key for a specified stream.Encryption is enabled by default on your stream with the master key owned by Kinesis Data Streams in regions where it is supported.kinesis.Stream(self,"MyEncryptedStream")You can enable encryption on your stream with a user-managed key by specifying theencryptionproperty.
A KMS key will be created for you and associated with the stream.kinesis.Stream(self,"MyEncryptedStream",encryption=kinesis.StreamEncryption.KMS)You can also supply your own external KMS key to use for stream encryption by specifying theencryptionKeyproperty.key=kms.Key(self,"MyKey")kinesis.Stream(self,"MyEncryptedStream",encryption=kinesis.StreamEncryption.KMS,encryption_key=key)ImportAny Kinesis stream that has been created outside the stack can be imported into your CDK app.Streams can be imported by their ARN via theStream.fromStreamArn()APIimported_stream=kinesis.Stream.from_stream_arn(self,"ImportedStream","arn:aws:kinesis:us-east-2:123456789012:stream/f3j09j2230j")Encrypted Streams can also be imported by their attributes via theStream.fromStreamAttributes()APIimported_stream=kinesis.Stream.from_stream_attributes(self,"ImportedEncryptedStream",stream_arn="arn:aws:kinesis:us-east-2:123456789012:stream/f3j09j2230j",encryption_key=kms.Key.from_key_arn(self,"key","arn:aws:kms:us-east-1:123456789012:key/12345678-1234-1234-1234-123456789012"))Permission GrantsIAM roles, users or groups which need to be able to work with Amazon Kinesis streams at runtime should be granted IAM permissions.Any object that implements theIGrantableinterface (has an associated principal) can be granted permissions by calling:grantRead(principal)- grants the principal read accessgrantWrite(principal)- grants the principal write permissions to a StreamgrantReadWrite(principal)- grants principal read and write permissionsRead PermissionsGrantreadaccess to a stream by calling thegrantRead()API.
If the stream has an encryption key, read permissions will also be granted to the key.lambda_role=iam.Role(self,"Role",assumed_by=iam.ServicePrincipal("lambda.amazonaws.com"),description="Example role...")stream=kinesis.Stream(self,"MyEncryptedStream",encryption=kinesis.StreamEncryption.KMS)# give lambda permissions to read streamstream.grant_read(lambda_role)The following read permissions are provided to a service principal by thegrantRead()API:kinesis:DescribeStreamSummarykinesis:GetRecordskinesis:GetShardIteratorkinesis:ListShardskinesis:SubscribeToShardWrite PermissionsGrantwritepermissions to a stream is provided by calling thegrantWrite()API.
If the stream has an encryption key, write permissions will also be granted to the key.lambda_role=iam.Role(self,"Role",assumed_by=iam.ServicePrincipal("lambda.amazonaws.com"),description="Example role...")stream=kinesis.Stream(self,"MyEncryptedStream",encryption=kinesis.StreamEncryption.KMS)# give lambda permissions to write to streamstream.grant_write(lambda_role)The following write permissions are provided to a service principal by thegrantWrite()API:kinesis:ListShardskinesis:PutRecordkinesis:PutRecordsCustom PermissionsYou can add any set of permissions to a stream by calling thegrant()API.user=iam.User(self,"MyUser")stream=kinesis.Stream(self,"MyStream")# give my user permissions to list shardsstream.grant(user,"kinesis:ListShards")MetricsYou can use common metrics from your stream to create alarms and/or dashboards. Thestream.metric('MetricName')method creates a metric with the stream namespace and dimension. You can also use pre-define methods likestream.metricGetRecordsSuccess(). To find out more about Kinesis metrics checkMonitoring the Amazon Kinesis Data Streams Service with Amazon CloudWatch.stream=kinesis.Stream(self,"MyStream")# Using base metric method passing the metric namestream.metric("GetRecords.Success")# using pre-defined metric methodstream.metric_get_records_success()# using pre-defined and overriding the statisticstream.metric_get_records_success(statistic="Maximum")