Possible to subscribe to aws IOT topic in lambda?


Newbie question. Can’t find a previous answer.

I want to build a simple pump controller with Alexa. Would like Alexa to report pump state.

Simplest approach is Alexa -> lambda -> publish_to_Iot. And then, or perhaps just before the publish, subscribe to another topic to which the local controller would publish pump state that would be passed back thru Alexa.

As near as I can tell its not possible to subscribe to a topic from Lambda… which actually makes sense in the context of a lambda function.

Specific question is, can a lambda function subscribe to an IoT topic?

Yes, I know about IoT shadows, was hoping to avoid some complexity.


It’s actually possible to do this, it’s just not terribly intuitive. The AWS-SDK doesn’t have a subscribe method so you need to use aws-iot-device-sdk. This library typically needs a certificate and lot of other config information.

but it doesn’t make sense to use a client certificate in a lambda. The lambda is already running under an IAM user so you should just be able to leverage that right? It turns out you can but it took a little digging. The aws-iot-device-sdk.js library will read the credentials out of the environment variables in Lambda (process.env.AWS_ACCESS_KEY_ID, process.env.AWS_SECRET_ACCESS_KEY). But you HAVE to use the wss protocol.

One of the pitfalls of this approach is that there is natural latency in Lambda and of course additional latency to establish a connection to the topic. This really slows things down. A nice pattern to follow is to have lambda listen on a topic that is specific to that Lambda instance (e.g. lambda/some-uuid/response) and then when you post a message to your device, you can ask it to respond on that topic. The benefit is that the topic lives as long as the Lambda function is up and running. That could be hours if there is a lot of traffic or if you keep it warm. With this model, there is no latency to establish the connection and subscribe to the topic. In my tests, this is extremely fast with low latency.

This is how I handled the subscription.

Leave a Reply