AWS CDK AWS IoT Actions Alpha
The `aws-cdk-aws-iot-actions-alpha` library provides integration classes for defining receipt rule actions for AWS IoT Core topic rules. Being an 'alpha' package, its APIs are experimental and under active development, meaning they are subject to non-backward compatible changes or removal in future versions without adhering to semantic versioning. It allows connecting IoT messages to various AWS services like Lambda, S3, SQS, SNS, Kinesis, CloudWatch, and more. It is part of the AWS Cloud Development Kit (CDK) v2 ecosystem and is released frequently alongside other CDK modules.
Common errors
-
ForbiddenException: UnknownError occurs when using awsApiCall('IotData', 'publish', ...)cause This error typically indicates an incorrect IAM policy prefix used in the underlying AWS SDK call. For `IotData` operations like 'publish', the policy action prefix should be `iot`, not `iot-data`.fixReview the generated IAM policies for your IoT actions. If manually crafting policies or using custom resources, ensure the action is specified as `iot:Publish` (or similar `iot:` prefix for `IotData` operations) instead of `iot-data:Publish`. -
ClientError: An error occurred (AccessDeniedException) when calling the ... operation: User: arn:aws:sts::... is not authorized to perform: ...
cause The IAM role associated with the AWS IoT Topic Rule does not have the necessary permissions to execute the configured action against the target AWS service.fixAdd the required permissions to the IAM role that the IoT Topic Rule uses. For instance, for an S3PutObjectAction, ensure `s3:PutObject` is allowed on the target bucket. For LambdaFunctionAction, ensure `lambda:InvokeFunction` is allowed on the target Lambda function. -
We couldn't evaluate your expression for the action. Make sure that the variable names, input names, and paths to the data refer to the existing variables and input values.
cause This error occurs when an IoT rule action's expression (e.g., for `IoTEventsPutMessageAction` or `DynamoDBv2PutItemAction`) attempts to use a variable or path that doesn't exist in the incoming MQTT message payload or is syntactically incorrect.fixVerify the SQL query in your `TopicRule` and the expressions used in your action's properties. Ensure that `FROM` clause and `SELECT` statements correctly extract and name the fields you intend to use in the action payload.
Warnings
- breaking This package (`aws-cdk-aws-iot-actions-alpha`) is an 'alpha' module. Its APIs are experimental and under active development, meaning they are subject to non-backward compatible changes or removal in any future version, without adhering to semantic versioning.
- gotcha Incorrect IAM permissions are a common cause of deployment or runtime failures with AWS IoT actions. The IoT Topic Rule must have the necessary permissions to perform the action (e.g., `s3:PutObject` for S3 actions, `lambda:InvokeFunction` for Lambda actions).
- gotcha When configuring actions that involve expression evaluation (e.g., for IoT Events payloads or specific action properties), errors can occur due to incorrect variable names, input names, paths to data, or payload size limits (e.g., 1KB for IoT Events).
Install
-
pip install aws-cdk-aws-iot-actions-alpha
Imports
- IotRepublishMqttAction
import aws_cdk.aws_iot_actions_alpha as actions
- LambdaFunctionAction
from aws_cdk.aws_iot_actions_alpha import LambdaFunctionAction
- S3PutObjectAction
from aws_cdk.aws_iot_actions_alpha import S3PutObjectAction
- TopicRule
from aws_cdk.aws_iot_alpha import TopicRule
from aws_cdk.aws_iot import TopicRule
Quickstart
from aws_cdk import App, Stack, Duration
from aws_cdk.aws_s3 import Bucket
from aws_cdk.aws_iot import TopicRule, IotSql
from aws_cdk.aws_iot_actions_alpha import S3PutObjectAction
class MyIotStack(Stack):
def __init__(self, scope: App, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# Create an S3 bucket to store IoT data
bucket = Bucket(self, "MyIoTDataBucket")
# Define an IoT Topic Rule with an S3 action
# This rule will trigger when a message is published to 'device/+/data'
# and put the message into the S3 bucket.
topic_rule = TopicRule(
self, "MyS3IotRule",
sql=IotSql.from_string_as_ver20160323(
"SELECT topic(2) as device_id, timestamp() as timestamp, * FROM 'device/+/data'"
),
actions=[
S3PutObjectAction(bucket)
]
)
app = App()
MyIotStack(app, "MyIotS3IntegrationStack")
app.synth()