AWS lambda read parameter or outputs from CloudFormation

故事扮演 提交于 2021-02-19 04:53:45

问题


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:


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:


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


来源:https://stackoverflow.com/questions/35781529/aws-lambda-read-parameter-or-outputs-from-cloudformation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!