Search code examples
configurationpulumi

Abstracting configuration in Pulumi


I've been learning Pulumi (really great tool!) and I've the following scenario. Let's assume that I've a bunch of resources which I'd like to deploy to multiple environemnts. For each of the environments, I'd like to have different names (for example, ResourceGroup in dev == "dev", qa =="qa" etc.). How can I achieve that with Pulumi ? What I'd like to avoid is having all the configurations (for all the envs) added in one Pulumi config file. With a small number of resources it may work, but with a bigger number, I guess this file will become unreadable. The only solution that comes to my mind is to have a component created for all of the resources and then create a stack for every single environment, which can use the component with the resources. But maybe there's a better option ?


Solution

  • Pulumi stacks are designed specifically for this. You can have one pulumi project file that can be deployed into multiple environments via stacks.

    To get diferent names per stack:

    1. Create a stack with environment name, for example:

      pulumi stack init dev
      
    2. Use pulumi.getStack() to get stack name inside your code.

      It will looks something like this:

      const stack = pulumi.getStack()
      
      const resourceGroup = new azure.core.ResourceGroup("group", {
        name: `group-{stack}` // use stack name as part of resource name
        location: "West US"
      });
      
      

      This example is for JS, but you can find references for other languages in linked docs

    In addition to stack names, you can also have separate configuration values per stack - https://www.pulumi.com/docs/intro/concepts/project/#stack-settings-file