Trigger a lambda function based on thing type

0

How do you i trigger a lambda function based on a thing type? For example add a rule whenever a device shadow was updated. My use case is, having two cloudformation stacks for dev and staging.
All lambda functions are tagged specifically per stack. (Eg shadow-update-dev, shadow-update-staging). I'm thinking to add a attribute or type for a thing, and create an IOT rule to trigger what tag function it should trigger.

Should i just create a placeholder lambda that checks the thing type and trigger whats the corresponding lambda function? Is a step function a good match for this?

asked 5 years ago413 views
6 Answers
0

Hi janpoltan,

Today, the rules engine cannot fetch thing type, thing group, or registry attributes for a thing when processing a message. You could write a Lambda function to fetch these kinds of metadata and evaluate that at runtime with the aws_lambda() function: https://docs.aws.amazon.com/iot/latest/developerguide/iot-sql-functions.html#iot-func-aws-lambda

The other way to achieve that is as you describe, with a Step Functions solution.

A third way to achieve this, if it makes sense for your solution, is to store the thing type in the device shadow. The shadow can be referenced by the rules engine at runtime today. The main concern is whether this leaks inappropriate control plane info to your data plane, or whether there is risk of your device or other actors accidentally clearing this type from the shadow.

Ryan

AWS
Ryan_B
answered 5 years ago
0

Ive tried using the aws_lambda

https://docs.aws.amazon.com/iot/latest/developerguide/iot-sql-functions.html#iot-func-aws-lambda

It looks like the sql dont support passing topic vars?

SELECT topic(3) as id, aws_lambda("arn:aws:lambda:eu-west-1:xxxx:function:xxxx", {"payload": id}) as deviceEnv FROM 'device/test'

or

SELECT aws_lambda("arn:aws:lambda:eu-west-1:xxxx:function:xxxx", {"payload": topic(3)}) as deviceEnv FROM 'device/test'
answered 5 years ago
0

I'm a little surprised the second approach didn't work. If the topic() function is not evaluated first before sending the value to Lambda, you can write this in a nested way to work around:

SELECT aws_lambda("arn:aws:lambda:eu-west-1:xxxx:function:xxxx", {"payload": topic}) as deviceEnv FROM (SELECT *, topic(3) as topic FROM 'device/test')

Admittedly I did not test the above SQL, but this is how I've solved past similar issues where values needed be generated and bubbled up to a secondary query.

HTH,
Ryan

AWS
Ryan_B
answered 5 years ago
0

Actually the second option worked. My mistake not updating the topic filter to

device/test/+
SELECT * FROM 'device/test/+' WHERE (SELECT aws_lambda("arn:aws:lambda:region:accountId:function:functionName", {"deviceId": topic(3)}).env as env FROM 'device/test/+') = "dev"

The where clause doesnt seem to work

Edited by: janpoltan on Apr 3, 2019 1:44 AM

answered 5 years ago
0

It seems it wont work

answered 5 years ago
0

Moved to other post

answered 5 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions