Send pm2 logs from ec2 instance to cloudwatch
Asked Answered
P

2

6

I have a Node.js app running on an AWS EC2(ubuntu) instance using pm2. I want to send logs generated by the app from EC2 to Cloudwatch. How can I achieve this?

Poignant answered 23/6, 2020 at 12:3 Comment(0)
R
7

Pm2 usually creates a log file in the home folder at ~/.pm2/logs/{app_name}-{id}. Start by changing or introducing a pm2 ecosystem file to redirect error and logs to the same location, let's say /var/logs/myApp.log

Next, you just have to install and configure the aws cloudwatch agent on your vm. See aws docs on how to do that https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/QuickStartEC2Instance.html

While configuring the agent, it lets you choose which files it should monitor for changes. Add the path to yours there.

Rendering answered 18/1, 2021 at 11:19 Comment(1)
this tutorial is useful: youtube.com/watch?v=7UlFuwONrvQBinocular
E
1

There is no direct way to push pm2 logs to AWS cloudwatch logs, pm2 does not have that facility. Instead pm2 keeps it in file and that stores in the server where your NodeJS is running.

What you can give a try to read the pm2 log file, the delta changes (CDC - capture data change) and push it to AWS cloudwatch. NPM fs can be useful here.

Endocrinotherapy answered 18/8, 2020 at 13:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.