MQTT Client cross-account to connect and publish to AWS IoT MQTT Broker

0

I need to use MQTT Client (ca, crt, key) from Lambda in Account A to connect to the AWS IoT core MQTT broker in Account B through mqtt publish. Here's what I've tried so far but it hasn't worked. Lambda is configured with NodeJS Node.js 20.x. Am I missing something?

const mqtt = require('mqtt');
export const handler = async (event) => {
        const mqttEndpoint = 'xxxxxxxx-ats.iot.us-east-1.amazonaws.com';
        const clientId = 'ThirdParty_xxxxxx_Cloud';
        const topic = 'xxxx/xxxx/xxxx/xxxx/xxxx';

        // Load SSL certificates and private key
        const fs = require('fs');
        const ca = fs.readFileSync('./AmazonRootCA1.pem');
        const cert = fs.readFileSync('./certificate.pem.crt');
        const key = fs.readFileSync('./private.pem.key');

        // Connect to MQTT broker
        const mqttClient = mqtt.connect(mqttEndpoint, {
            protocol: 'mqtts',
            port: 8883,
            clientId: clientId,
            ca: ca,
            cert: cert,
            key: key,
            rejectUnauthorized: true, // Set to false if your broker uses self-signed certificate
        });

        // Handle incoming messages
        mqttClient.on('message', (topic, message) => {
            console.log(`Received message on topic ${topic}: ${message.toString()}`);
            // Handle the message as needed
        });

        // Publish a message
        const message = 'Hello from Lambda!';
        mqttClient.publish(topic, message);

        // Disconnect after a certain period or when done
        setTimeout(() => {
            mqttClient.end();
        }, 5000);
};
Rowen
質問済み 3ヶ月前203ビュー
2回答
0

Hi. What's the error you get?

I'm not familiar with MQTT.js, but I think you need to specify the protocol in the endpoint string, as per the MQTT.js example here: https://aws.amazon.com/blogs/iot/use-aws-iot-core-mqtt-broker-with-standard-mqtt-libraries/

profile pictureAWS
エキスパート
Greg_B
回答済み 3ヶ月前
  • This code is not working, no error I got in the output, Its just waiting.

    https://aws.amazon.com/blogs/iot/use-aws-iot-core-mqtt-broker-with-standard-mqtt-libraries/ This example should be applicable to the device side. Is there any other way to connect to the IoT core of other AWS Accounts through "Lambda"?

    Don't know what I missed. Any suggestion will be helpful for me. Thanks you

  • Is your Lambda in a VPC? And what is the use case that makes you want to do this?

0

Hi,

You should follow the guidance of https://docs.aws.amazon.com/iot/latest/developerguide/accessing-cross-account-resources-using-rules.html

See section Cross-account setup for AWS Lambda in particular

Best,

Didier

profile pictureAWS
エキスパート
回答済み 3ヶ月前
  • This is to send MQTT messages across accounts through IoT core, but I hope to publish MQTT messages across accounts from Lambda in reverse. Is there any other way? Thank you

ログインしていません。 ログイン 回答を投稿する。

優れた回答とは、質問に明確に答え、建設的なフィードバックを提供し、質問者の専門分野におけるスキルの向上を促すものです。

質問に答えるためのガイドライン

関連するコンテンツ