This simple project demonstrates the conditional creation of resources using Pulumi, based on the state of pre-existing resources.
Inspired by this blog post.
First of all clone the repo:
git clone https://github.com/wtfzambo/pulumi-conditional-resource.git
You will need to create 2 AWS SSM parameters and 1 bucket. Open the AWS console and create the following resources:
- SSM parameter 1:
/pulumiExample/dev/shouldCreateBucket
with valuetrue
- SSM parameter 2:
/pulumiExample/prod/shouldCreateBucket
with valuefalse
- S3 bucket:
pulumiexample-prod-<YOUR_AWS_ACCOUNT_ID>
You can quickly retrieve the AWS account ID by running the following command if you have AWS CLI installed:
aws sts get-caller-identity
This project uses named AWS profiles for authentication, so make sure you have the right setup beforehand.
Export the AWS_PROFILE environment variable:
export AWS_PROFILE=my_profile
on Mac or Linux$env:AWS_PROFILE=my_profile
on Windows Powershell
brew install pulumi
on Mac or Linuxscoop install pulumi
on Windows
Pulumi can use different backends to manage state. For simplicity, let's use a local one.
pulumi login --local
python -m venv .venv
source .venv/bin/activate # (.venv\Scripts\activate on Windows)
pip install -r requirements.txt
Pulumi has the concept of stacks which can be compared to environments.
Select the dev stack: pulumi stack select dev
and then run:
pulumi preview
This will show you the resources that are about be created. You will notice that in the outputs, the ARN of the bucket is not displayed, showing output<string>
instead.
This is because the resource doesn't exist yet.
Now run
pulumi up -y
Pulumi has created the resources and the ARN of the bucket is now available in the output.
Now select the prod stack: pulumi stack select prod
and then run the same commands as above.
In this case, you will see that even during preview
, the ARN of the bucket is available. This is because on the condition we set, the prod
stack shouldn't create a new bucket, but just reference the already existing one that you created in the first steps.