Node.js 使用Nodejs将参数传递给CloudFormation函数
我创建了一个Lambda函数,订阅了一个SNS主题,并尝试将一个值从SNS消息传递给Nodejs中的CloudFormation createStack函数。SNS消息只包含一个数字,该数字被转换为变量并传递给我的create_stack_函数。从那以后,我不知道如何正确地通过它。模板需要一个名为InstanceNumber的值,该值告诉模板要创建的主机数Node.js 使用Nodejs将参数传递给CloudFormation函数,node.js,amazon-cloudformation,Node.js,Amazon Cloudformation,我创建了一个Lambda函数,订阅了一个SNS主题,并尝试将一个值从SNS消息传递给Nodejs中的CloudFormation createStack函数。SNS消息只包含一个数字,该数字被转换为变量并传递给我的create_stack_函数。从那以后,我不知道如何正确地通过它。模板需要一个名为InstanceNumber的值,该值告诉模板要创建的主机数 topic_arn = "arn:aws:sns:us-west-2:xxxxxxxxxxxx:xxxxxxxxxxxxxxx"; var
topic_arn = "arn:aws:sns:us-west-2:xxxxxxxxxxxx:xxxxxxxxxxxxxxx";
var AWS = require('aws-sdk');
AWS.config.region_array = topic_arn.split(':'); // splits the ARN in to and array
AWS.config.region = AWS.config.region_array[3]; // makes the 4th variable in the array (will always be the region)
// Searches SNS messages for number of hosts to create
exports.handler = function (event, context) {
const message = event.Records[0].Sns.Message;
var NumberOfHosts = message;
return create_stack_function(NumberOfHosts);
// Might change return value, but all code branches should return.
return true;
};
// Creates stack and publishes number of instances to the send_SNS_notification function
async function create_stack_function(NumberOfHosts) {
const cloudformation = new AWS.CloudFormation();
try {
const resources = await cloudformation.createStack({
StackName: "Launch-Test",
TemplateURL: "https://s3-us-west-2.amazonaws.com/cf-templates-xxxxxxxxxxx-us-west-2/xxxinstances.yaml",
InstanceNumber: NumberOfHosts,
}).promise();
return send_SNS_notification(NumberOfHosts);
} catch(err) {
console.log(err, err.stack);
}
}
// Publishes message to SNS
async function send_SNS_notification(NumberOfHosts) {
const sns = new AWS.SNS();
const resources_str = JSON.stringify(NumberOfHosts);
try {
const data = await sns.publish({
Subject: "CloudFormation Stack Created",
Message: "A new stack was created containing" + NumberOfHosts + "host(s).",
TopicArn: topic_arn
}).promise();
console.log('push sent');
console.log(data);
} catch (err) {
console.log(err.stack);
}
}
我希望此Lambda函数接收SNS消息,将消息转换为变量,创建CloudFormation堆栈,并发送有关正在创建的堆栈的SNS消息。根据,您传入一个参数
参数,该参数是一个对象数组,每个对象至少包含名称(ParameterKey
)和要传递到Cloudformation的参数的值(ParameterValue
)
请尝试以下操作:
cloudformation.createStack({
StackName: "Launch-Test",
TemplateURL: "https://s3-us-west-2.amazonaws.com/cf-templates-xxxxxxxxxxx-us-west-2/xxxinstances.yaml",
Parameters: [{
ParameterKey: "InstanceNumber", // name of the Cloudformation parameter
ParameterValue: String(NumberOfHosts) // its value, as a String
}]
});
根据,传入一个Parameters
参数,该参数是一个对象数组,每个对象至少包含要传入到Cloudformation的参数的名称(ParameterKey
)和值(ParameterValue
)
请尝试以下操作:
cloudformation.createStack({
StackName: "Launch-Test",
TemplateURL: "https://s3-us-west-2.amazonaws.com/cf-templates-xxxxxxxxxxx-us-west-2/xxxinstances.yaml",
Parameters: [{
ParameterKey: "InstanceNumber", // name of the Cloudformation parameter
ParameterValue: String(NumberOfHosts) // its value, as a String
}]
});