amazon-web-servicesamazon-sqsamazon-cloudwatchaws-event-bridge

SQS with AWS Event Bridge


I am trying to set up a demo environment to try out SQS as an AWS Event Bridge Source. I tried uploading few documents to SQS to see if Event Bridge detects any change, but I don't see any events triggered. How can I test SQS as a source with AWS Event Bridge?

Resources:
  Queue:
    Type: AWS::SQS::Queue
    Properties:
      QueueName: !Sub ${AWS::StackName}

  LambdaHandlerExecutionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - lambda.amazonaws.com
            Action: sts:AssumeRole
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

  EventConsumerFunction:
    Type: AWS::Lambda::Function
    Properties:
      Handler: index.lambda_handler
      Role: !GetAtt LambdaHandlerExecutionRole.Arn
      Code:
        ZipFile: |
          import json

          def lambda_handler(event, context):
              print("Received event: " + json.dumps(event, indent=2))

      Runtime: python3.7
      Timeout: 50

  EventRule:
    Type: AWS::Events::Rule
    Properties:
      Description: eventEventRule
      State: ENABLED
      EventPattern:
        source:
          - aws.sqs
        resources:
          - !GetAtt Queue.Arn
      Targets:
        - Arn: !GetAtt EventConsumerFunction.Arn
          Id: EventConsumerFunctionTarget

  PermissionForEventsToInvokeLambda:
    Type: AWS::Lambda::Permission
    Properties:
      FunctionName: !Ref EventConsumerFunction
      Action: lambda:InvokeFunction
      Principal: events.amazonaws.com
      SourceArn: !GetAtt EventRule.Arn


Solution

  • SQS data events (publishing new message) are not source events for Event Bridge (EB). Only management events can be picked up by EB, e.g.:

    Also your event rule should be more generic for that:

      EventRule:
        Type: AWS::Events::Rule
        Properties:
          Description: eventEventRule
          State: ENABLED
          EventPattern:
            source:
              - aws.sqs
            # resources:
            #   - !GetAtt Queue.Arn
          Targets:
            - Arn: !GetAtt EventConsumerFunction.Arn
              Id: EventConsumerFunctionTarget
    

    You can also enable CloudWatch trial and detect API events for the SQS. This should enable fetching more events.