1

For the past few days I've been trying to solve the problem of publishing a message from Lambda to the AWS cloud, using Greengrass v2.

The code in python was even provided in the documentation, only had to be slightly reworked.

When it comes to SDK v2 JS in documentation there is only minimal mention about publish function in AWS-CRT library.

I tried to create code using components from this library, but it looks like the library also requires a script with parameters.

This is my code that requires installation of aws-iot-sdk-v2 js.

const iotsdk = require("aws-iot-device-sdk-v2");
const mqtt = iotsdk.mqtt;
const os = require("os");
const util = require("util");

const GROUP_ID = process.env.GROUP_ID;
const THING_NAME = process.env.AWS_IOT_THING_NAME;
const THING_ARN = process.env.AWS_IOT_THING_ARN;

(topic = "gg/message"),
(payload = JSON.stringify({ message: util.format("ping") }));

function greengrassHelloWorldRun() {
   mqtt.MqttClientConnection.prototype.publish(topic, payload);
  }
  console.log(topic);
    console.log(payload);

 setInterval(greengrassHelloWorldRun, 5000);

  exports.handler = function (event, context) {
   console.log("event: " + JSON.stringify(event));
  console.log("context: " + JSON.stringify(context));
  };

I get errors about arguments and NAPI.

The same errors also appear when using this function as lambda component in greengrass logs

Maybe someone has some example how to publish some message on topic using Node lambda with sdk v2.

2 Answers2

2

ChristopherTal

I'm also using the Greengrass SDKs for JS and indeed they're still a work in progress. But I was able to send messages to the IoTCore from Greengrass using the JS SDKs. A few things to mention:

  • You seem to use the aws-iot-device-sdk-v2 SDK which is for things
  • The aws-greengrass-core-sdk npm package is made for components

It is important to differ between things and components and decide who's doing what.

To send data to IoTCore from a thing, you need indeed to use MQTT. On the deployment page on the Greengrass console, you need to revise the deployment and add following components:

This way your thing connects to the local MQTT Broker through the client device auth component and the MQTT Bridge decides how the traffic is routed. You can read all info on the links above.

I even realised this using the standard mqtt npm package. You need to create a certificate and a thing using lambda or the console and use those certificates to access the broker.

const mqtt = require('mqtt')
const fs = require('fs')

const ca = fs.readFileSync(locationOfTheCA)
const key = fs.readFileSync(locationOfThePrivateKey)
const cert = fs.readFileSync(locationOfTheCertificate)
console.log('Welcome to MQTT Connector')
const client  = mqtt.connect('mqtts://localhost:8883', {
  clientId: 'yourThingNameHere',
  ca: ca,
  key: key,
  cert: cert
})

client.on('connect', function () {
    console.log('Connected to MQTT')
    /* client.subscribe('$aws/*', function (err) {
    if (!err) {
      //client.publish('presence', 'Hello mqtt')
    }
  })*/
})

client.on('message', function (topic, message) {
  // message is Buffer
  console.log(message.toString())
  client.end()
})

Hopefully this helps you out! Warm regards

Hacor

Hans Cornelis
  • 63
  • 1
  • 7
-1

After contacting AWS Support I know it is impossible.

AWS doesn't support mqttProxy IPC for SDK V2 JS yet.

Loich
  • 813
  • 11
  • 18