# NodeRED/MQTT disconnect from AWS EC2

**URL:** <https://discourse.nodered.org/t/nodered-mqtt-disconnect-from-aws-ec2/31505>\
**Category:** General\
**Created:** [12 August 2020 23:36 UTC](https://discourse.nodered.org/t/nodered-mqtt-disconnect-from-aws-ec2/31505 "2020-08-12T23:36:07Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![tfmeier](https://avatars.discourse-cdn.com/v4/letter/t/a9a28c/32.png) [@tfmeier](https://discourse.nodered.org/u/tfmeier)\
**Post date:** [12 August 2020 23:36 UTC](https://discourse.nodered.org/t/nodered-mqtt-disconnect-from-aws-ec2/31505/1 "2020-08-12T23:36:08Z")

</div>

I have successfully setup NodeRED on an AWS EC2 instance and use MQTT as a means to communicate from/to my IoT devices (ESP8266/ESP32 at the moment).

I'm running a compiled version of Tasmota that has AWS IoT enabled.

What's strange is the MQTT nodes in my flow disconnect every few days (no pattern as far as I can tell). To reconnect I edit one of the nodes (same config for all MQTT nodes), edit the MQTT broker (simply 'edit' followed by 'update' without any changes) and deploy the flow. Nodes reconnect and everything works fine.

My Internet connection shouldn't come into play here as the NodeRED instance/app runs on AWS EC2 and connects to AWS IoT Core so this should be within the AWS Cloud environment as far as I can understand. Even if EC2 and the IoT Core were on different regions it's still AWS and any latency shouldn't influence this, right?

- Does NodeRED have the ability to automatically reconnect on dropping of a connection?
- What's the easiest way to diagnose this/what log files could I use to even start investigating?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [13 August 2020 06:55 UTC](https://discourse.nodered.org/t/nodered-mqtt-disconnect-from-aws-ec2/31505/2 "2020-08-13T06:55:33Z")

</div>

Is your Node-RED instance set to run on start ? Maybe you AWS instance is rebooting/moving ?

---

<div class="post-metadata">

**Author:** ![tfmeier](https://avatars.discourse-cdn.com/v4/letter/t/a9a28c/32.png) [@tfmeier](https://discourse.nodered.org/u/tfmeier)\
**Post date:** [13 August 2020 15:05 UTC](https://discourse.nodered.org/t/nodered-mqtt-disconnect-from-aws-ec2/31505/3 "2020-08-13T15:05:18Z")

</div>

Hmm. I haven't touched this instance in months. I'd hope that these don't just reboot by itself. Where would I find the NodeRED logs for a standard install? Maybe there's something to be found...

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [13 August 2020 15:09 UTC](https://discourse.nodered.org/t/nodered-mqtt-disconnect-from-aws-ec2/31505/4 "2020-08-13T15:09:15Z")

</div>

Depends how you start it. If as a service it should log to the system log. If via a command at start then to the console.

---

<div class="post-metadata">

**Author:** ![tfmeier](https://avatars.discourse-cdn.com/v4/letter/t/a9a28c/32.png) [@tfmeier](https://discourse.nodered.org/u/tfmeier)\
**Post date:** [15 August 2020 08:26 UTC](https://discourse.nodered.org/t/nodered-mqtt-disconnect-from-aws-ec2/31505/5 "2020-08-15T08:26:19Z")

</div>

Ok, finally managed to logon to the AWS console... the Node-RED messages are coming through to the console.

I did a straight install via

```auto
sudo npm install -g --unsafe-perm node-red node-red-admin

```

Two questions

- Is it possible to switch this over to a service so that this gets logged to the systemlog
- How do I ensure Node-RED automatically restarts when the instance restarts?

Thanks

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [15 August 2020 09:52 UTC](https://discourse.nodered.org/t/nodered-mqtt-disconnect-from-aws-ec2/31505/6 "2020-08-15T09:52:33Z")

</div>

If you installed on Ubuntu then you could follow the instructions here ? [https://nodered.org/docs/getting-started/aws#running-on-aws-ec2-with-ubuntu](https://nodered.org/docs/getting-started/aws#running-on-aws-ec2-with-ubuntu)  
Though the default debian script from the Pi install should also work - [https://nodered.org/docs/getting-started/raspberrypi](https://nodered.org/docs/getting-started/raspberrypi)

---

<div class="post-metadata">

**Author:** ![jesperuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jesperuk/32/27244_2.png) [@jesperuk](https://discourse.nodered.org/u/jesperuk)\
**Post date:** [15 August 2020 15:03 UTC](https://discourse.nodered.org/t/nodered-mqtt-disconnect-from-aws-ec2/31505/7 "2020-08-15T15:03:08Z")

</div>

I never really got along with AWS Iot, so launched an EC2 instance running mqtt server instead. From what I could tell it's cheaper to run as well.

At some point I experienced kinda the same issue on the EC2 instance, as you describe but after disabling logging, the problem went away.

---

<div class="post-metadata">

**Author:** ![tfmeier](https://avatars.discourse-cdn.com/v4/letter/t/a9a28c/32.png) [@tfmeier](https://discourse.nodered.org/u/tfmeier)\
**Post date:** [16 August 2020 23:31 UTC](https://discourse.nodered.org/t/nodered-mqtt-disconnect-from-aws-ec2/31505/8 "2020-08-16T23:31:02Z")

</div>

Followed the AWS instructions. All ok except the last one where the pm2 startup asks you to edit a startup script. What file am I looking for and where would that be located?

---

<div class="post-metadata">

**Author:** ![tfmeier](https://avatars.discourse-cdn.com/v4/letter/t/a9a28c/32.png) [@tfmeier](https://discourse.nodered.org/u/tfmeier)\
**Post date:** [3 September 2020 12:45 UTC](https://discourse.nodered.org/t/nodered-mqtt-disconnect-from-aws-ec2/31505/9 "2020-09-03T12:45:24Z")

</div>

I followed the setup section on [https://nodered.org/docs/getting-started/aws#running-on-aws-ec2-with-ubuntu](https://nodered.org/docs/getting-started/aws#running-on-aws-ec2-with-ubuntu).

The last step

`pm2 startup`

shows this

```auto
ubuntu
[PM2] To setup the Startup Script, copy/paste the following command:
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u ubuntu --hp /home/ubuntu

```

which startup script is this referring to? Folder?

As a followup question after this is done and Node-RED starts as a service where would I find the Node-RED log file?

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [3 September 2020 23:31 UTC](https://discourse.nodered.org/t/nodered-mqtt-disconnect-from-aws-ec2/31505/10 "2020-09-03T23:31:56Z")

</div>

> [@tfmeier](#):
>
> `sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u ubuntu --hp /home/ubuntu`

PM2 is trying to tell you to run that command for PM2 to be set as a systemd script and start up automatically. After that is done, you'll need to start Node-RED using PM2 and then it should be managed by it.

---

<div class="post-metadata">

**Author:** ![tfmeier](https://avatars.discourse-cdn.com/v4/letter/t/a9a28c/32.png) [@tfmeier](https://discourse.nodered.org/u/tfmeier)\
**Post date:** [4 September 2020 07:34 UTC](https://discourse.nodered.org/t/nodered-mqtt-disconnect-from-aws-ec2/31505/11 "2020-09-04T07:34:54Z")

</div>

Thanks. Well, that's almost embarrassing to have missed that 🙂

Node-RED comes up automatically.

Where does Node-RED log to by default given it runs as a service? I'm not a Unix expert so struggling a bit here. Had a look at /node-red/settings.js and the section logging states only console logging is supported. Given I just moved away from the console what happens to logging?

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [4 September 2020 07:45 UTC](https://discourse.nodered.org/t/nodered-mqtt-disconnect-from-aws-ec2/31505/12 "2020-09-04T07:45:29Z")

</div>

If you're running it using PM2, the logs will be handled by it and by default placed under `$HOME/.pm2`. It will log what would otherwise have been displayed on the console. So I assume the logs will be under `/home/ubuntu/.pm2` in your case.

Note that PM2 can write errors to a separate file if configure so. If I remember correctly that is the default.

---

<div class="post-metadata">

**Author:** ![tfmeier](https://avatars.discourse-cdn.com/v4/letter/t/a9a28c/32.png) [@tfmeier](https://discourse.nodered.org/u/tfmeier)\
**Post date:** [4 September 2020 07:52 UTC](https://discourse.nodered.org/t/nodered-mqtt-disconnect-from-aws-ec2/31505/13 "2020-09-04T07:52:01Z")

</div>

Bingo!

Yes, it was in `/home/ubuntu/.pm2/logs` and it writes two separate files `node-red-error.log` and `node-red-out.log`

The log level detail would still be defined in `settings.js`, right?

Thanks heaps!

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [4 September 2020 12:57 UTC](https://discourse.nodered.org/t/nodered-mqtt-disconnect-from-aws-ec2/31505/14 "2020-09-04T12:57:19Z")

</div>

> [@tfmeier](#):
>
> The log level detail would still be defined in `settings.js` , right?

Yep, correct.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [18 September 2020 12:57 UTC](https://discourse.nodered.org/t/nodered-mqtt-disconnect-from-aws-ec2/31505/15 "2020-09-18T12:57:27Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
