0

The immediate issue I am trying to overcome is that my aws-lambda function is not connecting to my broke using the js MQTT library. I am able to use this library in a local node environment to connect, just not in the aws-lambda function.

I have created a zip file from this repo: https://github.com/JordanKlaers/AlexaMQTT

That I uploaded to my lambda function. I am using the exported function from index.js.

Everything works well except for the part where it does not connect to the broker/client (line 83 in index.js) When I run oldIndex.js from the repo I linked (which is just the promise function that connects, from the aws-lambda function) in my local node environment it connects and things run correctly.

I dont know how to create a minimum reproducible sketch because its success is based on interacting with hardware. I did create the "oldIndex.js" as a minimum sketch to show that at least the function to connect works. I have included logs of the lambda function to show that it works as expected up to the attempt to connect.

The only thing I can speculate would be some issue with my permissions for the role used with the lambda function but I have researched and added different policies to my role but that hasnt helped.

Here are the logs from the function when called (which shows that it gets to the promise and attempts to connect but doesnt succeed)

I had done almost everything myself, but got some final clarification on my approach from this tutorial so I not sure what else Im not considering/missing. logs from lambda function

Jordan Klaers
  • 149
  • 11
  • You have absolutely no error handling in your use of the MQTT libray, so you have to way to debug this. Also have you looked at the broker logs to see if it's even seeing a connection attempt? Where is the broker running? – hardillb Dec 28 '19 at 08:26
  • I thought the error handler for the promise on line 107 was enough. I can add more. I didn't know there were logs for the broker, I'll have to look into that, the broker is running off a raspberry pi zero – Jordan Klaers Dec 28 '19 at 18:36
  • I'd the broker is on a raspberry pi on your network how is the lambda running in AWS's cloud supposed to be able to reach it? – hardillb Dec 28 '19 at 19:42
  • yes the pi/broker is running off my network. I guess I was making a poor assumption/did not consider that. I have seen no mention of how to address that concern though, do you have a suggestion? should I use a different broker, one that the lambda function and my esp8266 can both interact with? – Jordan Klaers Dec 28 '19 at 19:58

1 Answers1

0

The main problem here is that your broker is running on a Pi attached to your local home network.

This means it is behind a Home Broadband Router which is performing Network Address Translation (NAT). This takes packets from your home network (10.0.0.0/24) and remaps them to have come from your public facing IP address.

This means that the Lambda code (running on AWS) can not directly send packets to the broker, so has no way to connect.

There are several possible solutions to this, but here are a couple.

  1. Run a broker on a cloud hosting provider. You will be able to then reach this from anywhere.
  2. Enable port forwarding on your router to expose port 1883 to the internet and forward any packets to the broker running on your raspberry pi. (This option depends on you having a fixed IP address or dynamic DNS)

For both of these you will probably want to enable authentication/authorisation on the broker and also probably add TLS.

You also need to look closer at the MQTT.js library and how to enable the error tracking so you can see why things fail e.g.

client.on('error', function(err) {...});
hardillb
  • 54,545
  • 11
  • 67
  • 105