{"id":7953,"library":"aws-cdk-aws-iot-alpha","title":"AWS CDK AWS IoT Alpha Constructs","description":"The `aws-cdk-aws-iot-alpha` library provides experimental, higher-level (L2/L3) constructs for defining AWS IoT Core resources using the AWS Cloud Development Kit (CDK). As an 'alpha' module, its APIs are under active development and subject to non-backward compatible changes or removal in any future version. It allows developers to programmatically define IoT rules, logging, scheduled audits, and more, leveraging familiar programming languages. The current version is `2.250.0a0`, released as part of the AWS CDK v2 alpha series, with breaking changes announced in release notes rather than adhering to strict semantic versioning.","status":"active","version":"2.250.0a0","language":"en","source_language":"en","source_url":"https://github.com/aws/aws-cdk.git","tags":["AWS","CDK","IoT","Alpha","Infrastructure as Code","CloudFormation","IoT Core"],"install":[{"cmd":"pip install aws-cdk-aws-iot-alpha aws-cdk-aws-iot-actions-alpha aws-cdk-lib","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Core AWS CDK library for fundamental constructs and app structure.","package":"aws-cdk-lib","optional":false},{"reason":"Provides actions for IoT Topic Rules (e.g., Lambda invocation, S3 put).","package":"aws-cdk-aws-iot-actions-alpha","optional":false},{"reason":"Base class library for AWS CDK constructs.","package":"constructs","optional":false}],"imports":[{"note":"Standard import alias for the IoT alpha module.","symbol":"aws_iot_alpha","correct":"import aws_cdk.aws_iot_alpha as iot"},{"note":"Required for defining IoT Rule actions.","symbol":"aws_iot_actions_alpha","correct":"import aws_cdk.aws_iot_actions_alpha as actions"},{"note":"Standard import for AWS Lambda constructs, often used with IoT Rules.","symbol":"aws_lambda","correct":"import aws_cdk.aws_lambda as lambda_"}],"quickstart":{"code":"import os\nfrom aws_cdk import (\n    App,\n    Stack,\n    aws_lambda as lambda_,\n    aws_cdk as cdk\n)\nimport aws_cdk.aws_iot_alpha as iot\nimport aws_cdk.aws_iot_actions_alpha as actions\n\n\nclass IotRuleStack(Stack):\n    def __init__(self, scope: App, id: str, **kwargs) -> None:\n        super().__init__(scope, id, **kwargs)\n\n        # Define a Lambda function to be invoked by the IoT Rule\n        my_function = lambda_.Function(\n            self, \"MyIoTFunction\",\n            runtime=lambda_.Runtime.PYTHON_3_9,\n            handler=\"index.handler\",\n            code=lambda_.Code.from_inline(\n                \"\"\"import json\\n\\ndef handler(event, context):\\n    print(\"Received event: {}\".format(json.dumps(event)))\\n    return {'statusCode': 200, 'body': 'OK'}\"\"\"\n            )\n        )\n\n        # Create an IoT Topic Rule that invokes the Lambda function\n        # The SQL statement filters messages on 'device/+/data'\n        iot.TopicRule(\n            self, \"MyTopicRule\",\n            topic_rule_name=\"MyCdkExampleTopicRule\",\n            description=\"Invokes a Lambda function when a message is published to 'device/+/data'\",\n            sql=iot.IotSql.from_string_as_ver20160323(\"SELECT topic(2) as device_id, timestamp() as timestamp FROM 'device/+/data'\"),\n            actions=[actions.LambdaFunctionAction(my_function)]\n        )\n\n\napp = App()\nIotRuleStack(app, \"IotRuleStack\",\n             # For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html\n             env=cdk.Environment(\n                 account=os.environ.get(\"CDK_DEFAULT_ACCOUNT\"),\n                 region=os.environ.get(\"CDK_DEFAULT_REGION\")\n             )\n)\n\napp.synth()","lang":"python","description":"This quickstart defines an AWS IoT Topic Rule that listens for messages on the MQTT topic `device/+/data` and invokes an AWS Lambda function with the message payload. It demonstrates the use of `TopicRule` and `LambdaFunctionAction` from the alpha modules, showcasing how to connect IoT device messages to other AWS services. Before running, ensure AWS credentials and CDK environment variables (`CDK_DEFAULT_ACCOUNT`, `CDK_DEFAULT_REGION`) are configured."},"warnings":[{"fix":"Regularly review AWS CDK release notes when upgrading and be prepared to update your source code to align with new API patterns. Pin exact alpha versions to prevent unexpected breaking changes during CI/CD.","message":"This library (`aws-cdk-aws-iot-alpha`) contains experimental APIs that do not adhere to Semantic Versioning. Expect non-backward compatible changes, removals, or API renames in any future release without prior major version increments.","severity":"breaking","affected_versions":"2.x.x-alpha.0"},{"fix":"Understand the difference between L1, L2, and L3 constructs. Prefer L2/L3 for abstraction where available and stable. Use this alpha module with caution, balancing convenience against potential breaking changes.","message":"When migrating from older AWS IoT CDK usage or encountering issues, note that the core `aws-cdk.aws-iot` module (non-alpha) might only offer L1 (CloudFormation-like) constructs. This alpha module provides higher-level L2/L3 constructs, but their stability is experimental.","severity":"gotcha","affected_versions":"All 2.x.x versions (alpha and stable)"},{"fix":"Always install `aws-cdk-lib` and its alpha submodules with matching major/minor versions (e.g., `aws-cdk-lib==2.250.0`, `aws-cdk-aws-iot-alpha==2.250.0a0`). Use `pip freeze` to inspect installed versions and `pip install -r requirements.txt` after defining exact versions.","message":"Dependency versions between `aws-cdk-lib`, `aws-cdk-aws-iot-alpha`, and `aws-cdk-aws-iot-actions-alpha` must be compatible. Mismatches can lead to runtime errors or unexpected behavior.","severity":"gotcha","affected_versions":"All 2.x.x versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure the IAM identity used for deployment has `iot:*` permissions or more granular permissions like `iot:CreateTopicRule`, `iot:Publish`, `iot:Subscribe`, `lambda:InvokeFunction` (if integrating with Lambda) etc., relevant to the resources being deployed and interacted with.","cause":"The IAM user or role deploying the CDK stack lacks the necessary permissions to create or manage AWS IoT resources (e.g., Topic Rules, Thing Groups, Certificates).","error":"AccessDeniedException: User: arn:aws:iam::ACCOUNT_ID:user/USERNAME is not authorized to perform: iot:CreateTopicRule"},{"fix":"Review IAM policies related to IoT Data Plane. Change policy actions from `iot-data:Publish` to `iot:Publish`, `iot-data:Receive` to `iot:Receive`, etc. Also ensure the resource ARN correctly specifies the IoT topic or client.","cause":"When defining IAM policies for AWS IoT Data Plane actions (like `Publish`, `Receive`, `Subscribe`), the service prefix should be `iot`, not `iot-data`. This is a common confusion due to the `IotData` API.","error":"ForbiddenException: UnknownError when using awsApiCall('IotData', 'publish', ...)"},{"fix":"For IoT Events, consider changing the detector model's evaluation method to 'SERIAL'. Increase timer durations to account for latency. Implement retry mechanisms in Lambda functions sending events. For critical paths, consider direct IoT Rule integration to IoT Events, bypassing intermediate services like EventBridge and Lambda, if applicable.","cause":"While `aws-cdk-aws-iot-alpha` can define IoT Rules to send data to IoT Events, issues like batch evaluation, input throttling, or intermediary Lambda invocation problems can cause events to be missed by the IoT Events Detector Model.","error":"IoT Events Detector Model not receiving/logging all events, leading to premature timer expiry"}]}