Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 36 ยท Resources and Programs

apply, interpolate, all and Other Pulumi Output Helpers

Work with Pulumi Output values: apply, interpolate, concat, all, unsecret, jsonStringify, lifting property access, secrets and common pitfalls, with runnable stand-ins.

Inputs and Outputs explained why values arrive later. This lesson is the toolbox: the helpers you use every day to transform, combine and format Output values, plus the rules that keep previews honest.

apply: transform one value

apply runs a function when the value is known and returns a new Output:

TypeScript
const upper = bucket.id.apply(id => id.toUpperCase());
const length = bucket.id.apply(id => id.length);

Python: bucket.id.apply(lambda i: i.upper()). Go: bucket.ID().ApplyT(func(id string) string { return strings.ToUpper(id) }).(pulumi.StringOutput).

Property access on outputs ("lifting")

In TypeScript, Python and others you can read properties of an object-valued Output directly, and Pulumi lifts the access for you:

TypeScript
const subnetId = vpc.subnets[0].id;           // Output<string>, no apply needed
const first = config.requireObject<{ name: string }[]>("users")[0];

This works for simple property and index access. For anything calculated, use apply.

interpolate, concat and format

TypeScript
const url = pulumi.interpolate`https://${bucket.bucketDomainName}/${key}`;
Python
url = pulumi.Output.concat("https://", bucket.bucket_domain_name, "/", key)
url2 = pulumi.Output.format("https://{0}/{1}", bucket.bucket_domain_name, key)

all: combine several

TypeScript
const pair = pulumi.all([bucket.id, bucket.arn]);          // Output<[string, string]>
const message = pair.apply(([id, arn]) => `${id} has ARN ${arn}`);

const named = pulumi.all({ id: bucket.id, arn: bucket.arn }); // object form

Python: pulumi.Output.all(bucket.id, bucket.arn).apply(lambda args: f"{args[0]} {args[1]}").

JSON that contains outputs

IAM policies are JSON with resource ARNs inside. Two ways:

TypeScript
// 1. apply and JSON.stringify
const policy = bucket.arn.apply(arn => JSON.stringify({
  Version: "2012-10-17",
  Statement: [{ Effect: "Allow", Action: "s3:GetObject", Resource: `${arn}/*` }],
}));

// 2. Output-aware helper (available in recent SDKs)
const policy2 = pulumi.jsonStringify({
  Version: "2012-10-17",
  Statement: [{ Effect: "Allow", Action: "s3:GetObject", Resource: pulumi.interpolate`${bucket.arn}/*` }],
});

Python has pulumi.Output.json_dumps(...).

Secrets

TypeScript
const token = pulumi.secret("abc123");                 // mark a plain value secret
const shown = pulumi.unsecret(token);                  // explicitly strip the flag (dangerous)
const derived = token.apply(t => `Bearer ${t}`);       // still secret

Any Output derived from a secret is secret. Details in Secrets in Config.

Output.create and promises

Output.create(value) wraps a plain value (or Promise); it is useful in helper functions that accept Input<T>:

TypeScript
function toLower(input: pulumi.Input<string>): pulumi.Output<string> {
  return pulumi.output(input).apply(s => s.toLowerCase());
}

pulumi.output(x) turns an Input into an Output; pulumi.unwrap and pulumi.Unwrap<T> convert deep structures. Accepting Input<T> in your own functions is a good habit because it makes them usable with both plain values and Outputs.

What not to do

TypeScript
// Bad: leaks a computed value into program flow
if (bucket.id === "x") { /* never true: it's an Output */ }

// Bad: creating a resource inside apply
bucket.id.apply(id => new aws.s3.BucketObject(id, { bucket: id }));

// Bad: for a loop over an Output list
subnetIds.apply(ids => ids.forEach(...));   // creates hidden resources

For lists you must loop over, rely on plain data (config values, constants) or create resources outside apply using the Output.apply result only for properties. If the list truly is only known after another resource exists, split the work into two stacks and read it with a StackReference.

Stand-in: build the helpers from scratch

The following stand-in implements apply, all, interpolate, property lifting through a Proxy, and secret propagation, so you can see each rule in a few lines. It is JavaScript, not the SDK.

JS Stand-in: Output helpers

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Stand-in: spot the bug

The next stand-in demonstrates why branching on an Output does not work, and what to branch on instead.

JS Stand-in: branching on outputs

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which helper builds a string from several Output values most readably in TypeScript?

    interpolate is a tagged template that accepts Outputs.
  2. 2. What is the result of token.apply(t => Bearer ${t}) when token is a secret?

    Secretness propagates through apply, all and interpolate.
  3. 3. Why is if (bucket.id === 'x') wrong?

    Branch on config or constants, not on Output values.
  4. 4. What should a helper function accept so it works with plain values and Outputs?

    Input<T> covers plain values, promises and Outputs; wrap with pulumi.output inside.

Summary

  • apply transforms one Output; all combines several; interpolate, concat and jsonStringify build strings and JSON.
  • Simple property and index access on Outputs is lifted automatically.
  • Secrets propagate through every helper; unsecret removes the flag deliberately.
  • Keep callbacks pure, do not create resources inside apply, and never branch on Outputs.
  • Accept Input<T> in reusable functions.

Next: Dependencies and Ordering