CloudWatch custom metrics part one
Hello Everyone
Welcome to CloudAffaire and this is Debjeet
In the last blog posts, we have discussed the basics of CloudWatch.
https://cloudaffaire.com/cloudwatch/
In this blog post, we are going to create custom matrices for EC2 instance in CloudWatch. By default, AWS provides a set of default metrics for your EC2 instance monitoring. But you can always create your metrics and publish to the CloudWatch. There are several ways to create and publish your custom metrics in CloudWatch. In this demo, we are going to use AWS provided Perl scripts to create our own custom metrics.
Prerequisite for this demo:
- One running EC2 instance.
- IAM role with proper access to CloudWatch.
Create CloudWatch custom metrics for EC2 instance:
Step 1: Select your EC2 instance and click from ‘Actions’ click ‘Attach/Replace IAM Role’.
Click ‘Create new IAM role’. You will be navigated to IAM page.
Note: If you already have an IAM role with proper access the select it from the drop-down. In this demo, we will create an IAM role as well.
Click ‘Create Role’.
Select ‘EC2’ as service and click ‘Next:Permission’
Click ‘Create policy’.
Provide below JSON block under JSON and click ‘Review Policy’.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
{ "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": [ "cloudwatch:PutMetricData", "ec2:DescribeTags", "cloudwatch:GetMetricStatistics", "cloudwatch:ListMetrics" ], "Resource": "*" } ] } |
Provide a name to your Policy and click ‘Create Policy’.
Select the policy and click ‘Next: Tags’.
Provide a name to the IAM role and click ‘Review’.
Click ‘Create role’.
One success message will be displayed.
Next, select the IAM role and click ‘Attach’.
Our instance is ready for deployment of custom scripts provided by AWS.
Step 2: Login to your EC2 instance install some Perl packages required to run the Perl script.
1 |
sudo yum install -y perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https perl-Digest-SHA.x86_64 |
Step 3: Download the AWS provided Perl scripts.
1 |
curl https://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.2.zip -O |
Step 4: Unzip the Perl script.
1 2 3 |
unzip CloudWatchMonitoringScripts-1.2.2.zip && \ rm CloudWatchMonitoringScripts-1.2.2.zip && \ cd aws-scripts-mon |
Step 5: Test if the script is working
1 |
./mon-put-instance-data.pl --mem-util --mem-used --verify --verbose |
Note: For complete parameter list, please refer below AWS documentation
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/mon-scripts.html
Try to publish a single data point in CloudWatch
1 |
./mon-put-instance-data.pl --mem-util --mem-used |
Note: We have successfully published a single data point in CloudWatch using AWS provided Perl script. You can also check the CloudWatch console under Metrics.
Before custom metrics publication:
After custom metrics publication:
You can also put the script under a cron job to continually get custom metrics data in CloudWatch.
1 2 3 |
sudo crontab –e */5 * * * * /home/ec2-user/aws-scripts-mon/mon-put-instance-data.pl --mem-util --mem-used --from-cron |
Hope you have enjoyed this article. In the next blog post, we will use our own script to monitor and publish metrics data in CloudWatch using AWS CLI.
To get more details on CloudWatch, please refer below AWS documentation
https://docs.aws.amazon.com/cloudwatch/index.html