How to Send MongoDB Logs to CloudWatch Logs
Table of Contents
Introduction
This blog shows how to send MongoDB log files to AWS CloudWatch Logs. You can send multiple logs to AWS CloudWatch Logs by defining the awslogs.conf file.
Prerequisites
-
Set up an AWS account
-
MongoDB is running on an EC2 Linux instance
We assume you have already set up an EC2 instance with MongoDB. The following are the steps to send MongoDB logs to CloudWatch Logs.
Install the CloudWatch Logs Agent
Update the Amazon Linux 2 instance with the latest repositories.
sudo yum update -y
Install the AWS Logs agent on the EC2 instance.
sudo yum install -y awslogs
You'll need to modify the following file to change 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 CloudWatch Logs.
cd /etc/awslogs/
nano awslogs.conf
After opening the awslogs.conf file, you can add the following section, log-stream2. It will define a new log group named ta-mongo-logs, which will show up in CloudWatch Logs and push all the logs from /var/log/mongodb/mongod.log to CloudWatch Logs.
By doing it this way, you can continue to add more sections for different log files and push them to 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
Conclusion
That's how you can send MongoDB logs to AWS CloudWatch Logs. By adding additional sections to the awslogs.conf file, you can send logs from multiple sources to CloudWatch for centralized log management and analysis.