如何保护特定 Pulumi 堆栈中的资源不被删除

how to protect resources in a specific Pulumi stack from being deleted

我使用 Pulumi 在 GCP 中构建我的基础设施。 Pulumi 具有堆栈功能,可帮助您构建同一类型 Pulumi 代码的多个副本。

所以我有 dev/stage/prod 堆栈对应于我们拥有的每个环境。

我想知道是否有一种方法可以保护生产堆栈,以便任何人都无法删除其中的任何资源。

我知道关于保护位标志,但这将适用于我不想要的所有堆栈。

有几个选项可以实现此目的:

选项 1

一个选项是限制对 Pulumi 状态文件的访问,这样只有特权用户或实体(例如 continuous delivery pipeline) is able to read and write the prod state and therefore able to perform operations that might destroy resources. The Pulumi Console backend supports this with stack permissions 在粒度级别上可以通过 IAM 限制其他状态后端的访问特定提供商的功能(例如 AWS IAM)。

选项 2

另一个选项(可以与第一个选项结合使用)是根据堆栈名称以编程方式设置 protect 标志。以下是 Python 中的示例,但相同的概念适用于所有语言:

import pulumi
from pulumi_aws import s3

# only set `protect=True` for "prod" stacks
prod_protected = False
if "prod" == pulumi.get_stack():
    prod_protected = True

bucket = s3.Bucket("my-bucket",
    opts=pulumi.ResourceOptions(
        protect=prod_protected, # use `prod_protected` flag
    ),
)

您需要在堆栈中的每个资源上设置 protect=... 以保护 prod 堆栈中的 所有 资源。 Pulumi SDK 提供了一种使用 堆栈转换 同时在所有资源上进行设置的方法。有一个执行堆栈转换以在资源 here.

上设置标签的示例