Skip to content

bug: Events.Rule is not filtering by EventPattern #11335

@vladshoob

Description

@vladshoob

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

Given there are multiple AWS::Events::Rules triggering the same target (lambda) only last rule is applied, without filtering down to rules satisfying EventPattern property.

Expected Behavior

All rules are applied that are filtered down with EventPattern.

How are you starting LocalStack?

With a docker-compose file

Steps To Reproduce

Given localstack is up with docker compose up

# docker-compose.yml

services:
  localstack:
    image: localstack/localstack:latest
    environment:
      - PROVIDER_OVERRIDE_EVENTS=v2
      - DEBUG=1
    ports:
      - "127.0.0.1:4566:4566"            # LocalStack Gateway
      - "127.0.0.1:4510-4559:4510-4559"  # external services port range
    volumes:
      - "${LOCALSTACK_VOLUME_DIR:-./tmp/localstack}:/var/lib/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"

And stack

# cloudformation.yaml`
Resources:
  LogGroup:
    Type: AWS::Logs::LogGroup
    Properties:
      LogGroupName: /aws/lambda/AwsLambdaMinimalExample

  LambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      FunctionName: AwsLambdaMinimalExample
      Handler: index.handler
      Runtime: nodejs20.x
      Role: !GetAtt LambdaFunctionRole.Arn
      MemorySize: 256
      Code:
        ZipFile: |
          exports.handler = async (event) => {
            console.log(process.env);
            console.log('event', JSON.stringify(event));
          
            return {
              statusCode: 200,
              body: JSON.stringify(event),
            }
          }

  LambdaFunctionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action:
              - sts:AssumeRole
      Path: "/"
      Policies:
        - PolicyName: AppendToLogsPolicy
          PolicyDocument:
            Version: '2012-10-17'
            Statement:
              - Effect: Allow
                Action:
                  - logs:CreateLogGroup
                  - logs:CreateLogStream
                  - logs:PutLogEvents
                Resource: "*"

  EventRuleWithPayload:
    Type: AWS::Events::Rule
    Properties:
      Name: input-rule-with-payload
      EventPattern:
        detail-type:
          - "myDetailType"
        detail:
          payload:
            id:
              - exists: true
      Targets:
        - Id: invoke-lambda-with-payload
          Arn: !GetAtt LambdaFunction.Arn
          InputTransformer:
            InputPathsMap:
              "body": '$.detail.payload'
            InputTemplate: |
              {
                "whoAmI" : "I am event with payload",
                "body": <body>
              }

  EventRuleWithoutPayload:
    Type: AWS::Events::Rule
    Properties:
      Name: input-rule-without-payload
      EventPattern:
        detail-type:
          - "myDetailType"
        detail:
          id:
            - exists: true
      Targets:
        - Id: invoke-lambda-without-payload
          Arn: !GetAtt LambdaFunction.Arn
          InputTransformer:
            InputPathsMap:
              "body": '$.detail'
            InputTemplate: |
              {
                "whoAmI" : "I am event without payload",
                "body": <body>
              }

#  EventRuleWithAnotherType:
#    Type: AWS::Events::Rule
#    Properties:
#      Name: input-rule-without-payload
#      EventPattern:
#        detail-type:
#          - "anotherType"
#      Targets:
#        - Id: invoke-lambda-another-type
#          Arn: !GetAtt LambdaFunction.Arn
#          InputTransformer:
#            InputPathsMap:
#              "body": '$.detail'
#            InputTemplate: |
#              {
#                "whoAmI" : "I am event with another type",
#                "body": <body>
#              }

  PermissionForEventToInvokeLambdaWithPayload:
    Type: AWS::Lambda::Permission
    Properties:
      FunctionName: !GetAtt LambdaFunction.Arn
      Action: "lambda:InvokeFunction"
      Principal: "events.amazonaws.com"
      SourceArn: !GetAtt EventRuleWithPayload.Arn

  PermissionForEventToInvokeLambdaWithoutPayload:
    Type: AWS::Lambda::Permission
    Properties:
      FunctionName: !GetAtt LambdaFunction.Arn
      Action: "lambda:InvokeFunction"
      Principal: "events.amazonaws.com"
      SourceArn: !GetAtt EventRuleWithoutPayload.Arn

#  PermissionForEventToInvokeLambdaWithAnotherType:
#    Type: AWS::Lambda::Permission
#    Properties:
#      FunctionName: !GetAtt LambdaFunction.Arn
#      Action: "lambda:InvokeFunction"
#      Principal: "events.amazonaws.com"
#      SourceArn: !GetAtt EventRuleWithoutPayload.Arn

is deployed using cli command
aws cloudformation create-stack --stack-name minimal-example --capabilities CAPABILITY_NAMED_IAM --template-body file://cloudformation.yaml --endpoint-url http://localhost:4566 --region us-east-1

on dispatching eventbridge event
aws events put-events --endpoint-url http://localhost:4566 --region us-east-1 --entries file://event_v2.json

#event_v2.json
[
  {
    "EventBusName": "default",
    "Source": "com.mycompany.myapp",
    "Detail": "{ \"payload\": { \"id\": \"987654321\", \"name\": \"John Doe\" } }",
    "Resources": [
      "resource1",
      "resource2"
    ],
    "DetailType": "myDetailType"
  }
]

triggers EventRuleWithoutPayload instead of EventRuleWithPayload.

Validation is preformed by tailing a log group
aws logs tail /aws/lambda/AwsLambdaMinimalExample --endpoint-url http://localhost:4566 --region us-east-1 --follow


More so, if EventRuleWithAnotherType is uncommented and the same event is dispatched, EventRuleWithAnotherType is triggered, although should not as detail-type should filter this rule out.

Environment

- OS: Linux
- LocalStack:
  LocalStack version: 3.6.0
  LocalStack Docker image sha: 52f958b312df1232ccffa9944c6147d94cedb847bd4c9b3abb0de6c9966f9384

Anything else?

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions