Infrastructure as code · Intermediate · 22 min
Manage Pulumi stacks, configuration and encrypted secrets
By Padding · Updated 18 Nov 2026
Linux or macOS · Node.js 26 · Pulumi 3.278.0 · TypeScript 5.9 · @pulumi/random 4.21
You will run one Pulumi TypeScript program as separate dev and prod stacks with typed per-stack configuration, an encrypted database password, a second project that reads their outputs through a StackReference, and a rotated secrets passphrase.
On this page
A Pulumi project is your program. A stack is one deployed copy of it, with its own configuration, its own secrets and its own state. Most teams run at least two stacks of the same project, dev and prod, and keep the differences between them out of the code and in each stack's configuration.
In this tutorial you build a small project called shop that describes a pretend service. It uses the random provider only, so nothing touches a cloud account and nothing bills. You will:
- keep state and secrets on your own disk, with no Pulumi Cloud account
- create
devandprodstacks and give each typed settings: a region, a replica count, a flag and a nested object - store a database password as a secret, typed at a prompt, and see that only ciphertext reaches the files
- read all of it in TypeScript with
pulumi.Config - read one stack's outputs from a second project with a
StackReference - rotate the passphrase that protects the secrets, and see how to move a stack to a cloud key service
Everything lives in ~/pulumi-stacks-demo, and the last step removes it.
Step 1 · Install the CLI and keep state on your own disk
If you followed Write your first Pulumi program in TypeScript you already have the CLI. Otherwise install Pulumi 3.278.0 with the official script. It puts the binaries in ~/.pulumi/bin and adds that folder to your shell profile:
curl -fsSL https://get.pulumi.com | sh -s -- --version 3.278.0
export PATH="$HOME/.pulumi/bin:$PATH"
pulumi versionA CLI installed this way gets no updates from your package manager. To upgrade, run the same command with a newer --version.
Pulumi needs two things before it can create a stack: somewhere to keep state (the backend) and a way to encrypt secrets (the secrets provider). Use a folder as the backend, and a passphrase as the secrets provider. The passphrase goes in a file that only you can read, so it is never typed on a command line or saved in your shell history:
mkdir -p ~/pulumi-stacks-demo/state
( umask 077; head -c 32 /dev/urandom | base64 > ~/pulumi-stacks-demo/passphrase )
export PULUMI_BACKEND_URL="file://$HOME/pulumi-stacks-demo/state"
export PULUMI_CONFIG_PASSPHRASE_FILE="$HOME/pulumi-stacks-demo/passphrase"PULUMI_BACKEND_URL affects only shells where it is set, so this demo does not change the backend your other Pulumi projects use. If you open a new terminal later, run the two export lines again. Anyone who has the passphrase file and the state can read every secret, so keep the file out of Git and out of shared folders.
Check it worked
pulumi version prints v3.278.0, and the passphrase file is private to you:
ls -l ~/pulumi-stacks-demo/passphraseThe line starts with -rw-------.
Step 2 · Write the shop project
Create the project by hand, so you can see every file. Pulumi.yaml names the project and the language. Its config block holds a value that is the same for every stack, the owning team:
mkdir -p ~/pulumi-stacks-demo/shop
cd ~/pulumi-stacks-demo/shop
cat > Pulumi.yaml <<'YAML'
name: shop
description: One program, run as a dev stack and a prod stack
runtime: nodejs
config:
owner: platform-team
YAML
cat > package.json <<'JSON'
{
"name": "shop",
"main": "index.ts",
"private": true,
"dependencies": {
"@pulumi/pulumi": "3.278.0",
"@pulumi/random": "4.21.6"
},
"devDependencies": {
"@types/node": "26.6.11",
"typescript": "5.9.3"
}
}
JSON
cat > tsconfig.json <<'JSON'
{
"compilerOptions": {
"strict": true,
"outDir": "bin",
"target": "es2020",
"module": "nodenext",
"moduleResolution": "nodenext",
"sourceMap": true
},
"files": ["index.ts"]
}
JSONThe program reads seven settings. require and its typed cousins stop the run with a clear error when a value is missing, while getBoolean returns undefined, so you can fall back to a default in code. requireSecret returns an Output marked as secret, and anything built from it stays secret:
cat > index.ts <<'TS'
import * as pulumi from "@pulumi/pulumi";
import * as random from "@pulumi/random";
interface Limits {
cpu: string;
memoryMb: number;
}
const config = new pulumi.Config();
const owner = config.require("owner");
const region = config.require("region");
const replicas = config.requireNumber("replicas");
const debug = config.getBoolean("debug") ?? false;
const limits = config.requireObject<Limits>("limits");
const dbPassword = config.requireSecret("dbPassword");
if (replicas < 1 || replicas > 10) {
throw new Error(`replicas must be between 1 and 10, got ${replicas}`);
}
const stack = pulumi.getStack();
const suffix = new random.RandomPet("suffix", { length: 2 });
export const serviceName = pulumi.interpolate`shop-${stack}-${suffix.id}`;
export const settings = { owner, region, replicas, debug, limits };
export const databaseUrl = pulumi.interpolate`postgres://shop:${dbPassword}@db-${stack}.internal:5432/shop`;
TS
npm installnew pulumi.Config() with no argument reads keys in the project's own namespace, so region means shop:region. Versions in package.json are exact, so a new release cannot change behaviour under you. On prepaid or metered data, note that npm install downloads the Pulumi SDK once per project, and the first pulumi up downloads the random provider plugin once into ~/.pulumi/plugins.
Check it worked
The TypeScript compiler finds no errors:
npx tsc --noEmit && echo "types OK"It prints types OK.
Step 3 · Create a dev stack and a prod stack
Create both stacks. Each one gets its own salt for encrypting secrets, derived from your passphrase:
cd ~/pulumi-stacks-demo/shop
pulumi stack init dev --secrets-provider passphrase
pulumi stack init prod --secrets-provider passphrase
pulumi stack lspulumi stack init also selects the new stack, so prod is now the current one, marked with * in the list. Relying on the selected stack is how people deploy to the wrong environment. For the rest of this tutorial, every command names its stack with -s, and you can do the same in your own scripts.
Check it worked
pulumi stack ls lists dev and prod*. Each stack has a settings file next to Pulumi.yaml:
ls Pulumi.*.yaml
cat Pulumi.dev.yamlYou see Pulumi.dev.yaml and Pulumi.prod.yaml. Each holds one line so far, encryptionsalt: v1:.... The salt is not secret on its own.
Step 4 · Give each stack typed settings
Set the same keys on both stacks with different values. A top-level value such as replicas is saved as a string; the program's requireNumber and getBoolean turn it into a number or a boolean when it runs, and stop with an error if it cannot be parsed. --path sets one field inside a nested object, such as limits.cpu, and inside that object integers and true/false are stored as real YAML numbers and booleans:
cd ~/pulumi-stacks-demo/shop
pulumi config set -s dev region eu-west-2
pulumi config set -s dev replicas 1
pulumi config set -s dev debug true
pulumi config set -s dev --path limits.cpu 250m
pulumi config set -s dev --path limits.memoryMb 256
pulumi config set -s prod region af-south-1
pulumi config set -s prod replicas 3
pulumi config set -s prod --path limits.cpu 1000m
pulumi config set -s prod --path limits.memoryMb 1024Prod has no debug key, so the program's ?? false default applies there. af-south-1 is the AWS Cape Town Region; here it is only a string the program passes through, so use whatever region names your provider has.
Check it worked
cat Pulumi.prod.yamlThe file now has a config: block with shop:region: af-south-1, shop:replicas: "3" (a quoted string, which requireNumber parses) and a nested shop:limits: map with cpu: 1000m and memoryMb: 1024, where 1024 has no quotes because --path stored it as a number. pulumi config -s dev shows the same keys for dev as a table.
Step 5 · Store the database password as a secret
Set the password with --secret and leave the value off the command line. Pulumi prompts for it and does not show what you type, so it never lands in your shell history or the process list:
cd ~/pulumi-stacks-demo/shop
pulumi config set --secret -s dev dbPassword
pulumi config set --secret -s prod dbPasswordType a different password at each value: prompt and press Enter.
Check it worked
grep -A1 'dbPassword' Pulumi.dev.yaml
pulumi config -s devThe file holds shop:dbPassword: followed by secure: v1: and a long base64 string, which is the password encrypted with the dev stack's key. That is why stack files are safe to commit to Git while the passphrase is not. In the pulumi config table, the password shows as [secret]. To read it back, run pulumi config get -s dev dbPassword, which needs the passphrase.
Step 6 · Deploy both stacks and read their outputs
Preview dev, then apply it. --yes skips the confirmation prompt because you have just read the preview:
cd ~/pulumi-stacks-demo/shop
pulumi preview -s dev
pulumi up -s dev --yes
pulumi up -s prod --yesOn a stack other people depend on, leave --yes off and answer the prompt yourself. If you set replicas to 0, the check in index.ts stops the preview with your error message before anything changes.
Check it worked
pulumi stack output -s dev
pulumi stack output -s prod settings --jsonDev shows three outputs: a serviceName such as shop-dev-happy-otter, the settings object, and databaseUrl as [secret]. Prod's settings show "replicas": 3, "debug": false from the code default and "owner": "platform-team" from Pulumi.yaml. Each stack got its own random name, because each has its own state.
The secret is also encrypted in the state file. Count the encrypted values in dev's state:
grep -c '"ciphertext"' ~/pulumi-stacks-demo/state/.pulumi/stacks/shop/dev.jsonIt prints a number of at least 1, and the file contains no plaintext password. Pass --show-secrets to pulumi stack output only when you need to see the value.
Step 7 · Rotate the passphrase and know the other providers
If the passphrase may have leaked (a laptop lost, a teammate gone), change it. pulumi stack change-secrets-provider decrypts every secret in a stack's config and state with the current passphrase and encrypts them again with a new one. Create the new passphrase in its own private file:
cd ~/pulumi-stacks-demo/shop
( umask 077; head -c 32 /dev/urandom | base64 > ~/pulumi-stacks-demo/passphrase.new )
grep encryptionsalt Pulumi.dev.yamlNote the salt. Now rotate each stack. The current passphrase still comes from PULUMI_CONFIG_PASSPHRASE_FILE; the new one is read from standard input, so it never appears on the command line:
pulumi stack change-secrets-provider -s dev passphrase < ~/pulumi-stacks-demo/passphrase.new
pulumi stack change-secrets-provider -s prod passphrase < ~/pulumi-stacks-demo/passphrase.new
mv ~/pulumi-stacks-demo/passphrase ~/pulumi-stacks-demo/passphrase.old
mv ~/pulumi-stacks-demo/passphrase.new ~/pulumi-stacks-demo/passphraseEach command prints Migrating old configuration and state to new secrets provider. The file name in PULUMI_CONFIG_PASSPHRASE_FILE stays the same, so nothing else needs to change.
A passphrase is one of several secrets providers. With a cloud key service, nobody holds a passphrase at all: access to the key decides who can decrypt. For a prod stack on AWS, the command looks like this, with an existing KMS key alias and AWS credentials in your shell:
pulumi stack change-secrets-provider -s prod "awskms://alias/shop-prod-pulumi?region=af-south-1"The other options are azurekeyvault://, gcpkms://, hashivault://, and default for stacks kept in Pulumi Cloud.
Check it worked
grep encryptionsalt Pulumi.dev.yaml
pulumi preview -s dev --expect-no-changesThe salt differs from the one you noted, and the preview succeeds with the new passphrase and reports no changes. The old passphrase no longer opens the stack:
if PULUMI_CONFIG_PASSPHRASE_FILE="$HOME/pulumi-stacks-demo/passphrase.old" pulumi preview -s dev; then echo "old passphrase still works"; false; else echo "old passphrase rejected"; fi
rm ~/pulumi-stacks-demo/passphrase.oldThe preview fails with an incorrect passphrase error and the line old passphrase rejected follows.
Step 8 · Read shop's outputs from another project
Big systems split into projects with different owners: the network team's stack, the database stack, the app stack. A StackReference lets one project read another stack's outputs without copying values around. Create a second project, app, whose stacks follow shop's stack names:
mkdir -p ~/pulumi-stacks-demo/app
cd ~/pulumi-stacks-demo/app
cat > Pulumi.yaml <<'YAML'
name: app
description: Reads the shop stack with the same name
runtime: nodejs
YAML
cat > package.json <<'JSON'
{
"name": "app",
"main": "index.ts",
"private": true,
"dependencies": {
"@pulumi/pulumi": "3.278.0"
},
"devDependencies": {
"@types/node": "26.6.11",
"typescript": "5.9.3"
}
}
JSON
cp ../shop/tsconfig.json .
cat > index.ts <<'TS'
import * as pulumi from "@pulumi/pulumi";
const env = pulumi.getStack();
const shop = new pulumi.StackReference(`organization/shop/${env}`);
export const upstreamService = shop.requireOutput("serviceName");
export const upstreamReplicas = shop.requireOutput("settings").apply((s) => s.replicas);
export const upstreamDatabaseUrl = shop.requireOutput("databaseUrl");
TS
npm install
pulumi stack init dev --secrets-provider passphrase
pulumi up -s dev --yesA stack reference needs the fully qualified name, <organisation>/<project>/<stack>. A backend on your own disk always uses the organisation name organization, spelt that way; in Pulumi Cloud it is your organisation's name. requireOutput fails the run if shop stops exporting a value, which is better than deploying with undefined.
Check it worked
pulumi stack output -s devupstreamService matches shop's dev serviceName, upstreamReplicas is 1, and upstreamDatabaseUrl is [secret]: a secret stays secret when another project reads it.
Step 9 · Clean up
Destroy the consumer first, then shop's stacks. --remove deletes each stack and its Pulumi.<stack>.yaml once its resources are gone:
cd ~/pulumi-stacks-demo/app
pulumi destroy -s dev --yes --remove
cd ~/pulumi-stacks-demo/shop
pulumi destroy -s dev --yes --remove
pulumi destroy -s prod --yes --remove
cd ~
rm -rf ~/pulumi-stacks-demo
unset PULUMI_BACKEND_URL PULUMI_CONFIG_PASSPHRASE_FILEThe Pulumi CLI and the downloaded random plugin stay installed. Remove the plugin with pulumi plugin rm resource random 4.21.6 --yes if you no longer need it.
Check it worked
ls ~/pulumi-stacks-demo reports that there is no such file or directory.
Next steps
- Start from the basics of inputs, outputs and previews: Write your first Pulumi program in TypeScript.
- Compare this with the Terraform approach to environments: Use Terraform workspaces for separate environments.
- Keep passphrase files and
.envfiles out of your repository: Keep secrets out of Git.
Related tutorials
- Write your first Pulumi program in TypeScriptYou will install the Pulumi CLI, keep state in a local folder, declare resources in a TypeScript program, preview and apply it, read its outputs, change it and watch the diff, then destroy and remove the stack.
- Use Terraform workspaces for separate environments, and know when not toYou will run one Terraform configuration as separate dev and staging workspaces with per-environment settings, find where each workspace keeps its state, guard every apply with a workspace check, and compare the result with a directory per environment.
- How to Keep Passwords and API Keys Out of GitYou will keep secrets in a git-ignored .env file, commit a safe template instead, stop a commit that contains a key with a pre-commit hook, and learn what to do when a secret has already been committed.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials