繁体   English   中英

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

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

我创建了一个Lambda函数来挂起我的自动伸缩组中的“终止”进程,当我在Lambda Node.js代码中对ASG名称进行硬编码时,该函数将起作用。 我需要从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" 

如何告诉node.js函数从上面的CloudFormation“ ASGName”属性中提取ASG名称?

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

我尝试创建一个变量= ASGName以指向CloudFormation属性,然后将其引用为AutoscalingGroupName。 我知道语法在这里不正确。 我看过很多例子,但都没有用。

我是Node.js的新手,所以我们将不胜感激!

谢谢

建议不要使用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

暂无
暂无

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

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