2

Looks as really simple task but it's difficult to find good example on it.
So, the task is following: AWS lambda puts some message to AWS-SQS.

Code of AWS lambda contains such line:

var QUEUE_URL = 'https://sqs.us-west-2.amazonaws.com/ID/QUEUE_NAME';", 

In order to get rid of this code there are possible two options:

  1. Create query that will lookup this queue based on region and queue name SQS has predictable names;
  2. Create Cloud Formaion script and specify these dependencies there.

Based on this fact that periodic trigger (lambda) will work many times a day it's better to specify this dependency duting deployment.

In general it looks like as straight forward task and cloud formations script was created:

 "Resources": { "LF2HNR1": { "Type": "AWS::Lambda::Function", "Properties": { "Description": "This is lambda trigger", "Handler": "index.myHandler", "Runtime": "nodejs", "Timeout": "300", 

And also dependency was specified that lambda depends on SQS:

 "DependsOn": [ "SQSQ562D4" ] }, "SQSQ562D4": { "Type": "AWS::SQS::Queue", "Properties": {}, } 

Hovewer it's not stright forward task how to programmatically get SQS url in lambda code:

 exports.handler = function(event, context) { var params = { MessageBody: JSON.stringify(event), var QUEUE_URL = ???? 
1
  • If you're satisfied with my answer, can you please accept it? Thanks! Commented Mar 5, 2016 at 16:46

2 Answers 2

2

The main complexity was to correctly use CloudFormaion API to get SQS URL.

In order to do it I used following code that mainly was driven form this API:

 var queueURL; cloudFormation.describeStackResource(cloudFormationParams, function(err, data) { if (err){ console.log(err, err.stack); // an error occurred } else { var queueURL =data.StackResourceDetail.PhysicalResourceId; var params = { MessageBody: JSON.stringify(event), QueueUrl: queueURL }; sqs.sendMessage(params, function(err,data){ if(err) { context.done('error', "ERROR Put SQS"); // ERROR with message }else{ console.log('data:',data.MessageId); context.done(null,''); // SUCCESS } }); 
Sign up to request clarification or add additional context in comments.

Comments

1

I suggest you retrieve the SQS URL and use it as CloudFormation output:

"Outputs" : { "SQSQ562D4" : { "Description" : "URL of the source queue", "Value" : { "Ref" : "SQSQ562D4" } } } 

Grant your Lambda function cloudformation:DescribeStacks permission to read outputs of your CloudFormation stack and load this output in your code at runtime to access the SQS URL.

Edit: Don't use the approach from the answer below. It's loading resource configuration (the queue URI) in function run time instead of injecting it at Lambda Function deploy time. Below approach is increasing latency, can have random issues with AWS service rate limiting and can is dependent on the AWS CloudFormation API.

2 Comments

The main question is how to load cloudformation output in my code. Any good references (nodejs is preferable) ?
Basically your answer helped me to understand how to do it :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.