hashicorp/terraform-provider-aws

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

Aperta

#41.680 aperta il 5 mar 2025

 (6 commenti) (0 reazioni) (0 assegnatari)Go (10.312 fork)github user discovery
enhancementgood first issueservice/cloudwatch

Metriche repository

Star
 (11.045 stelle)
Metriche merge PR
 (Merge medio 19g 21h) (251 PR mergiate in 30 g)

Descrizione

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

Guida contributor