Ensure the permissions. It needs 4 permissions the following.
- cloudwatch:PutMetricData
- cloudwatch:GetMetricStatistics
- cloudwatch:ListMetrics
- ec2:DescribeTags
You can attach permittions using one of the following options.
- Associate an IAM role with your instance
- Specify your AWS credentials in a credentials file
Run attached file or Copy and Paste the following on the target EC2 instance with SSH. You can change the mon-put-instance-data.pl options as you needs. Please have a look at this link. https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/mon-scripts.html#using_put_script
# install dependencies
sudo yum install perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https -y
cd
# download a script
curl https://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.2.zip -O
# unzip script
unzip CloudWatchMonitoringScripts-1.2.2.zip
# delete a downloaded script
rm CloudWatchMonitoringScripts-1.2.2.zip
# add to crontab
(crontab -l ; echo "*/5 * * * * ~/aws-scripts-mon/mon-put-instance-data.pl --mem-util --mem-used --mem-avail --swap-util --swap-used --disk-space-util --disk-space-used --disk-space-avail --disk-path=/ --from-cron") | crontab
The data will be published to CloudWatch each 5 minute. You can see it the following.