Monitoring Memory (RAM) with AWS ElasticBeanstalk EC2 Instances & Cloudwatch

If you are like me, you are wondering why in the world Amazon Web Services Cloudwatch service doesn’t allow you to monitor memory usage?  Luckily they do allow you to use custom Perl scripts to create your own custom metrics. The following instructions will show you how to setup memory monitoring on your ElasticBeanstalk EC2 instances in less than 5 minutes. This uses the scripts from here as a resource and shows you how to use them within your AWS ElasticBeanstalk Environment.

 

Step 1: Setup an IAM fole for your ElasticBeanstalk environments.

First, click your name in the top right of the AWS console and go to Security Credentials. On this page, click roles on the left and assign your aws-elasticbeanstalk-ec2-role CloudWatch Full Access.

add-iam-role-to-elasticbeanstalk-instances

Next, go into your ElasticBeanstalk environment (repeat for each environment) and assign the role you gave Cloudwatch access to above:

assign-iam-role-elasticbeanstalk-ec2

 

Step 2: Add the following to your GIT Repo

Add this in a folder named .ebextensions at the root of your repo and give it a filename of 15-memorymonitor.config.

container_commands:
  00download:
    command: "wget http://ec2-downloads.s3.amazonaws.com/cloudwatch-samples/CloudWatchMonitoringScripts-v1.1.0.zip"
    ignoreErrors: true
  01extract:
    command: "unzip CloudWatchMonitoringScripts-v1.1.0.zip"
    ignoreErrors: true
  02rmzip:
    command: "rm CloudWatchMonitoringScripts-v1.1.0.zip"
    ignoreErrors: true
  03cdinto:
    command: "mv aws-scripts-mon/ /home/ec2-user"
    ignoreErrors: true
  04cron:
    command: "crontab -l | { cat; echo '* * * * * /home/ec2-user/aws-scripts-mon/mon-put-instance-data.pl --mem-util --mem-used --mem-avail'; } | crontab -"
    ignoreErrors: false

 

Step 3: Push your new code to AWS

We prefer to do this via our continuous integration server but you can do this with the simple command line:

git aws.push

 

Conclusion

Within a few minutes you should see 3 memory metrics available in Cloudwatch under Linux System

show-memory-usage-cloudwatch
{o}
Otreva is a custom software product development company focusing on user experience, responsive web development, & mobile application development.

Interested in seeing what it costs to build an app?

Start Quote See Stats
224 Wyoming Ave. #100
Scranton, PA 18503

You've found the secret footer!

Tweets about @Otreva

Or check us out on:

Shopify Experts
Awwwards