我已经创建了Lambda函数,将其订阅了SNS主题,并且试图将SNS消息中的值传递给Nodejs中的CloudFormation createStack函数。 SNS消息仅包含一个数字,该数字将转换为变量并传递给我的create_stack_function。从那里,我不确定如何正确传递它。模板需要一个名为InstanceNumber的值,该值告诉它要创建的主机数。
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消息。
答案 0 :(得分:0)
根据docs,您传入一个Parameters
参数,该参数是一个对象数组,每个对象至少包含名称(ParameterKey
)和值({{ 1}})中要传递给Cloudformation的参数。
尝试以下操作:
ParameterValue