Terraform AWS Cloudwatch Rule with Event Pattern incorrectly updated

Viewed 779

I'm trying ot configure cloudwatch event rule that'll tigger sns topic. I have been facing problem to trigger sns topic from cloutwatch rule with event pattern created from terraform. Below is my terraform code:

resource "aws_cloudwatch_event_rule" "s3-event" {
  name        = "s3-event"
  description = "Capture each AWS s3 event"

  event_pattern = <<EOF
{
  "source": ["aws.s3"],
  "detail-type": ["AWS API Call via CloudTrail"],
  "detail": {
    "eventSource": ["s3.amazonaws.com"],
    "eventName": ["CreateBucket"]
  }
}
EOF
}

After apply the checked the event pattern on aws ui it look like :

Event pattern
{
  "detail": {
    "eventName": ["CreateBucket"],
    "eventSource": ["s3.amazonaws.com"]
  },
  "detail-type": ["AWS API Call via CloudTrail"],
  "source": ["aws.s3"]
}

IT gave invocation failed error on creation of s3 bucket. then I resaved as from aws console:

{
  "source": ["aws.s3"],
  "detail-type": ["AWS API Call via CloudTrail"],
  "detail": {
    "eventSource": ["s3.amazonaws.com"],
    "eventName": ["CreateBucket"]
  }
}

then it passed.

Can someone suggest how I disable the terraform to rearrange event pattern.

1 Answers

Rearranging the pattern is not the resolution here when I creating the cloudwatch event from the terraform by default it doesn't create an access policy for sns but after editing the pattern from the AWS console access policy are getting created, so we have to define the access policy in terraform like below and attach to sns topic:

statement {
    effect  = "Allow"
    actions = ["SNS:Publish"]

    principals {
      type        = "Service"
      identifiers = ["events.amazonaws.com"]
    }
Related