CodexBloom - Programming Q&A Platform

Terraform: Issues with Setting Up Custom CloudWatch Alarms for ECS Service Metrics

👀 Views: 372 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-12
terraform aws cloudwatch hcl

I've searched everywhere and can't find a clear answer. I'm trying to set up a custom CloudWatch alarm for my ECS service metrics using Terraform, but I'm running into issues with the alarm not triggering as expected. I have the following configuration: ```hcl resource "aws_cloudwatch_metric_alarm" "ecs_cpu_high" { alarm_name = "ECS High CPU Alarm" comparison_operator = "GreaterThanThreshold" evaluation_periods = "1" metric_name = "CPUUtilization" namespace = "AWS/ECS" period = "60" statistic = "Average" threshold = "80" alarm_description = "This metric monitors high CPU utilization for ECS" dimensions = { ClusterName = aws_ecs_cluster.my_cluster.name ServiceName = aws_ecs_service.my_service.name } alarm_actions = [aws_sns_topic.my_alerts.arn] } ``` Even though I can see CPU utilization data in the CloudWatch console, the alarm remains in the `INSUFFICIENT_DATA` state. I've confirmed that the ECS service is running and the metrics are being collected. I've also tried adjusting the `period` to `300` seconds, but that didn't help either. Additionally, I've checked the IAM permissions for the Terraform execution role, and it has `cloudwatch:PutMetricAlarm` permissions. I even tried adding a `depends_on` clause in case of any resource dependency issues: ```hcl depends_on = [aws_ecs_service.my_service] ``` Is there something I'm missing regarding the dimensions or the metric namespace that could be causing this alarm not to trigger? Any insight would be greatly appreciated! Has anyone dealt with something similar? I'm using Hcl latest in this project.