简体   繁体   中英

How can I delete and recreate a CDK Stack (Cloudformation) using a lambda?

I have a stack in cloudformation (created using AWS CDK) that creates an EC2 instance. Every 30 days I want to recreate that instance, ie delete and recreate the stack.

Is it possible to do it using a lambda with eventbridge rules, maybe?

How could I deploy/create the stack from a cfn template stored in S3. Using boto3.

You can use one of the SDKs AWS provides to create a lambda that does what you want. For example, you could write a lambda using Python code and the Python SDK to invoke API functions such as delete_stack or create_stack to do what you want.

There are many good tutorials available that cover how to create a lambda that uses one of the SDKs lambda supports. You can use EventBridge to schedule the lambda to run every 30 days. If you have the CloudFormation template available, say, in S3, then the lambda can easily consume it and use it for creating a stack. You won't necessarily be using the CDK inside the lambda, but you accomplish pretty much the same thing using an SDK.

Or if you really want to use the CDK inside lambda, you could do something like this . Also, this article gives a nice walk-through of many of the steps to do something very similar using the Python SDK.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM