cloudposse / terraform-aws-cloudwatch-events

Terraform Module for provisioning CloudWatch Events rules connected with targets.
https://cloudposse.com/accelerate
Apache License 2.0
14 stars 16 forks source link

Invalid/Missing cloudwatch_event_rule_pattern variable type #33

Open noizo opened 5 months ago

noizo commented 5 months ago

Describe the Bug

i'm defining event patter:

module "cloudwatch_event" {
  source = "cloudposse/cloudwatch-events/aws"
  version = "0.8.0"

  name          = var.name
  namespace     = var.namespace
  tenant        = var.tenant
  environment   = var.environment
  stage         = var.stage

  cloudwatch_event_rule_description = var.cloudwatch_event_rule_description
  cloudwatch_event_rule_pattern = {
    "source" : ["aws.medialive"]
  }
  cloudwatch_event_target_arn = module.sns.sns_topic.arn
}

when i run plan:

  # aws_cloudwatch_event_rule.this will be created
  + resource "aws_cloudwatch_event_rule" "this" {
      + arn            = (known after apply)
      + description    = "MediaLive Events"
      + event_bus_name = "default"
      + event_pattern  = "\"{\\\"source\\\":[\\\"aws.medialive\\\"]}\""
      + force_destroy  = false
      + id             = (known after apply)
      + is_enabled     = true
      + name           = "medialive-events"
      + name_prefix    = (known after apply)
      + tags_all       = (known after apply)
    }

When i run apply it can't apply string in place of json object.

│ Error: creating EventBridge Rule (medialive-events): operation error EventBridge: PutRule, https response error StatusCode: 400, RequestID: 4c39b1c1-f975-41ff-96fb-b436068e280b, InvalidEventPatternException: Event pattern is not valid. Reason: Filter is not an object
│  at [Source: (String)""{\"source\":[\"aws.medialive\"]}""; line: 1, column: 2]

Expected Behavior

Manipulating with jsonencode function gave no result until i added variable type = any as listed below

  # aws_cloudwatch_event_rule.this will be created
  + resource "aws_cloudwatch_event_rule" "this" {
      + arn            = (known after apply)
      + description    = "prd MediaLive Events"
      + event_bus_name = "default"
      + event_pattern  = jsonencode(
            {
              + source = [
                  + "aws.medialive",
                ]
            }
        )
      + force_destroy  = false
      + id             = (known after apply)
      + name           = "prd-medialive-events"
      + name_prefix    = (known after apply)
      + state          = "ENABLED"
      + tags_all       = (known after apply)
    }

Steps to Reproduce

FIX:

variable "cloudwatch_event_rule_pattern" {
  description = "Event pattern described a HCL map which will be encoded as JSON with jsonencode function. See full documentation of CloudWatch Events and Event Patterns for details. http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/CloudWatchEventsandEventPatterns.html"
  type        = any
}

Type any allow terraform to dynamically use any incoming type. If it's not provided, default is string.

Screenshots

No response

Environment

No response

Additional Context

No response

felipevacar commented 2 months ago

Fixing it would be very usefull, I am facing the same issue with Terragrunt