DEV Community

Cover image for How To Install Cloudwatch Agent On Amazon Linux By Using CLI
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

How To Install Cloudwatch Agent On Amazon Linux By Using CLI

Step 1 - Create EC2 and attach CloudWatchAgentAdminPolicy role to EC2

  • Choose CloudWatchAgentAdminPolicy role which already created by using AWS Managed Rule CloudWatchAgentAdminPolicy

Step 2 - Install CloudWatch Agent on EC2

  • For installation CloudWatch agent we need to execute the following command:
sudo yum install amazon-cloudwatch-agent -y 
Enter fullscreen mode Exit fullscreen mode
  • After running this command you will see that installation is complete.

  • Configure the CloudWatch agent with the wizard and fill in data about our log file.To create a configuration file execute the following command:
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard
Enter fullscreen mode Exit fullscreen mode
  • After running this command we need to answer the following questions (I will provide answers for my configuration):

  1. On which OS are you planning to use the agent? (Linux)

  1. Are you using EC2 or On-Premises hosts? (EC2)

  1. Which user are you planning to run the agent? (cwagent)

  1. Do you want to turn on the StatsD daemon? (yes)

  1. Which port do you want the StatsD daemon to listen to? (8125)

  1. What is the collection interval for the StatsD daemon? (10s)

  1. What is the aggregation interval for metrics collected by StatsD daemon?(the 60s)

  1. Do you want to monitor metrics from CollectD?(No)

  1. Do you want to monitor any host metrics? e.g. CPU, memory, etc. (yes)

  1. Do you want to monitor CPU metrics per core? (yes)

  1. Do you want to add ec2 dimensions (ImageId, InstanceId, InstanceType, AutoScalingGroupName) into all of your metrics if the info is available? (yes)

  1. Do you want to aggregate ec2 dimensions (InstanceId)? (yes)

  1. Would you like to collect your metrics at high resolution (sub-minute resolution)? This enables sub-minute resolution for all metrics, but you can customize for specific metrics in the output JSON file. (60s)

  1. Which default metrics config do you want? (Standard) On this question, you can choose the answer between Basic, Standard, Advanced, and None. (Detailed description below)
Detail level - Basic Metrics included
Mem mem_used_percent
Disk disk_used_percent

Detail level - Standard Metrics included
CPU cpu_usage_idle, cpu_usage_iowait, cpu_usage_user, cpu_usage_system
Disk disk_used_percent, disk_inodes_free
Diskio diskio_io_time
Mem mem_used_percent
Swap swap_used_percent

Detail level - Advanced Metrics included
CPU cpu_usage_idle, cpu_usage_iowait, cpu_usage_user, cpu_usage_system
Disk disk_used_percent, disk_inodes_free
Diskio diskio_io_time, diskio_write_bytes, diskio_read_bytes, diskio_writes, diskio_reads
Mem mem_used_percent
Swap swap_used_percent
Netstat netstat_tcp_established, netstat_tcp_time_wait

  1. Are you satisfied with the above config? (yes)

  1. Do you have any existing CloudWatch Log Agent? (no)

17.Do you want to monitor any log files? (yes)

  1. Log file path: (/var/log/httpd/error_log)

  1. Log group name:

  1. Log group class: (Standard)

  1. Log stream name: [{instance_id}]

  1. Log Group Retention in days (7)

  1. Do you want to specify any additional log files to monitor? (no)

  1. Do you want the CloudWatch agent to also retrieve X-ray traces?

  1. Do you want to store the config in the SSM parameter store? (no)

  • The configuration file will store in the “bin” folder:
ls /opt/aws/amazon-cloudwatch-agent/bin/
Enter fullscreen mode Exit fullscreen mode
  • Name - config.json

Step 3 - Start the CloudWatch agent with our configuration file

  • To launch the CloudWatch agent we need to execute the following command:
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json -s
Enter fullscreen mode Exit fullscreen mode
  • After running this command we see that execution is successfully finished.

  • Check CloudWatch Agent Status
sudo systemctl status amazon-cloudwatch-agent
Enter fullscreen mode Exit fullscreen mode


Resources & Next Steps


Top comments (0)