【问题标题】:Message in flight for SQS Lambda Trigger用于 SQS Lambda 触发器的消息
【发布时间】:2020-08-13 15:43:23
【问题描述】:

我有触发 lambda 的 SQS。

当我将消息放入 SQS 队列时,它会显示消息在飞行中,而我的 lambda 无法处理消息。

我的 Lambda 具有以下权限

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "sqs:DeleteMessage",
                "sqs:GetQueueUrl",
                "sqs:ListDeadLetterSourceQueues",
                "sqs:DeleteMessageBatch",
                "sqs:ReceiveMessage",
                "sqs:GetQueueAttributes",
                "sqs:ListQueueTags"
            ],
            "Resource": "*"
        }
    ]
}

它还具有以下权限

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "logs:CreateLogGroup",
            "Resource": "arn:aws:logs:us-east-1:5722*****:*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": [
                "arn:aws:logs:us-east-1:5722****:log-group:/aws/lambda/815223_Test:*"
            ]
        }
    ]
}

当我附加“管理员访问”权限时,它起作用并且 lambda 被触发。 我不确定我在这里缺少哪个权限。我的 SQS 队列未加密。

【问题讨论】:

  • 除了“console.log”你的 lambda 函数还有什么作用吗?
  • @Oxi 否 它只从 SQS 读取消息

标签: amazon-web-services aws-lambda amazon-sqs


【解决方案1】:

查看 CloudTrail 以确定 API 失败的根本原因。还要检查用于 SQS 的队列策略。

对于默认 SQS 和 Lambda 组合,您只需要以下权限。

- "SQS:SendMessage"
- "SQS:ReceiveMessage"
- "SQS:DeleteMessage"
- "SQS:GetQueueAttributes"

以下是供您参考的示例 CloudFormation 模板。

AWSTemplateFormatVersion: "2010-09-09"
Description: >
  Creates the SQS and Lambda pattern
Resources:
  # SQS queue and queue policy
  FileProcessingEventsQueue:
    Type: AWS::SQS::Queue
    Properties:
      QueueName: "FileProcessingEventsQueue"
      VisibilityTimeout: 60
  FileProcessingEventsQueuePolicy:
    Type: AWS::SQS::QueuePolicy
    Properties:
      Queues:
        - !Ref FileProcessingEventsQueue
      PolicyDocument:
        Statement:
          - Action:
              - "SQS:*"
            Effect: "Allow"
            Resource: !GetAtt FileProcessingEventsQueue.Arn
            Principal:
              AWS: "*"
            Condition:
              StringEquals:
                aws:SourceAccount: !Sub "${AWS::AccountId}"
  # Lambda function and role for handling the SQS events
  LambdaExecutionRole:
    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
      Policies:
        - PolicyName: InlinePolicy
          PolicyDocument:
            Statement:
              - Action:
                  - "SQS:SendMessage"
                  - "SQS:ReceiveMessage"
                  - "SQS:DeleteMessage"
                  - "SQS:GetQueueAttributes"
                Effect: Allow
                Resource: "*"
  LambdaFunction:
    Type: AWS::Lambda::Function
    Properties:
      Description: "Lambda for the event processing"
      Runtime: "python3.7"
      Role: !GetAtt LambdaExecutionRole.Arn
      Handler: index.handler
      MemorySize: 128
      Timeout: 60
      Code:
        ZipFile: |
          import json
          import logging

          # Configure logging

          LOGGER = logging.getLogger(__name__)
          LOGGER.setLevel(logging.DEBUG)

          def handler(event, context):
              LOGGER.debug(json.dumps(event, indent=4, default=str))
              data = {'status': 'event printed'}
              return data
  SQSAndLambdaMapping:
    Type: AWS::Lambda::EventSourceMapping
    Properties:
      EventSourceArn: !GetAtt FileProcessingEventsQueue.Arn
      FunctionName: !GetAtt LambdaFunction.Arn
Outputs:
  SQSQueue:
    Description: File processing queue
    Value: !Ref FileProcessingEventsQueue

【讨论】:

  • 我认为 Lambda 函数不需要任何 Amazon SQS 权限,因为 Lambda 服务负责从队列中检索消息,并通过 event 字段将其提供给函数,然后完成后删除消息。
  • 你是对的,lambda 不必调用 SQS,但是 lambda 角色用于创建与 SQS 的事件源映射,然后是后续的消息接收和从队列中删除。该许可仅用于此目的。消息本身是传入事件负载的一部分。
  • 如果未提供DeleteMessage 权限,您会注意到ApproximateReceiveCount 上升,并且在CloudWatch 中重复收到相同的消息。
  • 啊!感谢您的澄清!我没有意识到需要 SQS 权限来设置触发器本身(“事件源映射”)。最有趣的!我同意 CloudTrail 应该提供哪些命令因权限不足而失败的线索。
猜你喜欢
  • 2022-01-06
  • 1970-01-01
  • 2020-07-17
  • 2019-08-16
  • 2018-09-30
  • 1970-01-01
  • 2019-05-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多