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.
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;
import pulumi
my_stash = pulumi.Stash("myStash", input="Hello, World!")
pulumi.export("stashedValue", my_stash.output)
package main
import (
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
myStash, err := pulumi.NewStash(ctx, "myStash", &pulumi.StashArgs{
Input: pulumi.String("Hello, World!"),
})
if err != nil {
return err
}
ctx.Export("stashedValue", myStash.Output)
return nil
})
}
using Pulumi;
return await Deployment.RunAsync(() =>
{
var myStash = new Stash("myStash", new StashArgs
{
Input = "Hello, World!",
});
return new Dictionary<string, object?>
{
["stashedValue"] = myStash.Output,
};
});
package myproject;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.resources.Stash;
import com.pulumi.resources.StashArgs;
public class App {
public static void main(String[] args) {
Pulumi.run(ctx -> {
var myStash = new Stash("myStash", StashArgs.builder()
.input("Hello, World!")
.build());
ctx.export("stashedValue", myStash.output());
});
}
}
resources:
myStash:
type: pulumi:index:Stash
properties:
input: "Hello, World!"
outputs:
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",
},
},
});
config_stash = pulumi.Stash("configStash", input={
"region": "us-west-2",
"instanceType": "t3.micro",
"tags": {
"Environment": "production",
"Team": "platform",
},
})
configStash, err := pulumi.NewStash(ctx, "configStash", &pulumi.StashArgs{
Input: pulumi.Map{
"region": pulumi.String("us-west-2"),
"instanceType": pulumi.String("t3.micro"),
"tags": pulumi.Map{
"Environment": pulumi.String("production"),
"Team": pulumi.String("platform"),
},
},
})
var configStash = new Stash("configStash", new StashArgs
{
Input = new Dictionary<string, object>
{
["region"] = "us-west-2",
["instanceType"] = "t3.micro",
["tags"] = new Dictionary<string, object>
{
["Environment"] = "production",
["Team"] = "platform",
},
},
});
var configStash = new Stash("configStash", StashArgs.builder()
.input(Map.of(
"region", "us-west-2",
"instanceType", "t3.micro",
"tags", Map.of(
"Environment", "production",
"Team", "platform"
)
))
.build());
resources:
configStash:
type: pulumi:index:Stash
properties:
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;
api_key_stash = pulumi.Stash("apiKeyStash",
input=pulumi.Output.secret("my-secret-api-key"))
# The output is also marked as secret
pulumi.export("apiKey", api_key_stash.output)
apiKeyStash, err := pulumi.NewStash(ctx, "apiKeyStash", &pulumi.StashArgs{
Input: pulumi.ToSecret(pulumi.String("my-secret-api-key")),
})
if err != nil {
return err
}
// The output is also marked as secret
ctx.Export("apiKey", apiKeyStash.Output)
var apiKeyStash = new Stash("apiKeyStash", new StashArgs
{
Input = Output.CreateSecret("my-secret-api-key"),
});
// The output is also marked as secret
return new Dictionary<string, object?>
{
["apiKey"] = apiKeyStash.Output,
};
var apiKeyStash = new Stash("apiKeyStash", StashArgs.builder()
.input(Output.ofSecret("my-secret-api-key"))
.build());
// The output is also marked as secret
ctx.export("apiKey", apiKeyStash.output());
resources:
apiKeyStash:
type: pulumi:index:Stash
properties:
input:
fn::secret: my-secret-api-key
outputs:
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.
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;
import pulumi
import getpass
first_deployer = pulumi.Stash("firstDeployer",
input=getpass.getuser())
# The output will always show the original deployer, even if others run updates later
pulumi.export("originalDeployer", first_deployer.output)
import (
"os"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
firstDeployer, err := pulumi.NewStash(ctx, "firstDeployer", &pulumi.StashArgs{
Input: pulumi.String(os.Getenv("USER")),
})
if err != nil {
return err
}
// The output will always show the original deployer, even if others run updates later
ctx.Export("originalDeployer", firstDeployer.Output)
return nil
})
}
using Pulumi;
using System;
return await Deployment.RunAsync(() =>
{
var firstDeployer = new Stash("firstDeployer", new StashArgs
{
Input = Environment.UserName,
});
// The output will always show the original deployer, even if others run updates later
return new Dictionary<string, object?>
{
["originalDeployer"] = firstDeployer.Output,
};
});
package myproject;
import com.pulumi.Pulumi;
import com.pulumi.resources.Stash;
import com.pulumi.resources.StashArgs;
public class App {
public static void main(String[] args) {
Pulumi.run(ctx -> {
var firstDeployer = new Stash("firstDeployer", StashArgs.builder()
.input(System.getProperty("user.name"))
.build());
// The output will always show the original deployer, even if others run updates later
ctx.export("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;
import pulumi
from datetime import datetime
creation_time = pulumi.Stash("creationTime",
input=datetime.now().isoformat())
# This will always return the original creation time
pulumi.export("firstDeployed", creation_time.output)
import (
"time"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
creationTime, err := pulumi.NewStash(ctx, "creationTime", &pulumi.StashArgs{
Input: pulumi.String(time.Now().Format(time.RFC3339)),
})
if err != nil {
return err
}
// This will always return the original creation time
ctx.Export("firstDeployed", creationTime.Output)
return nil
})
}
using Pulumi;
using System;
return await Deployment.RunAsync(() =>
{
var creationTime = new Stash("creationTime", new StashArgs
{
Input = DateTime.UtcNow.ToString("o"),
});
// This will always return the original creation time
return new Dictionary<string, object?>
{
["firstDeployed"] = creationTime.Output,
};
});
package myproject;
import java.time.Instant;
import com.pulumi.Pulumi;
import com.pulumi.resources.Stash;
import com.pulumi.resources.StashArgs;
public class App {
public static void main(String[] args) {
Pulumi.run(ctx -> {
var creationTime = new Stash("creationTime", StashArgs.builder()
.input(Instant.now().toString())
.build());
// This will always return the original creation time
ctx.export("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;
import pulumi
# Generate a random password once
random_password = generatePassword()
# Stash it so it doesn't change on subsequent deployments
password_stash = pulumi.Stash("passwordStash",
input=pulumi.Output.secret(random_password))
# Use the stashed password for database configuration
pulumi.export("dbPassword", password_stash.output)
import (
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
// Generate a random password once
randomPassword := generatePassword()
// Stash it so it doesn't change on subsequent deployments
passwordStash, err := pulumi.NewStash(ctx, "passwordStash", &pulumi.StashArgs{
Input: pulumi.ToSecret(randomPassword),
})
if err != nil {
return err
}
// Use the stashed password for database configuration
ctx.Export("dbPassword", passwordStash.Output)
return nil
})
}
using Pulumi;
return await Deployment.RunAsync(() =>
{
// Generate a random password once
var randomPassword = generatePassword();
// Stash it so it doesn't change on subsequent deployments
var passwordStash = new Stash("passwordStash", new StashArgs
{
Input = Output.CreateSecret(randomPassword),
});
// Use the stashed password for database configuration
return new Dictionary<string, object?>
{
["dbPassword"] = passwordStash.Output,
};
});
package myproject;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.resources.Stash;
import com.pulumi.resources.StashArgs;
public class App {
public static void main(String[] args) {
Pulumi.run(ctx -> {
// Generate a random password once
var randomPassword = generatePassword();
// Stash it so it doesn't change on subsequent deployments
var passwordStash = new Stash("passwordStash", StashArgs.builder()
.input(Output.ofSecret(randomPassword))
.build());
// Use the stashed password for database configuration
ctx.export("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.