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. I need to pull the ASG name from the "ASGName" custom resource property in the CloudFormation Template (see below):

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?

This is the node.js function code:

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. 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!

Thanks

1 answer

  • answered 2017-10-11 10:16 BTuan

    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.

    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