Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sursingh-apptio opened this issue Mar 5, 2025 · 3 comments
Labels
enhancement Requests to existing resources that expand the functionality or scope. good first issue Call to action for new contributors looking for a place to start. Smaller or straightforward issues. service/cloudwatch Issues and PRs that pertain to the cloudwatch service.

Comments

@sursingh-apptio
Copy link

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

@sursingh-apptio sursingh-apptio added the bug Addresses a defect in current functionality. label Mar 5, 2025
Copy link

github-actions bot commented Mar 5, 2025

Community Guidelines

This comment is added to every new Issue to provide quick reference to how the Terraform AWS Provider is maintained. Please review the information below, and thank you for contributing to the community that keeps the provider thriving! 🚀

Voting for Prioritization

  • Please vote on this Issue by adding a 👍 reaction to the original post to help the community and maintainers prioritize it.
  • Please see our prioritization guide for additional information on how the maintainers handle prioritization.
  • Please do not leave +1 or other comments that do not add relevant new information or questions; they generate extra noise for others following the Issue and do not help prioritize the request.

Volunteering to Work on This Issue

  • If you are interested in working on this issue, please leave a comment.
  • If this would be your first contribution, please review the contribution guide.
  • For new resources and data sources, use skaff to generate scaffolding with comments detailing common expectations.

@github-actions github-actions bot added needs-triage Waiting for first response or review from a maintainer. service/cloudwatch Issues and PRs that pertain to the cloudwatch service. labels Mar 5, 2025
@justinretzolk
Copy link
Member

Hey @sursingh-apptio 👋 Thank you for taking the time to raise this! We consider adding additional functionality to existing resources (in this case, an additional validation) to be an enhancement rather than a bug. With that in mind, I'm going to make a slight modification to this issue. No further information is needed from you at this point, I just like to let people know before I make these kinds of modifications.

As a note, the documentation for the resource does have a note that calls this out:

NOTE:
You must specify either metric or expression. Not both.

@justinretzolk justinretzolk changed the title [Bug]: terraform plan does not catch bad configuration for aws_cloudwatch_metric_alarm resource. [Enhancement]: aws_cloudwatch_metric_alarm: Add validation to prevent specifying both metric and expression Mar 6, 2025
@justinretzolk justinretzolk added enhancement Requests to existing resources that expand the functionality or scope. good first issue Call to action for new contributors looking for a place to start. Smaller or straightforward issues. and removed bug Addresses a defect in current functionality. needs-triage Waiting for first response or review from a maintainer. labels Mar 6, 2025
@sursingh-apptio
Copy link
Author

Thank you @justinretzolk for cleaning it up. I'll try and see if I can tackle this myself :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Requests to existing resources that expand the functionality or scope. good first issue Call to action for new contributors looking for a place to start. Smaller or straightforward issues. service/cloudwatch Issues and PRs that pertain to the cloudwatch service.
Projects
None yet
Development

No branches or pull requests

2 participants