3

我正在尝试设置一个演示环境来尝试将 SQS 作为 AWS 事件桥源。我尝试将一些文档上传到 SQS 以查看 Event Bridge 是否检测到任何更改,但我没有看到任何事件被触发。如何使用 AWS Event Bridge 将 SQS 作为源进行测试?

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

4

2 回答 2

4

SQS 数据事件(发布新消息)不是事件桥 (EB) 的源事件。EB 只能获取管理事件,例如:

  • 清除队列
  • 创建新队列
  • 删除队列

此外,您的事件规则应该更通用:

  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

您还可以启用 CloudWatch 试用并检测 SQS 的 API 事件。这应该能够获取更多事件。

于 2021-03-08T22:53:49.283 回答
1

我可能迟到了,但这可以让其他人受益,看看这个: https ://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-ecs-patterns.QueueProcessingFargateService.html

这将根据 SQS 队列中的消息数量来处理 Fargate 容器的缩放。

可以使用 AWS CDK 定义一个最简单的堆栈,如下所示:

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"
    )
于 2021-09-05T22:06:04.517 回答