简体   繁体   中英

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 :)

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.

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