Member-only story

Monitoring MongoDB Using CloudWatch

Vinayak Pandey
2 min readAug 28, 2020

In this post, we’ll see how we can leverage CloudWatch custom metrics to setup monitoring for our MongoDB cluster.

Pre-requisite: We need a running MongoDB replicaset which we’ll set monitoring for. You can follow https://gist.github.com/calvinh8/c99e198ce5df3d8b1f1e42c1b984d7a4 to setup a MongoDB replicaset.

Step 1: Download code given at https://raw.githubusercontent.com/vinycoolguy2015/awslambda/master/mongo_monitoring.py

In the given code, make sure to change the connection string accordingly.

Step 2: Setup a cron job to run this script every minute. You need to setup that cron on a server which can access our MongoDB cluster.Also make sure that the server has an IAM role associated with it and the role has following IAM policy attached.

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "cloudwatch:PutMetricData",
"Resource": "*"
}
]
}

Once this script is executed, you’ll see metrics getting created under mongo namespace.

Step 3: Once we have the metrics data, we can setup Dashboard based on these metrics.

Enhancement: Right now we’re running the monitoring code on a server and if that server goes down, we’ll lose monitoring data. So we can wrap up the code as a package and run it using AWS Lambda.

You can also add additional metrics like replication lag etc.

Also for storing connection string(or credentials), you can use secrets manager.

--

--

Vinayak Pandey
Vinayak Pandey

Written by Vinayak Pandey

Experienced Cloud Engineer with a knack of automation. Linkedin profile: https://www.linkedin.com/in/vinayakpandeyit/

No responses yet