我创建了一个lambda函数,它赞同的SNS话题,我试图从SNS的消息在一个的NodeJS CloudFormation创建堆栈传递函数的值。所述SNS消息只包含一个数,其被转换成一个变量,并传递给我create_stack_function。从那里,我不知道如何正确地传递。该模板想要一个所谓的实例号值告诉它的主机创建的数量。
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消息有关创建堆栈。
根据docs,您在Parameters
参数,它是一个对象数组传递,至少含有名字(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
}]
});