thotam
thotam

Reputation: 1011

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

Upvotes: 5

Views: 10851

Answers (3)

Roberto Vargas
Roberto Vargas

Reputation: 1

Amazon SQS only supports logging the following actions with CloudTrail (does not include SendMessage):

AddPermission

CreateQueue

DeleteQueue

PurgeQueue

RemovePermission

SetQueueAttributes

TagQueue

UntagQueue

Upvotes: 0

Potato
Potato

Reputation: 87

I might be late but this can benefit someone else, have a look at this: https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ecs-patterns.QueueProcessingFargateService.html

This will handle scaling of the Fargate container based on a number of messages in the SQS Queue.

a simplest stack can be defined using AWS CDK as following:

queue = sqs.Queue(stack, "Queue")

cluster = aws_ecs.Cluster(
            stack, 'FargateCluster'
        )

queue_processing_fargate_service = QueueProcessingFargateService(stack, "Service",
        cluster=cluster,
        memory_limit_mi_b=512,
        image=ecs.ContainerImage.from_registry("test"),
        command=["-c", "4", "amazon.com"],
        enable_logging=False,
        desired_task_count=2,
        environment={
            "TEST_ENVIRONMENT_VARIABLE1": "test environment variable 1 value",
            "TEST_ENVIRONMENT_VARIABLE2": "test environment variable 2 value"
        },
        queue=queue,
        max_scaling_capacity=5,
        container_name="test"
    )

Upvotes: 2

Marcin
Marcin

Reputation: 238051

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.:

  • purging of the queue
  • creating of new queue
  • deletion of a queue

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.

Upvotes: 5

Related Questions