Terraform Demos
Copy and modify these pre-built Terraform configurations for your infrastructure.
AWSAWS CloudWatch Alarms and Dashboard
●AWSUploadedCreate CloudWatch alarms and a dashboard for monitoring.
By System · Security
▾
AWS CloudWatch Alarms and Dashboard
●AWSUploadedCreate CloudWatch alarms and a dashboard for monitoring.
By System · Security
main.tf · variables.tf · outputs.tf
Downloadresource "aws_cloudwatch_metric_alarm" "cpu_high" {
alarm_name = "${var.app_name}-cpu-high"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 2
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = 300
statistic = "Average"
threshold = 80
alarm_actions = [var.sns_topic_arn]
dimensions = {
InstanceId = var.instance_id
}
}
resource "aws_cloudwatch_dashboard" "main" {
dashboard_name = "${var.app_name}-dashboard"
dashboard_body = jsonencode({
widgets = [
{
type = "metric"
x = 0
y = 0
width = 12
height = 6
properties = {
metrics = [["AWS/EC2", "CPUUtilization", "InstanceId", var.instance_id]]
period = 300
stat = "Average"
region = var.aws_region
title = "CPU Utilization"
}
}
]
})
}
variable "app_name" {
type = string
}
variable "aws_region" {
type = string
default = "us-east-1"
}
variable "instance_id" {
type = string
}
variable "sns_topic_arn" {
type = string
}Troubleshooting & Solutions
Common Issues
- Alarm never triggers
- Too many false-positive alerts
- Dashboard shows no data
Solutions
- Confirm the dimensions block exactly matches the resource's actual metric dimensions
- Tune evaluation_periods/threshold based on real baseline traffic, not guesses
- Check the metric namespace and name are spelled exactly as AWS publishes them
Troubleshooting Steps
- Check alarm state history in the CloudWatch console
- Verify the SNS topic has a confirmed subscription
- Confirm the monitored resource is actually publishing that metric
- Check the region matches where the resource actually lives
Version compatibility: Terraform >= 1.0, AWS Provider >= 5.0