【问题标题】:Referencing CloudFormation variable from a Node.js Lambda function从 Node.js Lambda 函数引用 CloudFormation 变量
【发布时间】:2018-03-22 23:25:36
【问题描述】:

我创建了一个 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 的新手,因此我们将不胜感激!

谢谢

【问题讨论】:

    标签: javascript node.js amazon-web-services lambda amazon-cloudformation


    【解决方案1】:

    我建议您使用 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

    【讨论】:

    • 感谢您的回复。我已经重写了现在可以工作的代码。它使用以下语法提取值:var ASGName (event.ResourceProperties.ASGName);
    猜你喜欢
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 1970-01-01
    相关资源
    最近更新 更多