Introduction
This part of the series is to help you to recreate your project structure in Pulumi. Therefore, you need to create a Pulumi Stack for each of your project environments.
What is a Stack?
A Stack in Pulumi is an independent configurable instance of a Pulumi program. On the initial creation of a Pulumi Project, it will automatically create the first Stack for you. Each Stack has its own Statefile containing the Resources of the Stack and can be deployed independent.
Why should I have a Stack per environment,
With having a Stack per environment, you gain the advantage of having separate Statefiles and separate configuration files. It enabled you to use the configuration to handle differences of your resources between your environments. Examples for this would be the naming convention, different resource locations or Business Continuity settings to save cost in the development environments.
Setting Up the Pulumi Project
If you already have a Pulumi Project set up, just skip this chapter and check out how to add new Stacks in your project.
To set up the Pulumi Project with the Azure C# configuration I am using, after logging into Azure CLI and connecting to the Pulumi Backend type:
pulumi new azure-csharp
As additional information you now have to enter the Project Name, a Stack name, a Azure Location and a Passphrase. The Passphrase is used to decrypt Secrets in the Statefile.
Adding new Stacks to the Pulumi project
To add new Stacks to your Pulumi project, use the following command:
pulumi stack init <stackname>
Repeat this command for each of your project environments. This will create a Pulumi..yaml File for each Stack. The .yaml File is containing the configuration for a stack. You can add specific Key-Value Pairs to the configuration file per stack. The config file has the following format:
encryptionsalt: <secretValue>
config:
azure-native:location: <azureLocation>
<pulumiProjectName>:<Key> <Value>
Using Key-Value Pairs from configuration files
On top of your code, define a new Pulumi configuration:
var config = new Pulumi.Config();
This var can later be used to Get or Require Secrets from the configuration file. For Key-Value Pairs that cannot be null, you can use:
var valueOfKey = config.Require("<Key>");
If you are using values, that can be absent on another Stack use:
var valueOfKey = config.Get("<Key>");
Be sure to handle any Null-Exceptions in that case.
With this, you have learned how to create Stacks for your environments and also how to use the configuration files for any differences in between your resources.
Top comments (0)