Skip to content

Instantly share code, notes, and snippets.

@jasonforte
Last active June 2, 2021 15:02
Show Gist options
  • Select an option

  • Save jasonforte/c57fd23ad7ce3f0db60725f4e0101ff4 to your computer and use it in GitHub Desktop.

Select an option

Save jasonforte/c57fd23ad7ce3f0db60725f4e0101ff4 to your computer and use it in GitHub Desktop.
Amazon CloudWatch Logs - Standard Setup - Ubuntu

Amazon CloudWatch Agent - Setup Instructions

Install the Amazon CloudWatch Agent on the instances. We want to monitor the memory usage mostly but the CPU & disk metrics are nice to have available too.

Installation

Connect to the instance and run the following:

$ wget https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb
$ sudo dpkg -i amazon-cloudwatch-agent.deb

Add configuration for CloudWatch Agent

Add the config from amazon-cloudwatch-agent.json to the cloudwatch agent config file at /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json

$ sudo vi /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json

Starting the CloudWatch Agent

Once the config file is available, you can start the agent

$ sudo systemctl start amazon-cloudwatch-agent.service

Check Agent is Running and Troubleshooting

$ sudo systemctl status amazon-cloudwatch-agent.service # should be running
$ sudo journalctl -fu amazon-cloudwatch-agent # Any logs on startup
$ cat /opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log # Check for errors in the agent logs

Once installed wait a couple of minutes. You should start to see metrics coming through under the mem_used_percent tag.

https://eu-west-1.console.aws.amazon.com/cloudwatch/home?region=eu-west-1#metricsV2:graph=~(view~'timeSeries~stacked~false~region~'eu-west-1~start~'-PT1H~end~'P0D~stat~'Average~period~300);query=~'mem_used_percent

{
"agent": {
"metrics_collection_interval": 60,
"run_as_user": "cwagent"
},
"metrics": {
"append_dimensions": {
"AutoScalingGroupName": "${aws:AutoScalingGroupName}",
"ImageId": "${aws:ImageId}",
"InstanceId": "${aws:InstanceId}",
"InstanceType": "${aws:InstanceType}"
},
"metrics_collected": {
"cpu": {
"measurement": [
"cpu_usage_idle",
"cpu_usage_iowait",
"cpu_usage_user",
"cpu_usage_system"
],
"metrics_collection_interval": 60,
"totalcpu": false
},
"disk": {
"measurement": [
"used_percent",
"inodes_free"
],
"metrics_collection_interval": 60,
"resources": [
"*"
]
},
"diskio": {
"measurement": [
"io_time"
],
"metrics_collection_interval": 60,
"resources": [
"*"
]
},
"mem": {
"measurement": [
"mem_used_percent"
],
"metrics_collection_interval": 60
},
"swap": {
"measurement": [
"swap_used_percent"
],
"metrics_collection_interval": 60
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment