Detailed Guide: Installing and Configuring AWS CloudWatch Agent on EC2 Instances

Pushkar Joshi
3 min readFeb 18, 2024

--

Monitoring your EC2 instances is essential for ensuring their health, performance, and security. AWS CloudWatch provides a comprehensive solution for collecting, monitoring, and analyzing metrics and logs from your EC2 instances. In this guide, we’ll walk through the detailed steps of installing the CloudWatch Agent on your EC2 instances and how to monitor these metrics in CloudWatch.

Purpose of This Blog

The purpose of this blog is to provide a detailed guide on how to monitor your EC2 instances using the AWS CloudWatch Agent. By following these steps, you’ll be able to collect crucial metrics and logs from your EC2 instances and gain insights into their performance and behavior. Monitoring with CloudWatch allows you to identify issues proactively, optimize resource utilization, and ensure the reliability of your applications hosted on EC2.

1. Installing the CloudWatch Agent

Before you begin, make sure you have an AWS account and access to the EC2 dashboard. Follow these steps to install the CloudWatch Agent on your Linux EC2 instance:

  1. Sign in to AWS Console: Navigate to the AWS Management Console and sign in to your account.
  2. Create an IAM Role: If you haven’t already, create an IAM role with permissions to run the CloudWatch Agent on your EC2 instance [6].
  3. Launch EC2 Instance: Launch a new EC2 instance with Amazon Linux 2 as the operating system. Make sure to assign the IAM role you created in the previous step.
  4. Connect to EC2 Instance: Use SSH to connect to your EC2 instance. Open a terminal and run the following command:
ssh -i <key_pair.pem> ec2-user@<public_dns>

Replace <key_pair.pem> with the path to your private key file and <public_dns> with your instance's public DNS [2].

Download the CloudWatch Agent Package: On your EC2 instance, download the CloudWatch Agent package using the following command:

wget https://s3.amazonaws.com/amazoncloudwatch-agent/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm

Install the Agent: Install the CloudWatch Agent package using the package manager appropriate for your Linux distribution. For Amazon Linux 2, use the following command:

sudo rpm -U ./amazon-cloudwatch-agent.rpm

Configure the Agent: After installation, configure the CloudWatch Agent by running the configuration wizard:

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

Start the Agent: Once configured, start the CloudWatch Agent service:

sudo systemctl start amazon-cloudwatch-agent

Verify Installation: Check the status of the CloudWatch Agent to ensure it’s running without errors:

sudo systemctl status amazon-cloudwatch-agent

2. Configuring CloudWatch Metrics and Logs

After installing the CloudWatch Agent, you can configure it to collect metrics and logs from your EC2 instance. Use the CloudWatch Agent configuration file (amazon-cloudwatch-agent.json) to specify the metrics, logs, and additional configurations.

  1. Edit Configuration File: Open the CloudWatch Agent configuration file for editing:
sudo vim /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json

Add Metrics and Logs: Customize the configuration file to include the metrics and logs you want to collect. Refer to the AWS documentation for detailed configuration options [5].

Restart Agent: After making changes, restart the CloudWatch Agent for the new configurations to take effect:

sudo systemctl restart amazon-cloudwatch-agent
  • Verify Configuration: Check the CloudWatch Agent logs and metrics in the CloudWatch console to verify that data is being collected successfully.

Conclusion

By following these detailed steps, you can easily run and install the CloudWatch Agent on your Linux EC2 servers, enabling comprehensive monitoring with AWS CloudWatch. Stay proactive in managing your infrastructure’s health and performance, ensuring a seamless experience for your users.

🌐 Sources

  1. Installing the CloudWatch agent — AWS Documentation
  2. Tutorial: Get started with Amazon EC2 Linux instances — AWS Documentation
  3. Installing and running the CloudWatch agent on your servers — AWS Documentation
  4. How To Install CloudWatch Agent on Linux EC2 Instances — Checkmate
  5. Collect metrics, logs, and traces with the CloudWatch agent — AWS Documentation
  6. Install and configure the unified CloudWatch agent to push metrics — AWS Knowledge Center

Please do suggest if any changes or corrections are required. You can reach me for any queries.

Creator :

Pushkar Joshi

If you like this blog and find it helpful then please do share and do not forget to clap and follow me on Medium & Linkedin here.

https://www.linkedin.com/in/jpushkar/

--

--

No responses yet