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

documentation example for aws_cloudwatch_composite_alarm is incorrect #19575

Open
paravz opened this issue May 28, 2021 · 1 comment
Open

documentation example for aws_cloudwatch_composite_alarm is incorrect #19575

paravz opened this issue May 28, 2021 · 1 comment
Labels
bug Addresses a defect in current functionality. documentation Introduces or discusses updates to documentation. service/cloudwatch Issues and PRs that pertain to the cloudwatch service.

Comments

@paravz
Copy link

paravz commented May 28, 2021

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Terraform CLI and Terraform AWS Provider Version

$  terraform -v
Terraform v0.15.4
on linux_amd64
+ provider registry.terraform.io/hashicorp/aws v3.42.0

Affected Resource(s)

  • aws_cloudwatch_composite_alarm

Terraform Configuration Files

locals {
  alarm_rule_with_newlines = <<-EOF
    ALARM(${aws_cloudwatch_metric_alarm.alarm-test1.alarm_name}) OR
    ALARM(${aws_cloudwatch_metric_alarm.alarm-test2.alarm_name})
  EOF
}

resource "aws_cloudwatch_metric_alarm" "alarm-test1" {
  alarm_name          = "alarm-test1"
  comparison_operator = "GreaterThanThreshold"
  evaluation_periods  = "1"
  metric_name         = "ApproximateNumberOfMessagesVisible"
  namespace           = "AWS/SQS"
  period              = "300"
  statistic           = "Sum"
  threshold           = "0.0"
  treat_missing_data  = "ignore"
  alarm_description   = "Queue backlog exceeded"
  dimensions = {
    QueueName = "dummy-queue"
  }
}

resource "aws_cloudwatch_metric_alarm" "alarm-test2" {
  alarm_name          = "alarm-test2"
  comparison_operator = "GreaterThanThreshold"
  evaluation_periods  = "1"
  metric_name         = "ApproximateNumberOfMessagesVisible"
  namespace           = "AWS/SQS"
  period              = "300"
  statistic           = "Sum"
  threshold           = "0.0"
  treat_missing_data  = "ignore"
  alarm_description   = "Queue backlog exceeded"
  dimensions = {
    QueueName = "dummy-queue"
  }
}

resource "aws_cloudwatch_composite_alarm" "composite-alarm-test" {
  alarm_description = "Test/Dummy composite alarm"
  alarm_name        = "test-composite-alarm"

  # fails
  alarm_rule = <<EOF
ALARM(${aws_cloudwatch_metric_alarm.alarm-test1.alarm_name}) OR
ALARM(${aws_cloudwatch_metric_alarm.alarm-test2.alarm_name})
EOF

  # works
  #alarm_rule = trimspace(replace(local.alarm_rule_with_newlines, "/\n+/", " ")) 
}

Debug Output

$  terraform apply

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_cloudwatch_composite_alarm.composite-alarm-test will be created
  + resource "aws_cloudwatch_composite_alarm" "composite-alarm-test" {
      + actions_enabled   = true
      + alarm_description = "Test/Dummy composite alarm"
      + alarm_name        = "test-composite-alarm"
      + alarm_rule        = <<-EOT
            ALARM(alarm-test1) OR
            ALARM(alarm-test2)
        EOT
      + arn               = (known after apply)
      + id                = (known after apply)
      + tags_all          = (known after apply)
    }

  # aws_cloudwatch_metric_alarm.alarm-test1 will be created
  + resource "aws_cloudwatch_metric_alarm" "alarm-test1" {
      + actions_enabled                       = true
      + alarm_description                     = "Queue backlog exceeded"
      + alarm_name                            = "alarm-test1"
      + arn                                   = (known after apply)
      + comparison_operator                   = "GreaterThanThreshold"
      + dimensions                            = {
          + "QueueName" = "dummy-queue"
        }
      + evaluate_low_sample_count_percentiles = (known after apply)
      + evaluation_periods                    = 1
      + id                                    = (known after apply)
      + metric_name                           = "ApproximateNumberOfMessagesVisible"
      + namespace                             = "AWS/SQS"
      + period                                = 300
      + statistic                             = "Sum"
      + tags_all                              = (known after apply)
      + threshold                             = 0
      + treat_missing_data                    = "ignore"
    }

  # aws_cloudwatch_metric_alarm.alarm-test2 will be created
  + resource "aws_cloudwatch_metric_alarm" "alarm-test2" {
      + actions_enabled                       = true
      + alarm_description                     = "Queue backlog exceeded"
      + alarm_name                            = "alarm-test2"
      + arn                                   = (known after apply)
      + comparison_operator                   = "GreaterThanThreshold"
      + dimensions                            = {
          + "QueueName" = "dummy-queue"
        }
      + evaluate_low_sample_count_percentiles = (known after apply)
      + evaluation_periods                    = 1
      + id                                    = (known after apply)
      + metric_name                           = "ApproximateNumberOfMessagesVisible"
      + namespace                             = "AWS/SQS"
      + period                                = 300
      + statistic                             = "Sum"
      + tags_all                              = (known after apply)
      + threshold                             = 0
      + treat_missing_data                    = "ignore"
    }

Plan: 3 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_cloudwatch_metric_alarm.alarm-test1: Creating...
aws_cloudwatch_metric_alarm.alarm-test2: Creating...
aws_cloudwatch_metric_alarm.alarm-test2: Creation complete after 1s [id=alarm-test2]
aws_cloudwatch_metric_alarm.alarm-test1: Creation complete after 1s [id=alarm-test1]
aws_cloudwatch_composite_alarm.composite-alarm-test: Creating...
╷
│ Error: error creating CloudWatch Composite Alarm (test-composite-alarm): ValidationError: AlarmRule must not contain leading or trailing whitespace or be null
│       status code: 400, request id: a93cd490-ae43-40b4-a940-1dfea2b4a284
│
│   with aws_cloudwatch_composite_alarm.composite-alarm-test,
│   on composite-alarm.tf line 34, in resource "aws_cloudwatch_composite_alarm" "composite-alarm-test":
│   34: resource "aws_cloudwatch_composite_alarm" "composite-alarm-test" {
│

Expected Behavior

Composite Alarm is created as per documentation example https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_composite_alarm#example-usage

Actual Behavior

Error from AWS on apply, likely because of newlines in heredoc:
ValidationError: AlarmRule must not contain leading or trailing whitespace or be null

Steps to Reproduce

  1. terraform apply
@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 May 28, 2021
@anGie44 anGie44 added documentation Introduces or discusses updates to documentation. bug Addresses a defect in current functionality. and removed needs-triage Waiting for first response or review from a maintainer. labels Jul 28, 2021
@jay-dizzale
Copy link

Hi there, the issue still persists. EOF<< will add additional whitspaces, which then trigger the error

Error: error creating CloudWatch Composite Alarm (test-composite-alarm): ValidationError: AlarmRule must not contain leading or trailing whitespace or be null

I had to add the alarmnames to a list and joint with 'or':
alarm_rule = join(" OR ", tolist(["test-alarm", "test-alarm-2"])

I think the provider should remove the whitespaces.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Addresses a defect in current functionality. documentation Introduces or discusses updates to documentation. service/cloudwatch Issues and PRs that pertain to the cloudwatch service.
Projects
None yet
Development

No branches or pull requests

3 participants