hashicorp/terraform-provider-aws

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

Aberta

#41.680 aberto em 5 de mar. de 2025

 (6 comentários) (0 reação) (0 responsável)Go (10.312 forks)github user discovery
enhancementgood first issueservice/cloudwatch

Métricas do repositório

Stars
 (11.045 estrelas)
Métricas de merge de PR
 (Mesclagem média 19d 21h) (251 fundiu PRs em 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

Guia do colaborador