CloudWatch Agent on Ubuntu

From Cramsession
Jump to navigationJump to search
✍️ Verified Author: MflavellClick to view professional profile & credentials

How to guide

Installing

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

Create IAM Role

  • Sign in to the IAM console
  • Select Roles, Create Role
  • Select AWS Service
  • Select EC2 common use case
  • Select Next
  • Find CloudWatchAgentServerPolicy
  • Click checkbox next to CloudWatchAgentServerPolicy
  • Click Next
  • Provide a Role Name
  • Click Create Role
  • Link IAM role to instance.
    • Select Insane > Actions > Security > Modify IAM Role
    • Add created IAM Role

Create an Access key

Create an access key for a server admin Save the key

Configure the agent

sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard

Answer questions

  • Make sure you select root as the user
  • dont save the configuration online

Start the agent

Linux will save the configuration information here: /opt/aws/amazon-cloudwatch-agent/bin/config.json

Run the command

sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json

Example:

ubuntu@ip-10-0-0-4:/opt/aws/amazon-cloudwatch-agent/etc$ sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c  file:/opt/aws/amazon-cloudwatch-agent/etc/env-config.json 
****** processing amazon-cloudwatch-agent ******
I! Trying to detect region from ec2 D! [EC2] Found active network interface I! imds retry client will retry 1 timesSuccessfully fetched the config and saved in  /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d/file_env-config.json.tmp
Start configuration validation...
2024/07/17 14:02:24 Reading json config file path: /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d/file_env-config.json.tmp ...
2024/07/17 14:02:24 I! Valid Json input schema.
2024/07/17 14:02:24 Configuration validation first phase succeeded
I! Detecting run_as_user...
I! Trying to detect region from ec2
D! [EC2] Found active network interface
I! imds retry client will retry 1 times
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent -schematest -config /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.toml
Configuration validation second phase succeeded
Configuration validation succeeded

Check Status

sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status

You shoud see...

{
 "status": "running",
 "starttime": "2024-07-17T17:15:14+00:00",
 "configstatus": "configured",
 "version": "1.300041.0b681"
}

Adding Metrics

  • Metrics are under CW Agent in CloudWatch

Removing

Removing the CloudWatch Agent

References

https://www.youtube.com/watch?v=JKQgXW5quNk

https://pushkar-sre.medium.com/detailed-guide-installing-and-configuring-aws-cloudwatch-agent-on-ec2-instances-462ddf67a3dc

https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/troubleshooting-CloudWatch-Agent.html