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?
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.
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.
© 2022 - 2024 — McMap. All rights reserved.