0
votes

I have created an AWS IoT rule. It triggers a Lambda function when client sends a message to the terminal1/ topic (Client is sending messages to the terminal1/ using certificate files generated from AWS IoT.). I need a Lambda function to generate a new message using the client message and send to another topic (terminal2/test).

I have tried triggering "Republish messages to an AWS IoT topic". However, it sends data to a particular topic. It just forwards the same message (I need to send a different message).

I have created a Lambda function to send a message to another topic. But I could not authenticate the endpoint.

Here is the Lambda Function I have created (According to this),

'use strict';

 // Load the AWS SDK
 var AWS = require("aws-sdk");

exports.handler = (event, context, callback) => {

     var iotdata = new AWS.IotData({
     endpoint: 'akugdx70brb.iot.us-west-2.amazonaws.com:8883',
     apiVersion: '2015-05-28'
   });

         var params = {
      topic: 'terminal2/test/',
      payload: new Buffer('...') || 'STRING_VALUE',
      qos:1
    };
    iotdata.publish(params, function(err, data) {
      if (err) console.log(err, err.stack); // an error occurred
      else     console.log(data);           // successful response
    });

};

It generates this error,

2018-05-01T06:22:02.394Z f5a570ef-4d07-11e8-b8a2-6bad8f2982f7 { Error: write EPROTO 139935652661056:error:14094412:SSL routines:ssl3_read_bytes:sslv3 alert bad certificate:../deps/openssl/openssl/ssl/s3_pkt.c:1493:SSL alert number 42
139935652661056:error:1409E0E5:SSL routines:ssl3_write_bytes:ssl handshake failure:../deps/openssl/openssl/ssl/s3_pkt.c:659:

at exports._errnoException (util.js:1018:11)
at WriteWrap.afterWrite (net.js:800:14)
message: 'write EPROTO 139935652661056:error:14094412:SSL routines:ssl3_read_bytes:sslv3 alert bad certificate:../deps/openssl/openssl/ssl/s3_pkt.c:1493:SSL alert number 42\n139935652661056:error:1409E0E5:SSL routines:ssl3_write_bytes:ssl handshake failure:../deps/openssl/openssl/ssl/s3_pkt.c:659:\n',
code: 'NetworkingError',
errno: 'EPROTO',
syscall: 'write',
region: 'us-west-2',
hostname: 'akugdx70brb.iot.us-west-2.amazonaws.com',
retryable: true,
time: 2018-05-01T06:22:02.394Z } 'Error: write EPROTO 139935652661056:error:14094412:SSL routines:ssl3_read_bytes:sslv3 alert bad certificate:../deps/openssl/openssl/ssl/s3_pkt.c:1493:SSL alert number 42\n139935652661056:error:1409E0E5:SSL routines:ssl3_write_bytes:ssl handshake failure:../deps/openssl/openssl/ssl/s3_pkt.c:659:\n\n at exports._errnoException (util.js:1018:11)\n at WriteWrap.afterWrite (net.js:800:14)'

I guess that it means I could not authenticate the host, I have not used any certificates when creating the Lambda Function. How can I send an MQTT message to the same host, but to a different topic using AWS Lambda? (The above Lambda Function triggers when a client sends a message, Do I have to re-authenticate the server to send a message to a different topic?)

3

3 Answers

0
votes

try using

AWS.config.update({
    accessKeyId: <accessKeyId>,
    secretAccessKey: <secretAccessKey>,
    region: <region>
  });
0
votes

Depending on what you want to generate, you may be able to transform the input message using the topic rule itself, and skip being charged for the lambda invocation. See https://docs.aws.amazon.com/iot/latest/developerguide/iot-substitution-templates.html for an example.

0
votes

Your lambda should use a role which has access to IoT policy. Your lambda execution role doesnt have enough privileges to write or connect and publish to IoT.