0
votes

I have a AWS Lambda function (triggered by an API call via API Gateway) that publishes messages to a collection of related IoT devices using an IoT topic based on the parameters received (the topic being a serial number that uniquely identifies a specific IoT device). I do this using boto3's IoT data-plane client method publish(). Is there a way for me to have this same Lambda function subscribe to the same IoT topic so that it is able to receive the response from the IoT device?

I am aware that the prescribed way for an IoT message to trigger a Lambda is to create an IoT Rule Action that calls a Lambda, however this doesn't really work for me (as far as I can tell) because what I want is for the original Lambda (the one triggered by the external API call to API Gateway) to listen for the IoT device's response and send back a HTTP response to the external caller based on that; this doesn't seem achievable if it's a separate Lambda that gets triggered when the IoT device responds on its IoT topic. Any ideas on how to do this?

2

2 Answers

1
votes

Why does it have to be the original lambda? I'm guessing there's some state you want to save; in which case you should either persist it or pass it in the messages.

0
votes

Though not pretty, this enables me to do what I posted in my question: I added an IoT rule which flicks the IoT device's response on to a SQS queue; the Lambda (that was invoked by API Gateway to handle the request at the start) polls that SQS queue for a message which it then consumes, using it to construct and send a response back via API Gateway to the external client that sent the HTTP POST request. To be sure, this is not a great way to use AWS Lambdas, but it seems the only way available to use Lambda + API Gateway to hook up HTTP request/response with IoT publish/subscribe.