AWS CDK Kinesis Constructs
The AWS Cloud Development Kit (CDK) Construct Library for AWS Kinesis provides high-level constructs for defining Kinesis streams using familiar programming languages. This entry specifically targets version 1.204.0, which is part of the v1 branch of the AWS CDK. The AWS CDK typically has frequent minor releases (often weekly or bi-weekly) and less frequent major releases (e.g., v1 to v2), with v2 being the currently recommended and actively developed version.
Common errors
-
ModuleNotFoundError: No module named 'aws_cdk_lib'
cause You are attempting to use AWS CDK v2 import paths (`from aws_cdk_lib import ...`) while your project is configured for or has only AWS CDK v1 packages (`aws-cdk-aws-kinesis`, `aws-cdk.core`) installed.fixIf you intend to use AWS CDK v1, ensure your imports follow the `from aws_cdk import aws_kinesis` pattern and `pip install aws-cdk.core`. If you intend to use v2, `pip install aws-cdk-lib` and update your imports accordingly. -
TypeError: 'module' object is not callable
cause This error often occurs when you try to call a module directly, rather than accessing a construct within it (e.g., `kinesis.Stream()` instead of `kinesis.Stream(...)`), or if there's a missing `import aws_cdk as cdk` or `from constructs import Construct` for v1 applications.fixEnsure all necessary imports are present and aliased correctly (`import aws_cdk as cdk`, `from aws_cdk import aws_kinesis as kinesis`, `from constructs import Construct`). Verify that you are instantiating constructs correctly, passing `self`, `id`, and props. -
Resource CFN_E_TARGET_NOT_FOUND error with message "Stream <stream-name> not found" when deploying.
cause This runtime error typically indicates that the Kinesis stream either does not exist, or the name/ARN specified in the CDK code does not match an existing stream, especially when attempting to import an existing resource. It can also happen due to region mismatch or if the stream was manually deleted after deployment.fixVerify the `stream_name` or `stream_arn` in your CDK code matches the actual resource. If importing, ensure the stream exists. If creating, ensure you're deploying to the correct AWS region. For cross-stack references, ensure the producer stack is deployed first.
Warnings
- breaking AWS CDK v1 is in maintenance mode; v2 is the current major version. Migrating from v1 to v2 involves significant changes to package structure and import paths. For v2, all construct libraries are consolidated under `aws-cdk-lib`.
- gotcha Kinesis Data Streams have a default data retention period of 24 hours. If you require longer data retention (up to 365 days), you must explicitly configure it.
- gotcha By default, Kinesis Data Streams are encrypted at rest using a Kinesis-owned AWS KMS key. If you need to use a customer-managed KMS key (CMK) for compliance or specific key management requirements, you must specify it.
Install
-
pip install aws-cdk-aws-kinesis
Imports
- Stream
from aws_cdk_lib import aws_kinesis as kinesis
from aws_cdk import aws_kinesis as kinesis
Quickstart
import aws_cdk as cdk
from aws_cdk import aws_kinesis as kinesis
from constructs import Construct
class MyKinesisStack(cdk.Stack):
def __init__(self, scope: Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# Create a Kinesis Data Stream
kinesis.Stream(self, "MyCDKStream",
stream_name="MyCDKPythonStream",
shard_count=1,
retention_period=cdk.Duration.hours(24))
app = cdk.App()
MyKinesisStack(app, "MyKinesisStack")
app.synth()