stackRef
View SourceType-safe cross-stack references that allow you to access outputs from other Pulumi stacks with full TypeScript support. Essential for multi-stack architectures where stateful infrastructure is separated from application stacks.
import * as pulumi from "@pulumi/pulumi";import * as saws from "@stackattack/aws";
function env() { const ctx = saws.context();
const certificate = saws.certificate(ctx, { domain: "mydomain.dev", wildcard: true });
return { certificate };}
function app() { const ctx = saws.context(); const config = new pulumi.Config();
const envStack = saws.stackRef(config.require('env-stack'), env); const certificate = envStack.require('certificate');
const bucket = saws.bucket(ctx, { paths: ["./dist"] });
const site = saws.staticSite(ctx, { domain: "docs.mydomain.dev", certificate, bucket, adapter: saws.astroAdapter() });
return { url: site.url };}
export default () => saws.select({ env, app });
Reference stacks using Pulumi config to specify stack names:
# Configure the env stack referencepulumi config set stack-type app -s my-app-stackpulumi config set env-stack my-env-stack -s my-app-stack
# Deploy with type-safe access to env stack outputspulumi up
The function parameter serves as a type template - it defines the shape of the referenced stack’s outputs without being executed. This enables full TypeScript IntelliSense and type checking for cross-stack references. See the Structuring Stacks guide for comprehensive multi-stack patterns.
Functions
Section titled “Functions”stackRef
Section titled “stackRef”Creates a reference to another Pulumi stack’s outputs.
function stackRef(stack: string, func: (args: never[]) => Output): StackRef<Output>
Parameters
Section titled “Parameters”stack
(string
) - The stack reference stringfunc
((args: never[]) => Output
) - Function that defines the output type structure
Returns
Section titled “Returns”- (
StackRef
<Output>
) - Creates a reference to another Pulumi stack’s outputs.
StackRef
Section titled “StackRef”Interface for referencing outputs from another Pulumi stack.
type StackRef = { get: (key: K) => AsOutput<T[K] | undefined>; require: (key: K) => AsOutput<T[K]> }