设置Lambda函数以运行CloudFormation堆栈 [英] Setup Lambda function to run a CloudFormation stack

查看:72
本文介绍了设置Lambda函数以运行CloudFormation堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我收到特定的SNS通知时,是否有可能运行CloudFormation堆栈. 有关如何实现此方案的任何建议.

Is it possible to run a CloudFormation stack whenever I get a specific SNS notification. Any suggestions how to achieve this scenario.

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

Whenever I get a specific SNS notification, a Lambda function should be triggered which will then launch a CloudFormation stack.

推荐答案

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

As you can access the AWS API from within AWS Lambda that's no problem at all. If you're using Python that could look like:

import boto3
cf_client = boto3.client('cloudformation')
cf_client.create_stack(
    StackName='your-stack',
    TemplateURL='https://s3.amazonaws.com/your-bucket/your-template'
)

当然,许多其他参数是也支持.

Of course lots of additional parameters are supported as well.

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

There is one big caveat: The code above will create a stack, but will not track if the stack creation succeeds. While you can get that information via the describe_stacks call, you can't rely on having a finished stack within that instance of the AWS Lambda function, as the maximum runtime of the AWS Lambda function is 15 minutes, but the CloudFormation stack creation might take longer than that.

如果您不关心堆栈创建是否成功,则应该很好,否则,我建议您将由create_stack调用返回的堆栈ID写入持久性存储(例如DynamoDB),并具有单独的计划AWS Lambda函数可检查DynamoDB中存储的CloudFormation堆栈的状态并处理可能的堆栈创建结果.

If you don't care if the stack creation succeeded you should be good, otherwise I suggest you write the stack id, returned by the create_stack call, to a persistent storage (e.g. DynamoDB) and have a separate scheduled AWS Lambda function which checks the status of the CloudFormation stacks stored in DynamoDB and handles the possible stack creation outcomes.

这篇关于设置Lambda函数以运行CloudFormation堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆