AWS SAM:在模板中使用现有的 SQS 队列

AWS SAM: Use an existing SQS Queue in template

我有一个 lambda 函数,它应该在消息到达我的队列时被触发。我正在通过 SAM cli 开发和部署此功能。但是,SQS 队列已经存在,由于我的用例的限制,我无法将它与 lambda 函数一起创建。所以,我必须使用这个现有的队列。

以下是我的template.yml

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  Serverless functions for foobar


Globals:
  Function:
    Timeout: 60 # 60 seconds timeout for each lambda function

Resources:

  # Lambda function #1; foobar SQS trigger
  OrderDrop:
    Type: AWS::Serverless::Function 
    Properties:
      CodeUri: sqs_trigger/foobar
      Handler: app.lambda_handler
      Runtime: python3.8
      Description: foobar SQS trigger
      Events:
        FooBarSQS:
          Type: SQS
          Properties:
            Queue: !GetAtt FooBarSQS.Arn
            BatchSize: 1

  # Foobar SQS
  FooBarSQS:
    Type: SQS
    Properties:
      Queue: arn:aws:sqs:us-east-1:1234567890:foobar_queue.fifo
      Enabled: true

我收到以下错误:

Error: Failed to create changeset for the stack: gitlabpoc, ex: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state Status: FAILED. Reason: Template format error: Unrecognized resource types: [SQS]

我正在关注此文档:

https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-sqs.html

还有这个文档:

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-sqs-queues.html

但是我不能告诉 arn 我现有队列的任何东西

我怎样才能做到这一点?

我想出来了,因为我的 OrderDropEvent 中的 Queue: !GetAtt FooBarSQS.Arn 属性 需要 Queue arn,我只是给了它我现有的 arn排队。

OrderDrop:
    Type: AWS::Serverless::Function 
    Properties:
      CodeUri: sqs_trigger/foobar
      Handler: app.lambda_handler
      Runtime: python3.8
      Description: foobar SQS trigger
      Events:
        FooBarSQS:
          Type: SQS
          Properties:
            Queue: arn:aws:sqs:us-east-1:1234567890:foobar_queue.fifo
            BatchSize: 1

成功了!