20
votes

I would like to have an easy command like I use in the bash to publish something to a topic on MQTT inside a AWS Lambda function. Along the lines of: mosquitto_pub -h my.server.com -t "light/set" -m "on"

Background: I would like to turn a lamp on and off with Alexa. Alexa can start a Lambda function, and inside of this Lambda function I would like to start an MQTT publish, because the lamp can listen to a MQTT topic and react on the messages there.(Maybe there are easier solutions, but we are in a complicated (university) network which makes many other approaches more difficult)

6
Please accept one of the answers below so that others can know which one you find useful.dpurrington

6 Answers

37
votes

If you are using Python, I was able to get an AWS Lambda function to publish a message to AWS IoT using the following inside my handler function:

import boto3
import json

client = boto3.client('iot-data', region_name='us-east-1')

# Change topic, qos and payload
response = client.publish(
        topic='$aws/things/pi/shadow/update',
        qos=1,
        payload=json.dumps({"foo":"bar"})
    )

You will also need to ensure that the Role (in your Lambda function configuration) has a policy attached to allow access to IoT publish function. Under IAM -> Roles you can add an inline policy to your Lambda function Role like:

{
   "Version": "2016-6-25",
   "Statement": [
    {
        "Effect": "Allow",
        "Action": [
            "iot:Publish"
        ],
        "Resource": [
            "*"
        ]
    }
   ]
}
8
votes

The AWS SDK has two classes to work with IoT: Iot and IotData. IotData.publish is the method you are looking for. It looks like the Iot object is for working with things and IotData is for working with MQTT and shadows. This ought to be directly referenced in the documentation on MQTT and shadows, but it isn't.

This service (IotData) is also available in the CLI.

7
votes

If you're using Node.js, this will work -

var AWS = require('aws-sdk');
var iotdata = new AWS.IotData({ endpoint: '*****************.iot.us-east-1.amazonaws.com' });

exports.handler = async(event) => {
    console.log("Event => " + JSON.stringify(event));
    var params = {
        topic: "MyTopic",
        payload: JSON.stringify(event),
        qos: 0
    };

    return iotdata.publish(params, function(err, data) {
        if (err) {
            console.log("ERROR => " + JSON.stringify(err));
        }
        else {
            console.log("Success");
        }
    }).promise();
};

Remember to add iot:publish permission to the role used by this lambda function.

1
votes

The previous post about nodeJS send the message 2 times for me. Correction is here

    var mqttParams = {
        topic: topicName,
        payload: JSON.stringify(event),
        qos: 1
    };
    
    const request = iotdata.publish(mqttParams);
    request
        .on('success', () => console.log("Success"))
        .on('error', () => console.log("Error"))
    return new Promise(() => request.send());
0
votes

If you use Node.js, you need to install the mqtt library. The following steps help you download and install mqtt library on AWS Lambda.

  1. Download and install Node.js and npm on your PC.

  2. Download MQTT library for node.js.

  3. Unzip it at the nodejs directory that Node.js was installed. (In Windows 10 x64, nodejs directory is C:\Program Files\nodejs)

  4. Create a folder to store the mqtt installed files. For example, D:\lambda_function.

  5. Run Command Prompt as administrator, change directory to nodejs directory.

  6. Install mqtt library to D:\lambda_function.

    C:\Program Files\nodejs>npm install --prefix "D:\lambda_function” mqtt 
    

Here's a similar project.

0
votes

Here is a simple JavaScript code using async await:

const AWS = require("aws-sdk");

exports.handler = async event => {
  try {
    const iotData = new AWS.IotData({ endpoint: "IOT_SERVICE_ID-ats.iot.eu-central-1.amazonaws.com" });

    const params = {
      topic: 'some/topic',
      payload: JSON.stringify({ var1: "val1" })
    }

    result = await iotData.publish(params).promise();

    console.log(result);

    return { statusCode: 200, body: `IoT message published.` };
  } catch (e) {
    console.error(e);
    return { statusCode: 500, body: `IoT message could not be published.` };
  }
};

Don't forget to give this lambda the required iot:publish permission to publish to this IoT topic.