1. How to convert Pulumi outputs to strings?

    TypeScript

    Pulumi's Output type represents values that are computed asynchronously in a Pulumi program. It's a fundamental part of how Pulumi deals with the challenge of using async code at all levels of a Pulumi program.

    The most common scenarios where you'll need to work with Outputs include:

    • When you use any of the get functions (like aws.s3.getBucket)
    • When you refer to properties of other resources you've created in your Pulumi program (e.g. referring to bucket.bucketName where bucket is an instance of new aws.s3.Bucket)

    Output values can be transformed using the apply method, which you can use with functions that manipulate the raw value of an Output.

    Below is an example of converting an Output from a AWS S3 bucket to a string.

    import * as pulumi from "@pulumi/pulumi"; import * as aws from "@pulumi/aws"; const bucket = new aws.s3.Bucket("my-bucket"); // Outputs are a pulumi type that represent a value that is not yet known, // here we're transforming this output into a string const bucketNameString: pulumi.Output<string> = bucket.id.apply(id => { if (typeof id === 'string') return id; throw new Error(`Expected a string but got: ${id}`); });

    In this example, bucket.id is an "Output". To use its value, we apply a callback function. The callback function takes the actual bucket id as id, and returns a string representation of it. The result of the apply, bucketNameString, is itself an Output, but now of type string. Note that this transformation is also taking care by typescript typing system.

    To get the actual string value, you will need to use pulumi.Output.unsecret(bucketNameString).promise(). This will return a native Javascript Promise of a string, that you can await on. Remember that you can't await at the top level of a Pulumi program because it has to have a non-promise callback.

    You can learn more about outputs in Pulumi in the documentation.