How would I publish a message to an Amplify client via a Lambda function creating an event in a IoT pub-sub topic? Does the Lambda func need to use Amplify or can you publish messages using the aws-sdk?
Create the AWS IoT client using the aws-sdk
//import and prepare iot for pubsub
const IotData = require("aws-sdk/clients/iotdata");
let iotData;
export default function getIotData() {
if (iotData) {
return iotData;
} else {
iotData = new IotData({
endpoint: process.env["AWS_IOT_ENDPOINT"],
region: process.env["AWS_IOT_REGION"]
});
return iotData;
}
}
And then publish like so:
import getIotData from "./getIotData";
export default async function publishDataObjToChannel(channelName, payload) {
const iotData = getIotData();
const topic = channelName;
const iotParams = {
payload: JSON.stringify(payload),
topic,
qos: 0
};
return await iotData.publish(iotParams).promise();
}
Thanks I'll give this a try. This should be better documented as I wasn't clear at all how to get started from the aws-sdk docs.
Most helpful comment
Thanks I'll give this a try. This should be better documented as I wasn't clear at all how to get started from the aws-sdk docs.