Skip to main content

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​

NameTypeDescription
regionstringAWS region where the EC2 instance is running
instance_idstringThe ID of the EC2 instance to monitor
threshold_percentnumberCPU usage threshold that triggers the alarm (e.g., 80 for 80%)
evaluation_periodsnumberNumber of consecutive periods the threshold must be breached
alarm_namestringThe name to assign to the CloudWatch alarm
environmentstringTag to specify the deployment environment (e.g., dev, staging, prod)

πŸ“€ Outputs​

NameDescription
alarm_nameThe name of the CloudWatch alarm
alarm_arnThe 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.