Using Output Helpers
Pulumi's SDKs include helper functions designed for the most common output manipulation tasks: constructing strings from output values and working with JSON. These helpers wrap apply, which transforms the value inside a single output, and all, which combines several outputs into one, but expose a more concise interface that closely mirrors each language's native string and JSON facilities.
Use a helper when you need to build a string from one or more outputs or serialize a data structure that contains outputs to JSON. Use apply or all directly when the transformation you need goes beyond building a string or converting to and from JSON.
String interpolation
Pulumi's string interpolation helpers let you construct a string from one or more output values without calling apply or all explicitly. The helpers work for both a single output and multiple outputs, so they serve as a shorthand regardless of how many values you are combining.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
const bucket = new aws.s3.Bucket("bucket");
const file = new aws.s3.BucketObject("bucket-object", {
bucket: bucket.id,
key: "some-file.txt",
content: "some-content",
});
// concat takes a list of args and concatenates all of them into a single output:
export const s3Url1: pulumi.Output<string> = pulumi.concat("s3://", bucket.bucket, "/", file.key);
// interpolate takes a JavaScript template literal and expands outputs correctly:
export const s3Url2: pulumi.Output<string> = pulumi.interpolate`s3://${bucket.bucket}/${file.key}`;TypeScript and JavaScript provide two string helpers:
pulumi.interpolate— A tagged template literal that accepts Pulumi outputs directly inside${}expressions. It is the most idiomatic option in TypeScript and handles any number of values.pulumi.concat()— Concatenates a list of strings and outputs into a singleOutput<string>. Useful when you are building a string from a dynamic list of values rather than a fixed template.
For more details, see the Node.js SDK documentation.
JSON helpers
Many cloud resources accept JSON strings as inputs — IAM policies, Lambda function configurations, S3 bucket policies, and others. Pulumi's JSON helpers let you work with data structures that contain outputs without needing to call apply manually to extract the underlying values.
Converting outputs to JSON strings
If you need to produce a JSON string from a data structure that contains one or more output values, use one of the JSON stringify helpers. These helpers accept a mix of plain values and Pulumi outputs, serialize the entire structure to JSON, and return an Output<string> suitable for passing to another resource as an input.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// Get the account ID of the current user as a Pulumi output.
const accountID = aws.getCallerIdentityOutput().accountId;
// Create an S3 bucket.
const bucket = new aws.s3.Bucket("my-bucket");
// Create an S3 bucket policy allowing anyone in the account to list the contents of the bucket.
const policy = new aws.s3.BucketPolicy("my-bucket-policy", {
bucket: bucket.id,
policy: pulumi.jsonStringify({
Version: "2012-10-17",
Statement: [
{
Effect: "Allow",
Principal: {
AWS: pulumi.interpolate`arn:aws:iam::${accountID}:root`,
},
Action: "s3:ListBucket",
Resource: bucket.arn,
},
],
}),
});
// Export the name of the bucket.
export const bucketName = bucket.id;pulumi.jsonStringify()— Accepts a value or output object and returns anOutput<string>of its JSON representation. Equivalent in behavior toJSON.stringify().
For more details, see the Node.js SDK documentation.
Converting JSON strings to outputs
If you have an output in the form of a JSON string and need to read individual fields from it or pass it to a function that expects a plain object, use one of the JSON parse helpers. These accept an output containing a JSON string and return a deserialized output.
import * as pulumi from "@pulumi/pulumi";
const jsonIAMPolicy = pulumi.output(`{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"s3:ListAllMyBuckets",
"s3:GetBucketLocation"
],
"Resource": "*"
},
{
"Sid": "VisualEditor1",
"Effect": "Allow",
"Action": "s3:*",
"Resource": "arn:aws:s3:::my-bucket"
}
]
}`);
// Parse the string output.
const policyWithNoStatements: pulumi.Output<object> = pulumi.jsonParse(jsonIAMPolicy).apply(policy => {
// Empty the policy's Statements list.
policy.Statement = [];
return policy;
});
// Export the modified policy.
export const policy = policyWithNoStatements;pulumi.jsonParse()— Accepts anOutput<string>and returns anOutput<any>of the parsed value. Equivalent in behavior toJSON.parse().
For more details, see the Node.js SDK documentation.