Search code examples
node.jsamazon-ec2amazon-cloudwatchpm2

Send pm2 logs from ec2 instance to cloudwatch


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?


Solution

  • 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.