serverless: Can't create two lambdas using the same eventBus

Bug Report

Description

Create two lambda functions using the same eventBus on EventBridge service gives an error that eventBus cannot be created.

  1. What did you do? Try to create two lambda functions using the same event bus

  2. What happened? Gives an error failed to create resource already exists

  3. What should’ve happened? Create the two lambdas using the same eventBus

  4. What’s the content of your serverless.yml file?

service: my-service

provider:
  name: aws
  stage: ${opt:stage}
  runtime: go1.x
  region: us-east-1

package:
  exclude:
  stage: ${opt:stage}
    - ./**
  include:
    - ./bin/**

functions:
  plans-create:
    handler: bin/eventbridge/plans/create
    events:
      - eventBridge:
          eventBus: mybus-${opt:stage}
          pattern:
            source:
            - prod.admin.plans
            detail-type:
            - plan-created

  plans-update:
    handler: bin/eventbridge/plans/update
    events:
      - eventBridge:
          eventBus: mybus-${opt:stage}
          pattern:
            source:
              - prod.admin.plans
            detail-type:
              - plan-updated

Failed to create resource. Event bus mybus-dev already exists.

Similar or dependent issues:

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 5
  • Comments: 20 (3 by maintainers)

Most upvoted comments

Ok find a way to workaround this, like the following example, but it needs at least the arn:aws to work

custom:
  accountNumber: 123456789
  eventBus: arn:aws:events:us-east-1:${self:custom.accountNumber}:event-bus/mybus-${opt:stage}

functions:
  plans-create:
    handler: bin/eventbridge/plans/create
    events:
      - eventBridge:
          eventBus: ${self:custom.eventBus}
          pattern:
            source:
            - prod.admin.plans
            detail-type:
            - plan-created

resources:
  Resources:
    EventBridgeBus:
      Type: AWS::Events::EventBus
      Properties:
          Name: mybus-${opt:stage}

@TreeMan360 Got the same EventBus.startsWith is not a function error with:

    events:
      - eventBridge:
          eventBus:
            Fn::GetAtt: [EventBus, Arn]

The event bus is being created fine when using resources, but I can’t reference with GetAtt.

So far I’m having 2 issues:

  1. Creating 2 lambdas using same eventBus with a custom name (according to serverless documentation it automatically creates event bus in such case) raises Cloud Formation error that EventBus is already created.
  2. Creating custom eventBus in resources section I can’t reference it with !GetAtt nor Fn::GetAtt.

I can work this around with the custom attribute hardcoding the ARN as Eduardo did in this thread, but would be good to do it in more declarative way rather than hardcoding ARN’s.

@XM37 I have some work in progress that should address this attached to #7063. I have have had to pause on finishing/testing it at the moment though.

I know this framework is “free”, but I would have thought having 2 Lambdas on the same Event Bus was a very common use-case (and test case). Building an event bus for a single subscriber would be the edge-case here. Unfortunately I don’t have the knowledge to create a PR to fix this issue. But I sure would appreciate it - as would most people that use EventBridge - if this issue could be addressed. Thanks 🙏 .

This is still an ongoing issue. Using the workaround from @eduardonunesp got us unblocked.\

I use EventBridge extensively using Serverless and have been for some time and I don’t have any issues as long as I put the EventBridge definition in another stack.

I create an infrastructure stack (infrastructure.yml) and then I reference the Event Bus Name and Arn as outputs of that stack. Then the bus already exists and I have no issues.

In reality most of my projects have several serverless stacks so having an infrastructure stack normally becomes necessary for me at some point anyway. And I prefer it like this.

There is definitely an issue using EB in a single stack. I tried it again a couple of weeks ago and it does not work properly. So yes it should be addressed, but for now just split your stacks and everything will be awesome 🤩.

I did elude to this ages ago above. I hope this helps someone.