AWS Lambda function as bridge between HTTP and MQTT

0

Hi folks, Hope you are well and safe and also hope that this question doesn't gonna be an already asked question.

I'm here to ask a help about a possible solution with AWS Lambda and other AWS environment stuffs: I need to understand if it is possible to use AWS Lambda function as bridge between HTTP request and MQTT topic. In particular, I need where and HTTP request trigger an AWS Lambda function, which in turn publish on a topic; when datas are published on this topic, an IoT thing response to the request, posting response on a topic. I require AWS Lambda to receive this event and send back to the first HTTP request, the response received from IoT thing.

Is it possible to achieve this kind of synchronization mechanism similar to the one shown here where, instead of a DynamoDB there is a pub/sub mechanism?

Cheers.

2 Answers
1

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.

Erik
answered 2 years 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?

1

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.

answered 2 years ago
  • 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.

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