Experimental
@aws-cdk/aws-iot-actions-alpha is an experimental (alpha) package: its API may change in any release, without a major version bump.
LambdaFunctionAction Class
The action to invoke an AWS Lambda function, passing in an MQTT message.
Example
ts
const func = new lambda.Function(this, 'MyFunction', {
runtime: lambda.Runtime.NODEJS_LATEST,
handler: 'index.handler',
code: lambda.Code.fromInline(`
exports.handler = (event) => {
console.log("It is test for lambda action of AWS IoT Rule.", event);
};`
),
});
new iot.TopicRule(this, 'TopicRule', {
sql: iot.IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id, timestamp() as timestamp, temperature FROM 'device/+/data'"),
actions: [new actions.LambdaFunctionAction(func)],
});
python
func = lambda_.Function(self, "MyFunction",
runtime=lambda_.Runtime.NODEJS_LATEST,
handler="index.handler",
code=lambda_.Code.from_inline("""
exports.handler = (event) => {
console.log("It is test for lambda action of AWS IoT Rule.", event);
};""")
)
iot.TopicRule(self, "TopicRule",
sql=iot.IotSql.from_string_as_ver20160323("SELECT topic(2) as device_id, timestamp() as timestamp, temperature FROM 'device/+/data'"),
actions=[actions.LambdaFunctionAction(func)]
)
java
Function func = Function.Builder.create(this, "MyFunction")
.runtime(Runtime.NODEJS_LATEST)
.handler("index.handler")
.code(Code.fromInline("\n exports.handler = (event) => {\n console.log(\"It is test for lambda action of AWS IoT Rule.\", event);\n };"))
.build();
TopicRule.Builder.create(this, "TopicRule")
.sql(IotSql.fromStringAsVer20160323("SELECT topic(2) as device_id, timestamp() as timestamp, temperature FROM 'device/+/data'"))
.actions(List.of(new LambdaFunctionAction(func)))
.build();
csharp
// Install: dotnet add package Amazon.CDK.AWS.IoT.Actions.Alpha
var func = new Function(this, "MyFunction", new FunctionProps {
Runtime = Runtime.NODEJS_LATEST,
Handler = "index.handler",
Code = Code.FromInline(@"
exports.handler = (event) => {
console.log(""It is test for lambda action of AWS IoT Rule."", event);
};")
});
new TopicRule(this, "TopicRule", new TopicRuleProps {
Sql = IotSql.FromStringAsVer20160323("SELECT topic(2) as device_id, timestamp() as timestamp, temperature FROM 'device/+/data'"),
Actions = new [] { new LambdaFunctionAction(func) }
});
go
// Install: go get github.com/aws/aws-cdk-go/awscdkiotactionsalpha/v2
func := lambda.NewFunction(this, jsii.String("MyFunction"), &FunctionProps{
Runtime: lambda.Runtime_NODEJS_LATEST(),
Handler: jsii.String("index.handler"),
Code: lambda.Code_FromInline(jsii.String(`
exports.handler = (event) => {
console.log("It is test for lambda action of AWS IoT Rule.", event);
};`)),
})
iot.NewTopicRule(this, jsii.String("TopicRule"), &TopicRuleProps{
Sql: iot.IotSql_FromStringAsVer20160323(jsii.String("SELECT topic(2) as device_id, timestamp() as timestamp, temperature FROM 'device/+/data'")),
Actions: []IAction{
actions.NewLambdaFunctionAction(func),
},
})