Skip to content

Instantly share code, notes, and snippets.

@atheiman
Last active March 13, 2023 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atheiman/2091894d2b76844f619d40bcbcc6fd8d to your computer and use it in GitHub Desktop.
Save atheiman/2091894d2b76844f619d40bcbcc6fd8d to your computer and use it in GitHub Desktop.
Linux Amazon CloudWatch Agent install and configure guide

Amazon CloudWatch Agent for Linux

  1. Attach needed IAM permissions to the EC2 instance profile IAM role:
    • CloudWatchAgentServerPolicy AWS managed IAM policy
    • Additional permissions likely needed (to download config from SSM parameter and to set CW Logs retention):
      {
        "Effect": "Allow",
        "Action": [
          "ssm:GetParameter"
        ],
        "Resource": [
          "arn:aws:ssm:*:*:parameter/*CloudWatch*",
          "arn:aws:ssm:*:*:parameter/*cloudwatch*",
        ]
      },
      {
        "Effect": "Allow",
        "Action": [
          "logs:PutRetentionPolicy"
        ],
        "Resource": "*"
      }
  2. Install the agent:
    • Amazon Linux 2: sudo yum install amazon-cloudwatch-agent
    • Other distributions:
      • Download and install the package. Ubuntu example:
        curl -LO https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb
        sudo dpkg -i -E ./amazon-cloudwatch-agent.deb
  3. Create /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json. The included example is a reasonable default, all available config can be found here.
  4. Apply the config and start the 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
{
"metrics": {
"append_dimensions": {
"ImageId": "${aws:ImageId}",
"InstanceId": "${aws:InstanceId}",
"InstanceType": "${aws:InstanceType}",
"AutoScalingGroupName": "${aws:AutoScalingGroupName}"
},
"metrics_collected": {
"disk": {
"measurement": [ "used_percent" ],
"ignore_file_system_types": [
"devtmpfs",
"overlay",
"squashfs",
"sysfs",
"tmpfs"
]
},
"mem": {
"measurement": [ "used_percent" ]
},
"processes": {
"measurement": [
"total",
"total_threads"
]
}
}
},
"logs": {
"logs_collected": {
"files": {
"collect_list": [
{
"file_path": "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log",
"log_group_name": "/amazon-cloudwatch-agent/amazon-cloudwatch-agent.log",
"retention_in_days": 14
}
]
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment