Introduction
Yesterday, you accomplished a significant milestone by setting up Grafana locally. Kudos to you! Now, let’s take it up a notch. In today’s session, we’ll connect Linux and Windows EC2 instances to Grafana, enabling you to monitor their metrics effectively. By the end of this blog, you’ll have a functional setup to visualize and analyze server performance metrics in real time.
What is Grafana, and Why Connect EC2 Instances?
Grafana is a powerful open-source tool for visualizing and analyzing metrics. It supports multiple data sources, making it a go-to choice for server monitoring. By connecting EC2 instances to Grafana, you can:
Track server performance metrics like CPU usage, memory, disk I/O, and network traffic.
Identify bottlenecks in real-time.
Gain actionable insights to optimize your infrastructure.
How to Connect EC2 Instances with Grafana
Here’s a step-by-step guide to connecting both Linux and Windows EC2 instances to Grafana.
Step 1: Prerequisites
Before diving in, ensure you have the following:
Grafana installed and running locally or on a server (Refer to Day 73).
AWS EC2 instances ready – one Linux and one Windows instance.
Access to AWS CloudWatch – Grafana integrates seamlessly with CloudWatch for EC2 metrics.
IAM Role with CloudWatch read permissions attached to your EC2 instances.
Step 2: Set Up AWS CloudWatch Integration
Create an IAM Role:
Navigate to the IAM Console in AWS.
Create a new role for EC2 and attach the policy CloudWatchAgentServerPolicy.
Attach this role to both your Linux and Windows EC2 instances.
Enable CloudWatch Logs:
- Ensure your instances have CloudWatch logs enabled to stream metrics.
Step 3: Install the CloudWatch Agent
On Linux EC2:
SSH into your Linux instance.
Install the CloudWatch Agent:
sudo yum install amazon-cloudwatch-agent
Configure the agent:
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-config-wizard
Choose EC2 Instance as the source.
Enable metrics for CPU, memory, and disk.
Start the agent:
sudo systemctl start amazon-cloudwatch-agent
On Windows EC2:
RDP into your Windows instance.
Download the CloudWatch Agent MSI package from the AWS website.
Run the installer and follow the on-screen instructions.
Configure the agent using the CloudWatch Agent Wizard.
Step 4: Configure Grafana to Pull CloudWatch Data
Add a Data Source in Grafana:
Log in to Grafana.
Navigate to Configuration > Data Sources.
Select CloudWatch.
Set Up CloudWatch in Grafana:
Enter your AWS Access Key and Secret Key.
Choose the appropriate AWS Region.
Save the configuration.
Create a Dashboard:
Go to Dashboards > New Dashboard.
Add a panel and select CloudWatch as the data source.
Configure the panel to display metrics like CPUUtilization, DiskReadOps, and NetworkIn.
Step 5: Monitor Linux and Windows EC2 Metrics
For Linux, monitor:
CPU usage
Memory consumption
Disk I/O performance
For Windows, track:
Disk utilization
Network traffic
Active processes
Use Grafana’s powerful visualization tools to create graphs, alerts, and thresholds.
Why Monitor EC2 Instances with Grafana?
Monitoring your EC2 instances ensures your infrastructure is:
Resilient – Detect and resolve issues before they impact users.
Scalable – Identify trends to plan for future capacity.
Efficient – Optimize resources to reduce costs.
Hands-On Task
Follow the steps to connect your Linux and Windows EC2 instances to Grafana.
Create a dashboard with at least three panels for each instance:
CPU usage
Disk I/O
Network metrics
Set up alerts for high CPU usage or low memory.
Conclusion
By connecting your EC2 instances to Grafana, you’ve taken a significant step in mastering server monitoring. This integration not only provides real-time insights but also empowers you to make data-driven decisions. Keep experimenting with different metrics and visualizations to get the most out of Grafana.