Serverless-application-model: Get SQS url in SAM template.yml file

Created on 1 Feb 2018  路  6Comments  路  Source: aws/serverless-application-model

How can I set up SQS url into the lambda Environment Variables? For example we can get URN for DynamoDB streams:

!GetAtt SuperTable.StreamArn

I want to do something like that for SQS url. If this feature exist it will help me to avoid many configuration issues.
(duplicate of https://stackoverflow.com/questions/48551094/aws-get-sqs-url-in-sam-template-yml-file , maybe here I'll get an answer)

Most helpful comment

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

Use the Ref function to get the url of the queue.

Example:

QueuedInvocationHandler:
  Type: AWS::Serverless::Function
  Properties:
    Handler: ....
    CodeUri: .......
    Policies:
      - SQSPollerPolicy:
          QueueName: !GetAtt InvocationQueue.QueueName
    Environment:
      Variables:
        INVOCATION_QUEUE_URL: !Ref InvocationQueue # Gets SQS Queue URL
    Events:
      Scheduled:
        Type: Schedule
        Properties:
          Schedule: rate(1 minutes)

All 6 comments

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

Use the Ref function to get the url of the queue.

Example:

QueuedInvocationHandler:
  Type: AWS::Serverless::Function
  Properties:
    Handler: ....
    CodeUri: .......
    Policies:
      - SQSPollerPolicy:
          QueueName: !GetAtt InvocationQueue.QueueName
    Environment:
      Variables:
        INVOCATION_QUEUE_URL: !Ref InvocationQueue # Gets SQS Queue URL
    Events:
      Scheduled:
        Type: Schedule
        Properties:
          Schedule: rate(1 minutes)

@oharaandrew314 , thank you!

Just a note for those coming here from Google: this doesn't work locally with sam local but it works when deployed.

@mnapoli Right. Function env vars have to be provided to sam local explicitly via the --env-vars option.

@jlhood thanks a lot for answering. Do you know why it doesn't work?

From my understanding:

  • environment variables in general should be populated with sam local
  • !Ref should work with sam local

so I don't get why specifically here it doesn't work.

!Ref of template parameters work, but SAM CLI has several issues open like https://github.com/awslabs/aws-sam-cli/issues/573 to support more intrinsic functions.

Was this page helpful?
0 / 5 - 0 ratings