- Newest
- Most votes
- Most comments
The trick to this would be responding back on the original HTTP connection. As David Katz point out, it is pretty easy to write a lambda function that is triggered from API Gateway to publish to an IoT Topic. Not sure your language of choice, but for example if you are using python, use boto3 library, create an iot client, then use the publish() function to publish your message.
Subscribing to a topic is more tricky and will be time consuming for a lambda function. I think you have two possibilities for a solution.
First, you would have to spin up a MQTT connection in your lambda, subscribe to the response topic, and then wait for an answer. Again, as an example, in python you can use AWSIoTPythonSDK to connect to your MQTT broker, and then subscribe to your topic. Once the message is received, then your lambda may respond back on the original HTTP connection.
Alternatively, create an IoT Rule that listens for your IoT response topic. The IoT rule sends the message to either a SQS Queue or Dynamo table. Your lambda function can then poll the queue or dynamo table for the message. Read the message and send it back on the HTTP connection.
Requests: Publishing to MQTT from lambda works no problem - and triggering the lambda from AWS API GW is obviously simple.
Responses: If you use AWS IoT, you can define a rule to trigger a lambda for the response: https://docs.aws.amazon.com/lambda/latest/dg/services-iot.html
if you are not using AWS IoT, I'd assume you would have to run a compute instance to receive incoming messages via an MQTT client and then invoke lambda yourself - perhaps with SQS or Kinesis in the middle.
Hi David, thanks for your reply.
My problem is related to "you can define a rule to trigger a lambda for the response": I've already linked a AWS IoT rule (with SQL query) to a lambda. The problem actually is to return a response to the HTTP connection that firstly start up all process. Hope that could help clarify the concept.
Relevant content
- asked a year ago
- What's the difference between Lambda function execution role permissions and invocation permissions?AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated a year ago
Hi Erik, thanks for your reply.
So, are you saying that it is possible for an AWS Lambda Function to subscribe to a topic and wait for response, keeping alive the HTTP request?
Cause I didn't find any solution that allow me to subscribe to topic on AWS Lambda function - publish on topic and wait for response of first subscription (all while a single HTTP request is kept alive).
m_piffari, Please see my updated answer. In fairness I have not tried either solution in real life. Both are doable. Personally, I would go with the alternative approach, using an IoT Rule and SQS Queue. As importing a MQTT client into a lambda seems a bit overkill.
Erik, thank for point out an alternative approach.
I've tried out the second approach: instead of using an SQS or a DynamoDB I've used an IoT Rule in order to trigger directly the AWS Lambda when something is published on a topic. However, when the IoT Rule is triggered, the initial HTTP request is gone away, and I'm not able to response to it anyway.
Maybe, do you know how can I integrate the AWSIoTPythonSDK (that you linked in original answer) in AWS Lambda? Where I can find some examples?