hashicorp/terraform-provider-aws

[Enhancement]: `aws_cloudwatch_metric_alarm`: Add validation to prevent specifying both `metric` and `expression`

Open

#41,680 opened on Mar 5, 2025

 (6 comments) (0 reactions) (0 assignees)Go (10,312 forks)github user discovery
enhancementgood first issueservice/cloudwatch

Repository metrics

Stars
 (11,045 stars)
PR merge metrics
 (Avg merge 19d 21h) (251 merged PRs in 30d)

Description

Terraform Core Version

1.9.6

AWS Provider Version

4.67.0

Affected Resource(s)

aws_cloudwatch_metric_alarm->metric_query

Expected Behavior

metric_query can only have a metric block or an expression field but not both. This is not specified in the documentation and not caught with terraform plan

Actual Behavior

plan does not catch a badly configured resource.

Relevant Error/Panic Output Snippet

after `terraform apply`, the output is - 


aws_cloudwatch_metric_alarm.complex_alarm_1: Creating...
╷
│ Error: creating CloudWatch Metric Alarm (complex-alarm-1): No metric_query may have both `expression` and a `metric` specified

Terraform Configuration Files

sample resource configuration -

# High Resolution Alarms
# Complex Alarms with Multiple Metrics
resource "aws_cloudwatch_metric_alarm" "complex_alarm_1" {
  alarm_name          = "complex-alarm-1"
  alarm_description   = "Complex alarm with multiple metrics"
  comparison_operator = "GreaterThanThreshold"
  evaluation_periods  = 2
  threshold          = 90
  treat_missing_data = "missing"

  metric_query {
    id          = "high1"
    return_data = false
	expression  = "SELECT AVG(CPUUtilization) FROM AWS/EC2 GROUP BY InstanceId"
	period = 10
    metric {
      metric_name = "CPUUtilization"
      namespace   = "AWS/EC2"
      period     = 10
      stat       = "Average"
    }
  }
}

Steps to Reproduce

run terraform plan for the above resource

Debug Output

No response

Panic Output

No response

Important Factoids

No response

References

No response

Would you like to implement a fix?

None

Contributor guide