Automatically install and Configure CloudWatch Custom metrics and automatically generate alarms for Amazon Linux EC2

Heena Gangrekar
Ankercloud Engineering
4 min readNov 17, 2023

As part of a series of cloudwatch installation and configuration for custom metrics, the next one is why not to automate cloudwatch alarm creation?

So let’s go into deep dive,

As we know that everyone want to understand there EC2 custom performance such as memory and disk utilization, Here actually the solution is based on the requirement that everytime we created the new EC2 instance we manually installing the cloudwatch Agent, manually configuring custom metrics and manually creating alarms,

What if i say that now all these things are possible with the userdata script which i discover.

Lets move into Steps:

Step 1: Create IAM role with these permissions

IAM Role Required Permissions

Create customer inline policy with these code

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "ec2:DescribeInstances",
"Resource": "*"
}
]
}

Step 2: Start Launching an EC2 instance with required Configuration

I. Update metadata version to be optional

II. Select created IAM role

III. Update userdata with these script

Note: You can prefer or create a new SNS topic which we are going to use for sending notification and modify the code with respect to your region. In the Alarm creation code update your sns ARN and region name

#!/bin/bash
yum install amazon-cloudwatch-agent -y

cat <<EOF > /opt/aws/amazon-cloudwatch-agent/bin/config.json
{
"agent": {
"metrics_collection_interval": 60,
"run_as_user": "cwagent"
},
"metrics": {
"metrics_collected": {
"cpu": {
"measurement": ["cpu_usage_idle"],
"metrics_collection_interval": 60,
"resources": ["*"],
"totalcpu": true
},
"disk": {
"measurement": ["used_percent"],
"metrics_collection_interval": 60,
"resources": ["*"]
},
"diskio": {
"measurement": ["write_bytes", "read_bytes", "writes", "reads"],
"metrics_collection_interval": 60,
"resources": ["*"]
},
"mem": {
"measurement": ["mem_used_percent"],
"metrics_collection_interval": 60
},
"net": {
"measurement": ["bytes_sent", "bytes_recv", "packets_sent", "packets_recv"],
"metrics_collection_interval": 60,
"resources": ["*"]
},
"swap": {
"measurement": ["swap_used_percent"],
"metrics_collection_interval": 60
}
}
}
}
EOF

/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json -s
sleep 240

instance_hostname=$(hostname)

# Create CloudWatch Alarms

# Memory Utilization Alarm
aws cloudwatch put-metric-alarm \
--alarm-name MemoryUtilizationAlarm \
--alarm-description "Memory utilization exceeds 70%" \
--namespace CWAgent \
--metric-name mem_used_percent \
--dimensions Name=host,Value=$instance_hostname \
--statistic Average \
--period 300 \
--comparison-operator GreaterThanThreshold \
--threshold 70 \
--evaluation-periods 1 \
--alarm-actions arn:aws:sns \
--region us-east-2


# CPU Utilization Alarm
aws cloudwatch put-metric-alarm \
--alarm-name CPUUtilizationAlarm \
--alarm-description "CPU utilization exceeds 80%" \
--namespace AWS/EC2 \
--metric-name CPUUtilization \
--dimensions Name=InstanceId,Value=$(curl -s http://169.254.169.254/latest/meta-data/instance-id) \
--statistic Average \
--period 300 \
--threshold 80.0 \
--comparison-operator GreaterThanThreshold \
--evaluation-periods 1 \
--alarm-actions arn:aws:sns \
--region us-east-2

instance_hostname=$(hostname)

# Disk Utilization Alarm
aws cloudwatch put-metric-alarm \
--alarm-name DiskUtilizationAlarm1 \
--alarm-description "Disk utilization exceeds 80%" \
--namespace CWAgent \
--metric-name disk_used_percent \
--dimensions Name=path,Value=/dev Name=host,Value=$instance_hostname Name=device,Value=devtmpfs Name=fstype,Value=devtmpfs \
--statistic Average \
--period 300 \
--threshold 80.0 \
--comparison-operator GreaterThanThreshold \
--evaluation-periods 1 \
--alarm-actions arn:aws:sns \
--region us-east-2

Output: Check your CloudWatch it will show you the newly created cwagent with specified metrics

See alarm status in the Alarms section.

With this blog you can able to Automate the process of CloudWatch installation and alarm creation on your Amazon Linux EC2 instance. You can use the same user data or same AMI to create multiple instances to setup the cloudwatch and alarm configuration.

--

--