On-Premise CloudWatch Metrics with AWS CLI

Who will watch the watchmen?


Have you tried AWS CLI?

CloudWatch is pretty cool, and looking at a metrics dashboard and setting up alarms is pretty badass—especially when you're not a full-time DevOps and don't have the time to worry about your monitoring solution in addition to the thing it's supposed to look after.

If you're running some of your stuff outside AWS (cue gasps), CloudWatch agent is supposed to have you covered, but after following the instructions to a tee, I found that it wasn't doing what it said on the tin, and the agent was refusing to start1.

Anyhoo, I was running out of time and I absolutely had to get this very simple metric into CloudWatch, and after 2 days of losing sleep and trying to make the agent work, I gave up. Thankfully, the ever reliable AWS CLI was there to pick me up.

It turns out a simple aws cloudwatch put-metric-data is all you need to push metrics to CloudWatch. There's actually an entire doc on it, which I only found while writing this post.

The Setup

You need to make sure you have a IAM role set up with the ability to push metrics to CloudWatch, which is covered in the CloudWatch agent docs.

For my setup, I just wanted to send the disk utilisation to CloudWatch, so I created a shell script that looked like this:

#!/bin/bash

freeSpace=`df -m | awk 'NR==2{print $4}'`
/usr/local/bin/aws cloudwatch put-metric-data --metric-name my-server-FreeDisk \
--value $freeSpace --unit Megabytes --namespace my-server --profile AmazonCloudWatchAgent

Here, I am using the --profile flag to select the correct IAM user, which has been added to my AWS credentials and config files.

The shell script was simply called every 5 minutes from a cron job, and I had the whole setup up and running in no time. You can also do fancy stuff like sending in entire JSON files, which will surely come in handy if you want to work with Docker stats etc.


  1. Some issue about the instance not having an EC2 identifier, which—duh 


Built using Pelican.