Cloudwatch alarms for memory and disk for AWS Ec2 using Terraform

Reading Time: 2 minutes

Hi all, in this post we will be going to learn about what are cloudwatch alarms, how to create cloudwatch alarms using terraform as in many use cases we need to provision infrastructure using IAAC(Infrastructure as a code), here terraform.

Introduction

Alarm:- In AWS we have concept of cloudwatch alarms from where we can track various metric on our ec2. Using alarm we can keep an eye on whether our usage has gone beyond the threshold value, if gone beyond then we can use sns(simple notification service) to send a mail to the user informing/alerting about the same.

Before going further in the alarm let us know one thing that there are namespaces. Some metrics are directly going from ec2 and we can use them in our alarm but metrics like disk usage and memory usage we need to install cloudwatch agent in our ec2. cloudwatch agent will be sending the variety of metrics from our ec2 to cloudwatch.

Downloading and Installing Cloudwatch Agent

Here i will be downloading cloudwatch agent on Amazon Linux Machine(Red hat based).

let us say we have already one ec2 instance running. connect to your ec2 instance

  1. cd /tmp
  2. wget https://s3.amazonaws.com/amazoncloudwatch-agent/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm .
  3. This will sudo rpm -U amazon-cloudwatch-agent.rpm .

Configure Cloudwatch Agent

Before going to create alarm we first need to configure cloudwatch agent for our metrics.

  1. First assign a role to your ec2, giving it full cloudwatch access

2. sudo nano /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json

3. Paste the following content

{
“agent”: {
“metrics_collection_interval”: 60,
“run_as_user”: “cwagent”
},
“metrics”: {
“append_dimensions”: {
“InstanceId”: “${aws:InstanceId}”
},
“metrics_collected”: {
“disk”: {
“measurement”: [
“used_percent”
],
“metrics_collection_interval”: 60,
“resources”: [
“/”
]
},
“mem”: {
“measurement”: [
“used_percent”
],
“metrics_collection_interval”: 60,
“resources”: [
“/”
]
}
}
}
}

4. sudo systemctl restart amazon-cloudwatch-agent. This will restart the agent

5. After sometime the metric will be visible in cloudwatch metric under CWAgent

Creating Cloudwatch Alarms using terraform

  1. Create main.tf file. This is my github repo link for disk usage only. Just add one more alarm for mem used–https://github.com/akpriyadarshi/aws_cloudwatch_dashboard_alarm_custom_metrics_using_terraform/blob/master/main.tf
  2. We have aws_cloudwatch_metric_alarm resource in terraform to deal with aws alarms
  3. In metric name write disk_used_percent and mem_used_percent respectively for both alarms
  4. Namespace would be CWAgent
  5. Dimension for mem_used_percent would be only InstanceId and for disk_used_percent i have provided in github main.tf
  6. you can have different comparison_operator. I have used GreaterThanOrEqualToThreshold. so that when value goes beyond to threshold it will trigger the alarm
  7. Please put arn of your sns(simple notofication service) to which you want to send email when alarm triggers
  8. Add period in seconds for after how much time you want datapoints. I have provided for every 2 minutes
  9. Apply the terraform code
  10. After that you will have cloudwatch alarm provisoned using terraform which will be getting metrics from cloudwatch agent

To know little basic about alarm visit https://blog.knoldus.com/how-to-set-alarms-and-notifications-in-aws-cloudwatch/

Scala Future

Leave a Reply