Event bus name not registering when attempting to connect eventbridge and lambda using terraform

Viewed 93

I am attempting to create an Eventbridge that will get notifications from Datadog, and trigger a lambda function to store the notifications to an S3 bucket. This is all going to be done through Terraform. The following is the code I have written:

###################################
# Eventbridge Integration to Lambda
###################################
data "aws_cloudwatch_event_source" "datadog_event_source" {
  name_prefix = var.spog_event_bus_name   # aws.partner/datadog.com/my_eventbus
}

resource "aws_cloudwatch_event_bus" "datadog_event_bus" {
  name              = data.aws_cloudwatch_event_source.datadog_event_source.name
  event_source_name = data.aws_cloudwatch_event_source.datadog_event_source.name
}

resource "aws_cloudwatch_event_rule" "spog_cloudwatch_rule" {
  name        = "spog_cloudwatch_rule"
  event_bus_name = aws_cloudwatch_event_bus.datadog_event_bus.name
  event_pattern = <<EOF
  {
    "account": [
      "${var.aws_account_id}"
    ]
  }
EOF
}

resource "aws_cloudwatch_event_target" "spog_cloudwatch_event_target" {
  rule      = aws_cloudwatch_event_rule.spog_cloudwatch_rule.name
  target_id = aws_lambda_function.write_datadog_events.function_name
  arn       = aws_lambda_function.write_datadog_events.arn
}

resource "aws_lambda_permission" "spog_allow_cloudwatch" {
  statement_id = "AllowExecutionFromCloudWatch"
  action        = "lambda:InvokeFunction"
  function_name = aws_lambda_function.write_datadog_events.function_name
  principal     = "events.amazonaws.com"
  source_arn    = aws_cloudwatch_event_rule.spog_cloudwatch_rule.arn
}

Here, the write_datadog_events is the lambda function to store the notifications.

The build succeeds, but when I try to apply the plan, I get an error saying that "validationException: EventBus name starting with 'aws.' is not valid". From inspecting the aws console, it seems that the actual eventbridge and rule are created successfully, but the event bus name is not registered properly on the eventbridge. The event bus name only says default, and I thought by changing the event_bus_name value within aws_cloudwatch_event_rule, it would not be default, but I was wrong.

Can anyone help me out with this. The lambda function itself is not wrong (since I ran a test case on it), it seems that the core issue is eventbridge not registering my event bus name. Also, although the rule is generated, the event bus is never generated.

Thanks for your help.

0 Answers
Related