DEV Community

Cover image for Building Centralized EC2 Observability with Amazon CloudWatch
Narendra Sivangula
Narendra Sivangula

Posted on

Building Centralized EC2 Observability with Amazon CloudWatch

1. Introduction

Infrastructure monitoring needs more than instance-level CPU metrics. Operators also need visibility into operating-system memory, disk utilization, and application or system logs. Amazon CloudWatch provides a central place to collect metrics, store logs, build dashboards, and evaluate alarms.

 

This project provisions an EC2 instance with Terraform, installs and configures the CloudWatch Agent, sends selected operating-system metrics and system logs to CloudWatch, and creates alarms and a dashboard. An SNS topic is used as the alarm action target. No email subscription is configured by default.

Project: AWS Community Builders – Article 11\
Region: ap-south-2\
Infrastructure as Code: Terraform\
Workload: Amazon Linux 2023 EC2 instance\
Observability: CloudWatch Agent, CloudWatch Logs, Metrics, Alarms, Dashboard, and SNS

Implementation note: This article documents the lab implementation and its verification evidence. Screenshots are referenced below.
Repository Code: AWS Community Builder Repository

2. What problem are we solving?

Imagine you are responsible for a Linux EC2 instance running an application. The EC2 console can show basic instance-level information such as CPU utilization and network activity, but that alone does not give an operator enough information to troubleshoot many production issues.

 

For example:

  • The application becomes slow because the instance is running out of memory.
  • Disk utilization gradually increases until the application can no longer write files.
  • An application process reports errors, but the relevant log entries are available only on the instance.
  • An operator has to open multiple AWS pages to understand the health of one EC2 instance.
  • There is no proactive alert when resource utilization crosses an operational threshold.

 

The problem we are solving: How can we centralize EC2 operating-system metrics and application logs, visualize the instance's health, and configure threshold-based alerts without opening inbound SSH access?

 

We will use the CloudWatch Agent for memory, disk, and application-log collection; native EC2 metrics for CPU; CloudWatch dashboards and alarms for visibility and alerting; SNS as the alarm action target; and Systems Manager Session Manager for administrative access.

3. Objectives

  • Provision a small EC2 monitoring target using Terraform.
  • Administer the instance through Systems Manager Session Manager without inbound SSH.
  • Install and configure the CloudWatch Agent.
  • Collect memory and disk utilization metrics.
  • Publish selected system log entries to a CloudWatch log group.
  • Create CloudWatch alarms for CPU, memory, and disk utilization.
  • Build a CloudWatch dashboard for a consolidated operational view.
  • Route alarm state changes to an SNS topic.
  • Validate metric ingestion, logs, dashboard data, and alarm behavior.

4. Architecture

Architecture: Terraform → EC2 (Amazon Linux 2023); EC2 CloudWatch Agent → CloudWatch Metrics (memory/disk) and CloudWatch Logs (system log group); EC2 native CPU metric → CloudWatch; CloudWatch alarms → SNS topic; CloudWatch dashboard reading CPU, memory, and disk metrics.

5. Services used

Service Purpose
Amazon EC2 Linux host being monitored
Amazon CloudWatch Agent Collects OS-level metrics and log files
Amazon CloudWatch Metrics Stores CPU, memory, and disk metrics
Amazon CloudWatch Logs Centralizes selected system logs
CloudWatch Alarms Evaluates thresholds and changes alarm state
CloudWatch Dashboard Displays operational metrics together
Amazon SNS Receives alarm actions
AWS Systems Manager Provides Session Manager access
IAM Grants EC2 access to Systems Manager and CloudWatch
Terraform Provisions the infrastructure

6. Infrastructure design

Terraform discovers the default VPC and subnet, creates an EC2 instance profile, and provisions an Amazon Linux 2023 instance. The security group has no inbound rules. The instance role includes AmazonSSMManagedInstanceCore and CloudWatchAgentServerPolicy.

 

 

 

The CloudWatch Agent configuration collects:

  • mem_used_percent
  • disk_used_percent for the root filesystem
  • /var/log/article-11/application.log (a dedicated application log file created for this lab)

 

The log group uses a short retention period suitable for a lab. CloudWatch Agent metrics use the CWAgent namespace.

 

For this implementation, a dedicated application log file is created at /var/log/article-11/application.log and configured as the CloudWatch Agent log source. This avoids configuring the agent to tail a file that is absent on the selected image.

7. Deploy

From the project root:

./scripts/deploy.sh
Enter fullscreen mode Exit fullscreen mode

The script initializes Terraform, validates the configuration, displays a plan, and asks for explicit confirmation before applying.

Alternatively:

terraform -chdir=terraform init
terraform -chdir=terraform fmt -check
terraform -chdir=terraform validate
terraform -chdir=terraform plan
terraform -chdir=terraform apply
Enter fullscreen mode Exit fullscreen mode

Review the plan and regional charges before applying.

8. Verify the CloudWatch Agent

Connect through Session Manager:

aws ssm start-session \
  --target "$(terraform -chdir=terraform output -raw instance_id)" \
  --region ap-south-2
Enter fullscreen mode Exit fullscreen mode

On the instance:

sudo systemctl status amazon-cloudwatch-agent
sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl \
  -a status
Enter fullscreen mode Exit fullscreen mode

Inspect the agent log if metrics or logs do not appear:

sudo tail -n 80 /opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log
Enter fullscreen mode Exit fullscreen mode

Allow time for the first metric samples to arrive.

 

 

9. Validate metrics and logs

In the CloudWatch console, select the CWAgent namespace and inspect mem_used_percent and disk_used_percent. Confirm that the dimensions correspond to the deployed instance. Verify that datapoints are present; a metric appearing in list-metrics alone does not establish that recent datapoints are being published.

 

 

 

Inspect the /article-11/ec2/system log group and verify that the application log stream contains the test entries written to /var/log/article-11/application.log. Allow a short interval for the CloudWatch Agent to publish the events.

 

 

 

CLI examples:

aws cloudwatch list-metrics \
  --namespace CWAgent \
  --region ap-south-2
Enter fullscreen mode Exit fullscreen mode
aws logs describe-log-streams \
  --log-group-name /article-11/ec2/system \
  --region ap-south-2
Enter fullscreen mode Exit fullscreen mode

10. Alarms, dashboard, and SNS

The Terraform configuration creates:

  • A CPU utilization alarm using the native AWS/EC2 metric.
  • A memory utilization alarm using CWAgent.
  • A disk utilization alarm using CWAgent.
  • A dashboard with CPU, memory, and disk graphs.
  • An SNS topic used as the alarm action target.

 

 

 

 

 

 

 

No email subscription is created by default. The SNS topic and alarm actions can be verified independently of email delivery.

 

Testing boundary: An alarm definition existing is not the same as proving an alarm transitioned to ALARM. During implementation, record the observed state transition and SNS delivery status only if you actually trigger and verify them.

11. Operational validation

A practical validation should confirm:

  1. EC2 instance is running and reachable through Session Manager.

 

  1. CloudWatch Agent service is active.
  2. CWAgent memory and disk metrics are being published.
  3. The configured log group receives log events.
  4. Dashboard widgets show data for the correct instance.
  5. Alarm definitions use the expected metric, dimensions, threshold, and SNS action.
  6. If you run a controlled load test, record the actual alarm state and recovery to OK.

Do not run uncontrolled stress tests on shared or production instances. Use only the dedicated lab instance and stop load generation after the test.

12. Evidence checklist

# File Evidence
1 evidence/screenshots/01-ec2-instance.png EC2 instance details and running state
2 evidence/screenshots/02-cloudwatch-agent.png CloudWatch Agent service and configuration status
3 evidence/screenshots/03-cwagent-metrics.png Memory and disk metrics
4 evidence/screenshots/04-cloudwatch-logs.png Log group, application log stream, and received events
5 evidence/screenshots/05-cloudwatch-dashboard.png CPU, memory, and disk dashboard widgets
6 evidence/screenshots/06-cloudwatch-alarms.png CPU, memory, and disk alarm definitions and states
7 evidence/screenshots/07-sns-topic.png SNS topic and alarm action target
8 evidence/screenshots/08-ssm-access.png SSM managed-node status or Session Manager access

CLI evidence:

evidence/cli/
├── ec2-instance.json
├── cwagent-metrics.json
├── cloudwatch-alarms.json
├── log-streams.json
├── dashboard.json
└── sns-topics.json
Enter fullscreen mode Exit fullscreen mode

13. Security and cost considerations

The instance has no inbound SSH rule and is accessed using Session Manager. IAM permissions are attached to an instance role rather than embedding credentials in user data. The CloudWatch log group has retention configured to limit indefinite log storage.

EC2 runtime, EBS storage, CloudWatch custom metrics, log ingestion and storage, dashboards, alarms, and SNS usage may incur charges. This project is not guaranteed to be free-tier eligible. Check current pricing for ap-south-2 and remove lab resources after collecting evidence.

14. Cleanup

After saving evidence, run:

./scripts/destroy.sh
Enter fullscreen mode Exit fullscreen mode

Review the script before execution. It runs Terraform destroy for resources managed by this project. Confirm the AWS account and region first.

15. Conclusion

This project demonstrates a practical observability baseline for an EC2 workload: operating-system metrics and logs collected by the CloudWatch Agent, native EC2 metrics, CloudWatch alarms, a dashboard, and an SNS alarm target. It provides a repeatable Terraform foundation for monitoring and troubleshooting a Linux host.

Top comments (0)