Installing CloudWatch Agent

  • Attach IAM role to EC2 instance – CloudWatchAgentRoleForEC2 (Need to create this role to allow EC2 instance to be able to access / write to CloudWatch logs)
  • Ubuntu AMD64
    • Download and install agent for AMD64
      • wget <https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb> -O /tmp/amazon-cloudwatch-agent.deb
      • sudo dpkg -i /tmp/amazon-cloudwatch-agent.deb
  • Ubuntu ARM64
    • Download and install agent for ARM64
      • wget <https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/arm64/latest/amazon-cloudwatch-agent.deb> -O /tmp/amazon-cloudwatch-agent.deb
      • sudo dpkg -i /tmp/amazon-cloudwatch-agent.deb
  • Setup configuration file
sudo tee /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json > /dev/null <<-'EOF'
{
    "agent": {
        "metrics_collection_interval": 300,
        "logfile": "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log",
        "debug": false,
        "run_as_user": "cwagent"
        },
    "metrics": {
        "append_dimensions": {
            "InstanceId": "${aws:InstanceId}"
        },
        "metrics_collected": {
            "disk": {
                "measurement": [
                    "used_percent"
                ],
                "metrics_collection_interval": 300,
                "resources": [
                    "/"
                ]
            },
            "diskio": {
                "measurement": [
                    "io_time",
                    "write_bytes",
                    "read_bytes"
                ],
                "metrics_collection_interval": 300,
                "resources": [
                    "xvda", "nvme0n1"
                ]
            },
            "mem": {
                "measurement": [
                    "mem_available_percent"
                ],
                "metrics_collection_interval": 300
            }
        }
    }

}
EOF
  • Start CloudWatch Agent
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/amazon-cloudwatch-agent.json
  • Check status of CloudWatch Agent
systemctl status amazon-cloudwatch-agent
  • To stop CloudWatch Agent
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a stop