How to Send MongoDB Logs to CloudWatch Logs

2021/3/52 min read
bookmark this
Responsive image

Pre requirement

  • Set up an AWS account
  • MongoDB is running under the EC2 Linux Instance

To sending MongoDB log file, we assume you have setup EC2 instance with MongoDB, now following are steps how to send logs to the CloudWatch logs.

Install old CloudWatch logs agent

Update Amazon Linux 2 instance with the latest repositories.

sudo yum update -y

Install AWS logs to EC2 instance.

sudo yum install -y awslogs

You'll need to cd to the following file to modify the AWS region if you're not using the default region

/etc/awslogs/awscli.conf

open the awslogs.conf file to modify the configuration for the CloudWatch logs.

cd /etc/awslogs/
nano awslogs.conf

After open the awslogs.conf file, you can modify by add following section, log-stream2. It will create define a new log group name as ta-mongo-logs, which will show on the CloudWatch logs, and push all the logs from /var/log/mongodb/mongod.log to the cloudwatch logs.

By doing it in this way, you can continue to add more section for different log files and push it to the CloudWatch logs. 

[log-stream2]
datetime_format = %b %d %H:%M:%S
file = /var/log/mongodb/mongod.log
buffer_duration = 5000
log_stream_name = {instance_id}
initial_position = start_of_file
log_group_name = ta-mongo-logs