简体   繁体   中英

AWS SAM: Use an existing SQS Queue in template

I have a lambda function that is supposed to be triggered when a message arrives in my queue. I am developing & deploying this function via SAM cli. But, the SQS queue already exists and I can not create it along with the lambda function due to a restriction in my use case. So, I have to use this existing queue.

following is my 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

I am getting the following error:

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]

I was following this document:

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

There is also this document:

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

But I can't anything where I can tell the arn of my existing queue

How can I achieve this?

I figured it out, since the Queue: .GetAtt FooBarSQS.Arn property in my OrderDrop 's Event requires the Queue arn, I just gave it the arn of my existing queue.

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

This did the trick!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM