Stash
The Stash resource is a built-in Pulumi resource that allows you to save values to your stack's state for later retrieval. A stash takes a single input value and stores it in state, making it available as an output property. Stashes are commonly used to persist computed values, pass data between program executions, or save intermediate results that need to be accessed later.
Every Stash resource accepts any value as its input property. It then exposes two output properties — one for the value it stashed, and one that echoes back the value you most recently passed in:
output— the value saved in state. This value is stateful: it persists the originalinputvalue even after theinputproperty is changed in a later deployment.input— an echo of the most recent value passed to the resource. Reference this output when you need the current value rather than the stashed one.
INFO
The built-in Stash resource was added in Pulumi v3.208.0. Upgrade the Pulumi CLI if your version predates it.
Create a stash
To create a new stash, instantiate a Stash resource and provide a value for the input property. The stash stores this value in your stack's state and makes it available through the output property.
import * as pulumi from "@pulumi/pulumi";
const myStash = new pulumi.Stash("myStash", {
input: "Hello, World!",
});
export const stashedValue = myStash.output;Like any other resource, a Stash needs a name that is unique across your program.
Stashing complex values
The input property of a Stash resource can accept any value, including complex objects, arrays, and nested structures. The value is serialized as a Pulumi property value when stored in state.
const configStash = new pulumi.Stash("configStash", {
input: {
region: "us-west-2",
instanceType: "t3.micro",
tags: {
Environment: "production",
Team: "platform",
},
},
});Stashing secret values
The Stash resource respects secret annotations. If the input value is marked as a secret, the output is also secret, and the value is encrypted in your stack's state.
const apiKeyStash = new pulumi.Stash("apiKeyStash", {
input: pulumi.secret("my-secret-api-key"),
});
// The output is also marked as secret
export const apiKey = apiKeyStash.output;The CLI does not show the plaintext content of a stashed secret by default; it displays [secret] instead. Pass --show-secrets to the command to reveal the plaintext value.
Updating stashed values
To update the value stored in a Stash you need to replace it. Three ways to do that:
- Pass the
--target-replaceargument topulumi upto tell the engine to replace the stash. - Run
pulumi state taintto mark the resource for replacement on the next deployment. - Set the
replacementTriggerresource option to replace the stash whenever a trigger value changes.
Without a replacement, changes to the input property are reflected in the input output property, but the output property does not change. It continues to return the original value the Stash was constructed with.
In Pulumi HCL, the replacement trigger is the triggers_replace argument of terraform_data. Because the HCL examples on this page ignore changes to input, both input and output keep returning the stashed value until the resource is replaced, and the replacement stores the current value of input.
Deleting a stash
To delete a Stash resource, remove it from your program and run pulumi up. Pulumi removes the stash — and the value it holds — from your stack's state during the update.
Common use cases
The Stash resource is useful for keeping track of a computed value across deployments — for example, the first user to run the deployment, the time the stack was first created, or a generated random value that needs to stay stable.
Capturing the first deployment user
When you need to record who first deployed the infrastructure:
import * as pulumi from "@pulumi/pulumi";
import * as os from "os";
const firstDeployer = new pulumi.Stash("firstDeployer", {
input: os.userInfo().username,
});
// The output will always show the original deployer, even if others run updates later
export const originalDeployer = firstDeployer.output;Recording the initial creation time
When you need to persist the timestamp of when the infrastructure was first created:
import * as pulumi from "@pulumi/pulumi";
const creationTime = new pulumi.Stash("creationTime", {
input: new Date().toISOString(),
});
// This will always return the original creation time
export const firstDeployed = creationTime.output;Preserving a stable random value
Use a stash when you need a random value that remains constant across deployments. In these examples, generatePassword is your own helper function that produces a fresh value on every run; the stash is what keeps the first value stable.
import * as pulumi from "@pulumi/pulumi";
// Generate a random password once
const randomPassword = generatePassword();
// Stash it so it doesn't change on subsequent deployments
const passwordStash = new pulumi.Stash("passwordStash", {
input: pulumi.secret(randomPassword),
});
// Use the stashed password for database configuration
export const dbPassword = passwordStash.output;Next steps
- Resource options — including
replacementTrigger, used above to update a stashed value. - Stacks and state and backends — where stashed values are stored.
- Secrets — how Pulumi encrypts secret values in state.