Better monitoring metrics on Elastic Beanstalk Instances

In one of my side-projects I have decided to explore how I can improve monitoring of different aspects of my infrastructure. 

After not long, I discovered the Elastic Beanstalk instances I'm using aren't emitting amazingly useful information. This is probably because it is assumed they are supposed to be mostly stateless and therefore shouldn't have to expose information about disk space, memory being free etc. 

After a little Google I discovered an excellent article by Emile Nijssen, go read the original here, it has all kinds of great details.

His solution is a YAML file that you'd push into the `.ebextensions` folder of your ElasticBeanstalk. Unfortunately, this approach has been deprecated with the introduction of Amazon Linux 2. Now you have to mash your deployment hooks into a `.platform` folder. I resolved to convert his script to the new structure. Please find it below.

All you have to do is:

  • download the script
  • add it to a folder called `.platform/hooks/postdeploy` in your project
  • make the cloudwatch.sh script executable (chmod +x cloudwatch.sh)
  • deploy your application that now has improved system metrics.

 

cloudwatch.sh script

 

									#!/bin/bash
									
									cd /home/ec2-user
									
									# get amazon cloudwatch agent and install it on the system
									wget https://s3.amazonaws.com/amazoncloudwatch-agent/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm
									yum localinstall -y amazon-cloudwatch-agent.rpm
									
									cat << 'EOF' > amazon-cloudwatch-agent.json
									{
									  "agent": {
									    "metrics_collection_interval": 60
									  },
									  "metrics": {
									    "append_dimensions": {
									      "InstanceId": "${aws:InstanceId}"
									    },
									    "metrics_collected": {
									      "mem": {
									        "measurement": [
									          "mem_total",
									          "mem_free",
									          "mem_used",
									          "mem_used_percent",
									          "mem_available",
									          "mem_available_percent"
									        ],
									        "append_dimensions": {
									          "AWSEBEnvironmentName": "__EB_ENV__"
									        }
									      },
									      "cpu": {
									        "measurement": [
									          "cpu_time_active",
									          "cpu_time_nice",
									          "cpu_time_steal",
									          "cpu_usage_active",
									          "cpu_usage_idle",
									          "cpu_usage_iowait"
									        ],
									        "append_dimensions": {
									          "AWSEBEnvironmentName": "__EB_ENV__"
									        }
									      },
									      "disk": {
									        "measurement": [
									          "disk_free",
									          "disk_total",
									          "disk_used",
									          "disk_used_percent"
									        ],
									        "append_dimensions": {
									          "AWSEBEnvironmentName": "__EB_ENV__"
									        }
									      }
									    }
									  }
									}
									EOF
									
									# make sure the access right are correct
									chmod 755 amazon-cloudwatch-agent.json
									chown root:root amazon-cloudwatch-agent.json
									
									# retrieve EB_ENV name and substitute it in the written file
									EB_ENV="$(/opt/elasticbeanstalk/bin/get-config container -k environment_name)"
									sed -i "s/__EB_ENV__/$EB_ENV/g" amazon-cloudwatch-agent.json
									
									amazon-cloudwatch-agent-ctl -a append-config -m ec2 -c file:/home/ec2-user/amazon-cloudwatch-agent.json -s
									

Also Read

How AI fits into my development workflow

By Marnix Kok on 27 August 2024

Interpreting Sitemaps, harder than you think.

By Marnix Kok on 22 August 2024