简体   繁体   English

如何保护AWS CloudFormation堆栈不被删除?

[英]How to protect an AWS CloudFormation stack from deletion?

I have a stack which creates a three-tier application. 我有一个堆栈,它创建了一个三层应用程序。 I want to protect my stack from accidental deletion. 我想保护我的堆栈免于意外删除。 Is there any way to protect an AWS CloudFormation stack? 有没有办法保护AWS CloudFormation堆栈?

Also I would like to know how, even if my stack gets deleted, how I can stop resources associated with the stack being deleted. 此外,我想知道,即使我的堆栈被删除,我如何能够停止与被删除的堆栈相关的资源。

There are several ways to protect resources that are created by AWS CloudFormation. 有几种方法可以保护AWS CloudFormation创建的资源。

Protect the Stack 保护堆栈

AWS CloudFormation takes a template that describes desired resources and deploys it as a stack of resources. AWS CloudFormation采用描述所需资源的模板,并将其部署为一堆资源。 When a stack is deleted, the resources are deleted too. 删除堆栈时,也会删除资源。

Therefore, the first method is to control which users have permission to delete the stack. 因此,第一种方法是控制哪些用户有权删除堆栈。 This can be assigned via Identity and Access Management (IAM) . 这可以通过身份和访问管理(IAM)分配。

Here is an example from the Controlling Access with AWS Identity and Access Management documentation: 以下是使用AWS Identity and Access Management文档控制访问的示例:

A sample policy that denies the delete and update stack actions for the MyProductionStack: 拒绝删除和更新MyProductionStack的堆栈操作的示例策略:

{
    "Version":"2012-10-17",
    "Statement":[{
        "Effect":"Deny",
        "Action":[
            "cloudformation:DeleteStack",
            "cloudformation:UpdateStack"
        ],
        "Resource":"arn:aws:cloudformation:us-east-1:123456789012:stack/MyProductionStack/*"
    }]
}

A policy can also require use of a Multi-factor Authentication (MFA) code before performing sensitive operations, such as deleting a stack. 在执行敏感操作(例如删除堆栈之前,策略还可能需要使用多重身份验证(MFA)代码

Protect the Resources 保护资源

Resources created by CloudFormation can still be deleted/modified by any user with appropriate permission. CloudFormation创建的资源仍然可以由具有适当权限的任何用户删除/修改。 Therefore, it is important that you protect important resources from being impacted by unauthorised users. 因此,保护​​重要资源免受未经授权用户的影响非常重要。 AWS recommends granting least privilege so that users only have control over the resources they require, and no more. AWS建议授予最小权限,以便用户只能控制所需的资源,而不再需要。

CloudFormation Deletion Policy CloudFormation删除政策

A deletion policy defines resources that should not be deleted when a stack is deleted. 删除策略定义在堆栈被删除不应删除的资源。

From the CloudFormation documentation : 来自CloudFormation文档

With the DeletionPolicy attribute you can preserve or (in some cases) backup a resource when its stack is deleted. 使用DeletionPolicy属性,您可以保留或(在某些情况下)在删除堆栈时备份资源。 You specify a DeletionPolicy attribute for each resource that you want to control. 您为要控制的每个资源指定DeletionPolicy属性。 If a resource has no DeletionPolicy attribute, AWS CloudFormation deletes the resource by default. 如果资源没有DeletionPolicy属性,AWS CloudFormation将默认删除该资源。

To keep a resource when its stack is deleted, specify Retain for that resource. 要在删除堆栈时Retain资源,请为该资源指定“ Retain ”。 You can use retain for any resource. 您可以将retain用于任何资源。 For example, you can retain an Amazon S3 bucket or an Amazon EC2 instance so that you can continue to use or modify those resources after you delete their stacks. 例如,您可以保留Amazon S3存储桶或Amazon EC2实例,以便在删除堆栈后继续使用或修改这些资源。

This is normally used to keep resources after intentional stack deletion. 这通常用于在故意堆栈删除后保留资源。 For example, retaining an Amazon S3 bucket or an Amazon RDS database. 例如,保留Amazon S3存储桶或Amazon RDS数据库。 However, it could also be used to preserve resource even a stack is accidentally deleted. 但是,即使堆栈被意外删除,它也可用于保留资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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