Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get SQS Url inside lambda using CDK?

I'm using CDK to instantiate a Queue and a Lambda Function.

Lambda function requires QueueURL in order to push messages into it.

QueueURL is not fixed, it changes when the stack is re-created.

I have two options:

  1. Pass QueueURL as an env variable to Lambda in CDK.
  2. Create a cfnOutput with QueueURL and read it from the Lambda.

If I use option 2, Lambda will have to make an API call every time it runs to get the URL.

Are these the only options? What is the recommended approach for this?

Thanks!

like image 292
user1275011 Avatar asked May 22 '26 07:05

user1275011


1 Answers

Option 1 is recommended. If the value changes for any reason, the lambda will also be updated accordingly automatically. It also ensures that the lambda will be created after the queue, as it creates an implicit dependency.

Don't forget to grant your lambda access to the queue with myQueue.grantSendMessages(myLambda);

like image 175
gshpychka Avatar answered May 25 '26 22:05

gshpychka