4

I can't find the documentation or an example Terraform module online.

How do I create a custom Event Bus in AWS Event Bridge?

zenbeni
  • 7,019
  • 3
  • 29
  • 60
Madhav Shenoy
  • 798
  • 12
  • 32
  • 1
    This isn't supported yet. We had to use the default Event Bus or create it with the AWS CLI or Console. EventBridge has a couple of serious IAM gaps right now: you can't restrict what buses an IAM principal can publish events too and it uses a Service principal instead of a Service Linked Role principal to access things like KMS keys used to encrypt the buses. It likely needs another 6 months to finish baking... – Alain O'Dea May 10 '20 at 17:22

3 Answers3

4

As of this writing, creating an EventBridge Event Bus isn't supported by the Terraform Provider for AWS yet.

We had to use the default Event Bus or create it with the AWS CLI or Console.

Caveats: EventBridge has a couple of serious IAM gaps right now: you can't restrict what buses an IAM principal can publish events too and it uses a Service principal instead of a Service Linked Role principal to access things like KMS keys used to encrypt the buses.

You can use a null_resource provisioner as a workaround for the missing provider resource (this assumes you are using environment variables or an IAM instance profile to authenticate your AWS provider):

resource "null_resource" "custom_event_bus" {
  triggers = {
    event_bus_name = var.event_bus_name
  }

  provisioner "local-exec" {
    command = "aws events create-event-bus --name ${var.event_bus_name}'"
  }
}

If you are using a named AWS configuration profile instead of environment variables, you'll need to specify that with --profile profile_name the same as you would if you ran it at your shell.

Alain O'Dea
  • 21,033
  • 1
  • 58
  • 84
4

With a recent update to the AWS Terraform Provider, the EOF template_body style mentioned in another answer is no longer the preferred way of specifying a CloudFormation stack. Here is an example snippet of code using the new STACK declaration style that accomplishes the same thing (provisions a custom EventBridge bus):

resource "aws_cloudformation_stack" "eventbridge_bus" {
  name = "eventbridge-bus"

  template_body = <<STACK
{
  "Resources" : {
    "bus" : {
      "Type" : "AWS::Events::EventBus",
      "Properties" : {
        "Name": "bus-name"
      }
    }
  }
}
STACK
}
  • How about to create a rule fot a custom event bus? it is going to be created in the aws_cloudformation_stack resource as well? or aws cli with null_resource local execution – Jorge Ivansevick Jul 27 '22 at 15:32
3

There is a ticket refering to the non support of event bridge in terraform: https://github.com/terraform-providers/terraform-provider-aws/issues/9330

By quoting github user https://github.com/mwarkentin who deserves the credit for the following snippet, there is a cloudformation in terraform hack to enable the declaration of an event bridge in terraform :

resource "aws_cloudformation_stack" "eventbridge_bus" {   
  name = "eventbridge-bus"
  template_body = <<EOF 
Resources:
  EventBus:
    Type: AWS::Events::EventBus
    Properties:
      Name: bus-name
EOF
}
zenbeni
  • 7,019
  • 3
  • 29
  • 60
  • This doesnt work. This is what I get when i do a terraform init with this configuration https://gist.github.com/mshenoy83/17d7773e2b5f1c2cf94b7989e2410989 – Madhav Shenoy Jul 21 '20 at 00:53
  • Really strange, but some character sequence in there throws an error. HOWEVER-- Copy/pasting the original from the github issue linked in the post *does* work. Visually identical. – ericpeters0n Jul 25 '20 at 02:00