← Recipes

阅读中文版本

Share Outputs Across Pulumi Stacks

Export stack outputs, read them from another stack with StackReference, and keep dependency direction acyclic so infrastructure composes cleanly.

~35 minpulumiiacinfrastructure

Task

Compose Pulumi stacks by consuming another stack outputs safely.

When to use this

Two stacks need to share a network id, but it is copy-pasted between them, so a change in one silently breaks the other.

Prerequisites

  • Pulumi CLI
  • Two or more stacks in the same organization

Steps

1. Export the values a downstream stack needs as stack outputs.

Expected output: The outputs are part of the stack contract.

2. Read them with StackReference and pick the specific output.

Expected output: The consumer gets the current value at deploy time.

3. Keep dependency direction one way to avoid cycles.

Expected output: Stacks have a clear order.

4. Guard against a missing or renamed output with an explicit check.

pulumi stack output vpcId --stack prod

Expected output: A contract change fails loudly.

Troubleshooting

Problem

Output renamed breaks consumer

Solution

Treat outputs as a contract and version them; add a check that fails with a clear message.

Problem

Stack update order

Solution

Deploy the producer before the consumer in CI so references resolve.

Limitations

  • Stack references create a deployment dependency, so the consumer cannot deploy before the producer succeeds.
  • They share plain values, not full resource graphs, so cross-stack changes are not automatically modeled.

Context

Compose Pulumi stacks by consuming another stack outputs safely. Two stacks need to share a network id, but it is copy-pasted between them, so a change in one silently breaks the other.

Steps

  1. Export the values a downstream stack needs as stack outputs. Expected: The outputs are part of the stack contract.
  2. Read them with StackReference and pick the specific output. Expected: The consumer gets the current value at deploy time.
  3. Keep dependency direction one way to avoid cycles. Expected: Stacks have a clear order.
  4. Guard against a missing or renamed output with an explicit check. Expected: A contract change fails loudly.
const network = new pulumi.StackReference('acme/network/prod');
const vpcId = network.requireOutput('vpcId');
const subnet = new aws.ec2.Subnet('app', { vpcId });

Verify

  • The outputs are part of the stack contract.
  • The consumer gets the current value at deploy time.
  • Stacks have a clear order.
  • A contract change fails loudly.

Limits

  • Stack references create a deployment dependency, so the consumer cannot deploy before the producer succeeds.
  • They share plain values, not full resource graphs, so cross-stack changes are not automatically modeled.