AWS CloudWatch Agent comes installed on Amazon Linux 2 AMI and other AMIs. However, you may run into older AMIs or custom AMIs that don’t include the CloudWatch Agent. If you need to install, uninstall, or reinstall CloudWatch Agent on a Linux EC2 instance follow these steps. I’m going to assume you have an EC2 instance running.
Create an IAM Role
- Navigate to IAM and click Create Role.
- Select AWS Service for your trusted entity.
- Choose EC2 for your use case.
- Search for and select CloudWatchAgentServerPolicy and AmazonSSMManagedInstanceCore for your policies.
- Tag your role if you’d like.
- Give the role a name. DefaultEC2role would be descriptive.
- Click Create Role.
- Navigate to EC2 and attach DefaultEC2role to your instance.
Create an Association
- Navigate to the AWS Systems Manager service.
- Click on State Manager.
- Select the Create Association button.
- Give your association a name like CloudWatchAgentInstaller
- Select AWS-ConfigureAWSPackage for your Document
- For your Parameters to
- Action: Install; Installation Type: Uninstall and Reinstall; Name: AmazonCloudWatchAgent; Version: Default.
- Action: Uninstall; Installation Type: Uninstall and Reinstall; Name: AmazonCloudWatchAgent; Version: Default.
You can choose all EC2 targets or manually, by tag, or by resource group.
You can specify a CRON job to run the association to ensure the CloudWatch Agent is installed on newly created EC2 instances with targeted tags or resource groups. Or you can opt to run the association once.
Connect to EC2 with Session Manager
Now that we have AWS CloudWatch Agent installed, let’s check it out on our EC2 instance by using Session Manager.
- Navigate to the AWS EC2 service.
- Select your EC2 instance.
- Select Connect
- Select Session Manager.
- Click the Connect button
- In bash, run
systemctl status amazon-cloudwatch-agent
- “amazon-cloudwatch-agent.service – Amazon CloudWatch Agent” should be active.
- Click the Terminate button to close your session
In conclusion, you should now have a way to automatically (un)install AWS CloudWatch Agent on supported EC2 instances!