设置Lambda函数以运行CloudFormation堆栈

时间:2017-09-06 12:31:35

标签: lambda amazon-cloudformation

每当我收到特定的SNS通知时,是否可以运行CloudFormation堆栈。 任何建议如何实现这种情况。

每当我收到特定的SNS通知时,都应触发Lambda函数,然后启动CloudFormation堆栈。

1 个答案:

答案 0 :(得分:4)

您可以从AWS Lambda中访问AWS API,这完全没有问题。如果您使用的Python看起来像:

<li onclick="buttonClicked(1)" id="answer1"></li>
<li onclick="buttonClicked(2)" id="answer2"></li>
<li onclick="buttonClicked(3)" id="answer3"></li>

当然,许多其他选项都是supported as well

有一个很大的警告:上面的代码将创建一个堆栈,但不会跟踪堆栈创建是否成功。虽然您可以通过describe_stacks调用获取该信息,但您不能依赖在AWS Lambda函数的该实例中完成堆栈,因为AWS Lambda函数的最大运行时间为5分钟,但CloudFormation堆栈创建可能需要更长时间。

如果您不关心堆栈创建是否成功,那么您应该是好的,否则我建议您将<html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <link href="https://fonts.googleapis.com/css?family=Varela+Round" rel="stylesheet"> <link href="quiz.css" rel="stylesheet"> <script> var index = 0; var counter = 0; var iconquiz = [ { iq: "Please Begin", ia1: "This is a quiz over different icons used in the coding community!", ia2: "", ia3: "" }, { iq: "which icon is used for GitHub?", ia1: "<i class='fa fa-gitlab fa-lg' aria-hidden='true'></i>" + "<input class='answers' type='radio' name='answer' value='incorrect'>", ia2: "<i class='fa fa-grav fa-lg' aria-hidden='true'></i>" + "<input class='answers' type='radio' name='answer' value='incorrect'>", ia3: "<i class='fa fa-github-alt fa-lg' aria-hidden='true'></i>" + "<input class='answers' type='radio' name='answer' value='correct'>", correct: 3 }, { iq: "What is this? " + "<i class='fa fa-gitlab' aria-hidden='true'></i>", ia1: "GitHub" + "<input class='answers' type='radio' name='answer' value='incorrect'>", ia2: "Atom.io" + "<input class='answers' type='radio' name='answer' value='incorrect'>", ia3: "GitLab" + "<input class='answers' type='radio' name='answer' value='correct'>", correct: 3 }, { iq: "question number three", ia1: "1 ", ia2: "2 ", ia3: "3 " }, ]; function buttonClicked(number) { if (iconquiz[index].correct === number) { counter++ } } function nextButton() { if (index < iconquiz.length-1) { index++; document.getElementById("questionHere").innerHTML = iconquiz[index].iq; document.getElementById("answer1").innerHTML = iconquiz[index].ia1; document.getElementById("answer2").innerHTML = iconquiz[index].ia2; document.getElementById("answer3").innerHTML = iconquiz[index].ia3; } if (iconquiz.length-1 === index) { document.getElementById("questionHere").innerHTML = 'you got ' + counter + ' answers correct!' } } function prevButton() { if (index != 0) { index--; document.getElementById("questionHere").innerHTML = iconquiz[index].iq; document.getElementById("answer1").innerHTML = iconquiz[index].ia1; document.getElementById("answer2").innerHTML = iconquiz[index].ia2; document.getElementById("answer3").innerHTML = iconquiz[index].ia3; } } </script> </head> <body> <h2>Icon Quiz</h2> <input id="toggle" type="checkbox" checked> <label for="toggle">take quiz</label> <div id="expand"> <section> <h3 id="questionHere">Please Begin</h3> <section> <li onclick="buttonClicked(1)" id="answer1"></li> <li onclick="buttonClicked(2)" id="answer2"></li> <li onclick="buttonClicked(3)" id="answer3"></li> </section> </section> <div class="buttons"> <button onclick="prevButton()" type="button">prev</button><button onclick="nextButton()" type="button">next</button> </div> </div> </body> </html> 调用返回的堆栈ID写入持久存储(例如DynamoDB)并单独创建预定的AWS Lambda函数,用于检查存储在DynamoDB中的CloudFormation堆栈的状态,并处理可能的堆栈创建结果。