Monday, 14 May 2018

Enabling memory and disk utilisation on EC2

As is often the case, documentation does a great job at explaining why you should do things - Not simply what you should do.  While it's well worth knowing the detail, sometimes you want to get the thing working first!

Here's how to setup host-based monitoring (memory, disk utilisation etc.)

1. You'll need to first attach an IAM role to your EC2 server, to let it push to CloudWatch.
2. Run the commands below to download and install CloudWatchAgent.
3. Within a few minutes, you should see your new metrics flowing in your AWS console at https://eu-west-1.console.aws.amazon.com/cloudwatch/home

Note that we use "resources": ["/"] for disk monitoring.  Without this, you'll get 8x unnecessary metrics for things like /dev, /run.  If you're looking to stay within the AWS Free Tier, this will push you past the limit.


# Download and install CloudWatchAgent
wget https://s3.amazonaws.com/amazoncloudwatch-agent/linux/amd64/latest/AmazonCloudWatchAgent.zip
unzip AmazonCloudWatchAgent.zip
sudo ./install.sh

# Create a basic config
cat<<EOF | sudo tee /opt/aws/amazon-cloudwatch-agent/bin/config.json >/dev/null
{
"metrics": {
"metrics_collected": {
"mem": {
"measurement": [
"mem_used_percent"
],
"metrics_collection_interval": 60
},
"swap": {
"measurement": [
"swap_used_percent"
],
"metrics_collection_interval": 60
},
"disk": {
"resources": [
"/"
],
"measurement": [
"disk_used_percent"
],
"metrics_collection_interval": 60
}
}
}
}
EOF

# Load and check the CloudWatchAgent
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json -s

sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status


The full AWS documentation for this is at  at https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/install-CloudWatch-Agent-on-first-instance.html