EventBridge trigger on localstack defaults to default event bus even though a new one is passed

Hi, I’m having trouble with setting up Serverless function with EventBridge trigger on custom event bus on localstack. It looks like even though I set custom event bus name as described in documentation of serverless and the event bus is created and event target has correct event bus set. The event rule still points to default event bus. I would appreciate any pointers of what am I doing wrong here.

docker-compose.yml

version: '3'

services:
  localstack:
    image: localstack/localstack:0.12.9.1
    ports:
      - '4566:4566'
    environment:
      - SERVICES=events,iam,lambda,cloudformation,s3,sts
      - DATA_DIR=/tmp/localstack/data
      - DEBUG=true
      - DOCKER_HOST=unix:///var/run/docker.sock
      - AWS_ACCESS_KEY_ID=test
      - AWS_SECRET_ACCESS_KEY=test
      - LAMBDA_EXECUTOR=docker
    volumes:
      - '${TMPDIR:-/tmp/localstack}:/tmp/localstack'
      - /var/run/docker.sock:/var/run/docker.sock

serverless.yml

service: test

frameworkVersion: '>=1.1.0'

custom:
  localstack:
    stages:
      - local
    host: http://localhost
    edgePort: 4566
    lambda:
      mountCode: false
provider:
  name: aws
  runtime: nodejs12.x
  eventBridge:
    useCloudFormation: true

functions:
  test:
    handler: lambda.handler
    events:
      - eventBridge:
          eventBus: customBus
          pattern:
            source:
              - "customSource"

plugins:
  - serverless-localstack

Here is the output of serverless deploy --stage local:

Serverless: Using serverless-localstack
Serverless: Warning: Unable to find plugin named: TypeScriptPlugin
Serverless: Deprecation warning: Resolution of lambda version hashes was improved with better algorithm, which will be used in next major release.
            Switch to it now by setting "provider.lambdaHashingVersion" to "20201221"
            More Info: https://www.serverless.com/framework/docs/deprecations/#LAMBDA_HASHING_VERSION_V2
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
......
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service test.zip file to S3 (693 B)...
Serverless: Validating template...
Serverless: Skipping template validation: Unsupported in Localstack
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
........
Serverless: Stack update finished...
Service Information
service: test
stage: local
region: us-east-1
stack: test-local
resources: 8
api keys:
  None
endpoints:
  None
functions:
  test: test-local-test
layers:
  None

And relevant output from localstack logs, which looks like the put_rule does not have EventBusName param set:

localstack_1  | 2021-04-12T11:38:42:DEBUG:localstack.utils.cloudformation.template_deployer: Running action "create" for resource type "Events::EventBus" id "CustomBusEventBridgeEventBus"
localstack_1  | 2021-04-12T11:38:42:DEBUG:localstack.utils.cloudformation.template_deployer: Request for resource type "Events::EventBus" in region us-east-1: create_event_bus {'Name': 'customBus'}
localstack_1  | 2021-04-12T11:38:43:DEBUG:localstack.utils.cloudformation.template_deployer: Handling "Add" for resource "Testlocaltestrule1EventBridgeRule" (1/4) type "AWS::Events::Rule" in loop iteration 1
localstack_1  | 2021-04-12T11:38:43:DEBUG:localstack.utils.cloudformation.template_deployer: Running action "create" for resource type "Events::Rule" id "Testlocaltestrule1EventBridgeRule"
localstack_1  | 2021-04-12T11:38:43:DEBUG:localstack.utils.cloudformation.template_deployer: Request for resource type "Events::Rule" in region us-east-1: put_rule {'EventPattern': '{"source":["customSource"]}', 'Name': 'test-local-test-rule-1', 'State': 'ENABLED'}
localstack_1  | 2021-04-12T11:38:43:DEBUG:localstack.utils.cloudformation.template_deployer: Request for resource type "Events::Rule" in region us-east-1: put_targets {'Rule': 'test-local-test-rule-1', 'EventBusName': 'customBus', 'Targets': [{'Arn': 'arn:aws:lambda:us-east-1:000000000000:function:test-local-test', 'Id': 'test-local-test-rule-1-target'}]}

The cloudformation-template-update-stack.json shows that the rule has the correct event bus set:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "The AWS CloudFormation template for this Serverless application",
  "Resources": {
    "CustomBusEventBridgeEventBus": {
      "Type": "AWS::Events::EventBus",
      "Properties": {
        "Name": "customBus"
      }
    },
    "Testlocaltestrule1EventBridgeRule": {
      "Type": "AWS::Events::Rule",
      "Properties": {
        "EventBusName": "customBus",
        "EventPattern": "{\"source\":[\"customSource\"]}",
        "Name": "test-local-test-rule-1",
        "State": "ENABLED",
        "Targets": [
          {
            "Arn": {
              "Fn::GetAtt": [
                "TestLambdaFunction",
                "Arn"
              ]
            },
            "Id": "test-local-test-rule-1-target"
          }
        ]
      },
      "DependsOn": "CustomBusEventBridgeEventBus"
    },
  }
}

The call using aws cli shows that the rule has default event bus set.

AWS_ACCESS_KEY_ID=test AWS_SECRET_ACCESS_KEY=test aws --region=us-east-1 --endpoint-url http://localhost:4566 events list-rules
{
    "Rules": [
        {
            "Name": "test-local-test-rule-1",
            "Arn": "arn:aws:events:us-east-1:000000000000:rule/test-local-test-rule-1",
            "EventPattern": "{\"source\":[\"customSource\"]}",
            "State": "ENABLED",
            "EventBusName": "default"
        }
    ]
}
1 Like

Hi,

I have a similar problem with a custom event bus.
I did try with AWS directly and code seems to work, but it doesn’t with localstack.
If I switch to use the default bus, it makes it work with localstack.

Any more insight on this bug or any workaround ?

Hi,
in the meantime I’ve opened a ticket on localstack github: EventBridge trigger on localstack defaults to default event bus even though a new one is passed. · Issue #3872 · localstack/localstack · GitHub. And it’s currently checked from what I can tell.

1 Like