Sam Morehouse
Sam Morehouse

Reputation: 339

How to reference an AWS resource created via SAM in a Lambda?

I am trying to write to an SQS Queue that I create in the same SAM template as my lambda. To send a message to the queue I need the URL.

client.send_message(QueueUrl='string', MessageBody='string', ...)

I can get the URL if I have the name of the queue (client.get_queue_url(QueueName='string', QueueOwnerAWSAccountId='string')), but this is randomly generated when deployed by SAM. For a Function you can specify FunctionName in the template.yaml, but I haven't found an example of this for a Queue.

I know that in the template.yaml I can use !Ref MyQueue but I don't know of an equivalent to use in the lambda. I assume there is a trick, I just don't know that trick :)

Upvotes: 1

Views: 651

Answers (1)

Jason Wadsworth
Jason Wadsworth

Reputation: 8887

You can pass in the queue URL as an environment variable to the lambda. That allows you use the !Ref MyQueue in the template to reference the queue. Just add that to the Environment -> Variables section of the lambda configuration.

Upvotes: 1

Related Questions