hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.71k stars 9.06k forks source link

[Bug]: aws_cloudwatch_log_metric_filter creation fails with "The specified filter pattern does not support dimensions" #28881

Open tomcart90 opened 1 year ago

tomcart90 commented 1 year ago

Terraform Core Version

v1.3.5

AWS Provider Version

4.50.0

Affected Resource(s)

aws_cloudwatch_log_metric_filter

Expected Behavior

Creating a aws_cloudwatch_log_metric_filter resource with both a valid pattern and dimensions should have succeeded.

Actual Behavior

When creating a aws_cloudwatch_log_metric_filter with dimensions I get the following error: InvalidParameterException: The specified filter pattern does not support dimensions

Relevant Error/Panic Output Snippet

│ Error: putting CloudWatch Logs Metric Filter (BitDefenderUpdaterIssueWatcher): InvalidParameterException: The specified filter pattern does not support dimensions
│
│   with aws_cloudwatch_log_metric_filter.issue_watcher,
│   on alarms.tf line 80, in resource "aws_cloudwatch_log_metric_filter" "issue_watcher":
│   80: resource aws_cloudwatch_log_metric_filter issue_watcher {
│

Terraform Configuration Files

locals {
  filtered_log_lines = [
    "Server has closed the connection",
    "Incremental update is not possible",
  ]
}

resource aws_cloudwatch_log_metric_filter issue_watcher {
  name           = "FilterName"
  pattern        = join(" ", [for line in local.filtered_log_lines : "?\"${line}\""])
  log_group_name = "LogGroupName"

  metric_transformation {
    name          = "LoggedIssueCount"
    namespace     = "LogGroupIssueWatcher"
    value         = "1"
    dimensions    = {
      stackName = "stack1"
    }
  }
}

Steps to Reproduce

Run a terraform apply with the above terraform configuration.

Debug Output

No response

Panic Output

No response

Important Factoids

No response

References

I believe this is essentially a duplicate of https://github.com/hashicorp/terraform-provider-aws/issues/22105, though as there has been no response on that issue I figured I'd raise another.

Would you like to implement a fix?

None

github-actions[bot] commented 1 year ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue

marmor7 commented 1 year ago

Got hit by this issue, and apparently, this is not a bug but intended behavior.

When creating metric filters in CloudWatch the specific pattern you use defines whether or not you can use dimensions. The only 2 types of patterns permitted to have dimensions are JSON and space-delimited.

The phrase "space-delimited" is a bit confusing in this context, it refers to patterns that look like this:

[ip, server, username, timestamp, request, status_code, bytes > 1000]

i.e. begin and end with [] and define each field on the row in between.

Read more here: https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/FilterAndPatternSyntax.html#extract-log-event-values Also, you can use the console to being creating a metric filter, and then use the "Test Pattern" button to try and get the correct results, once you do, click on next and check if the UI displays a section at the bottom to add dimensions or not.