CloudWatch Alarm - CPU Utilization
π¦ Overviewβ
This Terraform module provisions a CloudWatch alarm to monitor CPU utilization for a specified EC2 instance.
Itβs ideal for proactive monitoring of performance bottlenecks and workload surges in development, staging, or production environments.
β Key Featuresβ
- Monitors CPU usage and triggers alerts above a defined threshold
- Customizable evaluation period, threshold, and comparison operator
- Compatible with SNS for optional notifications
- Tagged by environment for visibility and filtering
πΌ Use Casesβ
- Alerting for high CPU usage on critical EC2 workloads
- Triggering auto-scaling based on compute pressure
- Monitoring under-provisioned instances in dev/test/staging
- Integrating alarms into broader observability pipelines
π₯ Input Variablesβ
| Name | Type | Description |
|---|---|---|
| region | string | AWS region where the EC2 instance is running |
| instance_id | string | The ID of the EC2 instance to monitor |
| threshold_percent | number | CPU usage threshold that triggers the alarm (e.g., 80 for 80%) |
| evaluation_periods | number | Number of consecutive periods the threshold must be breached |
| alarm_name | string | The name to assign to the CloudWatch alarm |
| environment | string | Tag to specify the deployment environment (e.g., dev, staging, prod) |
π€ Outputsβ
| Name | Description |
|---|---|
| alarm_name | The name of the CloudWatch alarm |
| alarm_arn | The ARN of the provisioned CloudWatch alarm |
π Deployment Readinessβ
- β Compatible with Terraform 1.0+
- β Tested with AWS provider 5.x
- π§ͺ Requires an existing EC2 instance in the target region
π οΈ Example Usageβ
module "cloudwatch-alarm-cpu-deployment" {
source = "git::ssh://git@github.com/archiphire/aws-level-1-modules.git//monitoring/cloudwatch-alarm-cpu?ref=v1.0.0"
region = "us-east-1"
instance_id = "i-0123456789abcdef0"
threshold_percent = 80
evaluation_periods = 2
alarm_name = "high-cpu-alert"
environment = "prod"
}
terraform init
terraform plan
terraform apply
or
tofu init
tofu plan
tofu apply
π§Ή Cleanup Optionsβ
terraform destroy
or
tofu destroy
Manual Deletion (production):
aws cloudwatch delete-alarms --alarm-names high-cpu-alert
π‘οΈ Notesβ
This is a Level 1 monitoring module. For production use, consider pairing with:
sns-topic-basic for alert notifications
Auto Scaling policies triggered by alarm actions
π Deployment Package Noticeβ
This module is part of the Level 1 AWS License Tier. To access the full deployment package and source code, subscribe to Archiphire.