1

I use Pulumi to bring up my infrastructures in GCP . Pulumi has the stack features that helps you to build multiple replications of the same type of Pulumi's code.

So I have dev/stage/prod stack that corresponds to each of the environment we have.

I want to know if there is a way that I can protect the production stack so that no one can delete any resources in there.

I am aware that about the protect bit flag, but that would apply to all the stacks which I don't want to.

Huy Le
  • 37
  • 6

1 Answers1

3

there are a couple options to achieve this:

Option 1

One option would be to restrict access to the Pulumi state file such that only a privileged user or entity (e.g. a 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 at a granular level and access can be restricted with the other state backends via the IAM capabilities of the specific provider (e.g. AWS IAM).

Option 2

Another option (that could be used in conjunction with the first) would be to programmatically set the protect flag based on the stack name. Below is an example in Python, but the same concept works in all languages:

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
    ),
)

You would be required to set protect=... on each resource in your stack to protect all resources in the prod stack. The Pulumi SDK provides a way to set this on all resources at once with a stack transformation. There's an example of doing a stack transformation to set tags on resources here.

Cameron
  • 431
  • 3
  • 3
  • 2
    ahh I see what you mean. Thank you for pointing out the stack transformation bit as well. That is a really clean pattern to approach this. – Huy Le Dec 30 '20 at 04:59