user1275011
user1275011

Reputation: 1762

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!

Upvotes: 2

Views: 1600

Answers (1)

gshpychka
gshpychka

Reputation: 11492

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

Upvotes: 0

Related Questions