Skip to content

Instantly share code, notes, and snippets.

@kyleian
Created June 14, 2018 19:33
Show Gist options
  • Save kyleian/9c2167d8e3f79ed88fff9dc78ca7b1f0 to your computer and use it in GitHub Desktop.
Save kyleian/9c2167d8e3f79ed88fff9dc78ca7b1f0 to your computer and use it in GitHub Desktop.
Cloudwatch Cloudformation Centos 7.2 RHEL UserData for Adding Memory Utilization Reporting to Alarm on MemoryUtilization
{
"UserData": { "Fn::Base64": { "Fn::Join": ["", [
"#!/bin/bash\n",
"cd /root \n",
"sudo yum install perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https perl-Digest-SHA -y\n",
"sudo yum install zip unzip -y \n",
"curl https://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.2.zip -O \n",
"unzip CloudWatchMonitoringScripts-1.2.2.zip \n",
"rm CloudWatchMonitoringScripts-1.2.2.zip \n",
"cd aws-scripts-mon \n",
"cp awscreds.template awscreds.conf \n",
"(crontab -l 2>/dev/null; echo \"*/1 * * * * /root/aws-scripts-mon/mon-put-instance-data.pl --mem-used-incl-cache-buff --mem-util --mem-used --mem-avail\") | crontab - \n"
]]}}
}
@kyleian
Copy link
Author

kyleian commented Jun 14, 2018

Tested on Centos 7.2, this script in Userdata will install the appropriate Perl requirements to run the monitor script, as well as add a cron to report Memory data every minute to Cloudwatch. This is useful for researching memory leaks, or rebooting EC2s when they are memory exhausted using a Cloudwatch alarm on what this script reports.

Note that your instance will either require an IAM role granted to the EC2, or credentials placed in that awscreds.conf on ln 11, that have access to the following:

[{
   "Action": "cloudwatch:PutMetricData",
   "Effect": "Allow",
   "Resource": "*"
  },
  {
    "Action": "cloudwatch:GetMetricStatistics",
    "Effect": "Allow",
    "Resource": "*"
  },
  {
   "Action": "cloudwatch:ListMetrics",
   "Effect": "Allow",
   "Resource": "*"
}]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment