简体   繁体   English

从Node.js Lambda函数引用CloudFormation变量

[英]Referencing CloudFormation variable from a Node.js Lambda function

I've created a Lambda function to suspend the "Terminate" process in my autoscaling group which works when I hardcode the ASG Name in the Lambda Node.js code. 我创建了一个Lambda函数来挂起我的自动伸缩组中的“终止”进程,当我在Lambda Node.js代码中对ASG名称进行硬编码时,该函数将起作用。 I need to pull the ASG name from the "ASGName" custom resource property in the CloudFormation Template (see below): 我需要从CloudFormation模板的“ ASGName”自定义资源属性中提取ASG名称(请参见下文):

SuspendProcess:
 Type: Custom::SuspendProcess
 Properties:
   ServiceToken: arn:aws:lambda:eu-west-1:############:function:TestFunction
   Region: !Ref AWS::Region
   ASGName: !Ref ASG
 DependsOn: "ASG" 

How do I tell the node.js function to pull the ASG name from the CloudFormation "ASGName" property above? 如何告诉node.js函数从上面的CloudFormation“ ASGName”属性中提取ASG名称?

This is the node.js function code: 这是node.js函数代码:

var AWS = require('aws-sdk');
var uuid = require('uuid');
AWS.config.update({ region: 'eu-west-1' });

exports.handler = (event, context, callback) => {

AWS.config.apiVersions = {
  autoscaling: '2011-01-01',
};

var autoscaling = new AWS.AutoScaling();
var ASGName = parseInt(event.ResourceProperties.ASGName);

/* This suspends the specified scaling process for the specified Auto 
Scaling group. */

 var params = {
  AutoScalingGroupName: "ASGName", 
  ScalingProcesses: [
     "Terminate"
  ]
 };
 autoscaling.suspendProcesses(params, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else     console.log(data);           // successful response
 });
};

I've tried to create a variable = ASGName to point to the CloudFormation property then reference this as the AutoscalingGroupName. 我尝试创建一个变量= ASGName以指向CloudFormation属性,然后将其引用为AutoscalingGroupName。 I know the syntax isn't correct here. 我知道语法在这里不正确。 I've seen lots of examples but none of them work. 我看过很多例子,但都没有用。

I'm new to Node.js so any help would be appreciated! 我是Node.js的新手,所以我们将不胜感激!

Thanks 谢谢

Instead of try to get it in properties, I suggest you use nodeJS sdk code to get that resource, then put that info into your ASGName variable. 建议不要使用nodeJS sdk代码来获取该资源,而不是尝试在属性中获取它,然后将该信息放入ASGName变量中。

var params = {
  LogicalResourceId: 'STRING_VALUE', /* required */
  StackName: 'STRING_VALUE' /* required */
};
cloudformation.describeStackResource(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     ASGName = data;           // successful response
});

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudFormation.html#describeStackResource-property http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudFormation.html#describeStackResource-property

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM