如果 CodeBuild 失败,则删除 CloudFormation 堆栈

Delete CloudFormation stack if CodeBuild fails

我在 CodePipeline 中有一个阶段,它部署 CloudFormation 模板以动态生成环境以执行集成测试。如果集成测试失败,我想删除这些环境,但我不知道如何解决这个问题。我正在考虑添加 CloudWatch 事件来触发 lambda,但后来我受到 lambda 5 分钟超时的限制。

I was thinking on adding a CloudWatch event to trigger a lambda but then I have the constraint of the 5 minutes timeout for the lambda.

这是我推荐的方法。 CodePipeline 发出 CloudWatch 事件,包括操作失败时:https://docs.aws.amazon.com/codepipeline/latest/userguide/detect-state-changes-cloudwatch-events.html

关于 5 分钟的超时,这应该不是问题,因为 CloudFormation DeleteStack API 会立即 return 即使 CloudFormation 需要几分钟才能真正删除堆栈。