How to convert Pulumi StringOutput to string?
TypeScriptIn Pulumi,
Output<T>
is a type that represents a value that will be resolved asynchronously. This is used for values that are not known until the infrastructure is deployed. For example, the address of a created database is not known until after Pulumi has created it, so it is represented asOutput<string>
.To perform operations on these
Output<T>
types, Pulumi provides theapply
method. You can use this method to provide a callback that will be invoked once the value is available. However, note that the result ofapply
is itself anOutput
.In TypeScript, suppose
foo
is apulumi.Output<string>
, e.g. a resource name.import * as pulumi from "@pulumi/pulumi"; let foo: pulumi.Output<string>; // Getting a pulumi.Output value out: const bar = foo.apply(fooValue => { console.log(`Inside the apply function: ${fooValue}`); // ... do something with fooValue ... return fooValue; });
In this example,
fooValue
inside of theapply
function is astring
and can be used as such.However,
bar
will still be apulumi.Output<string>
, because the apply function isn't executed until the infrastructure is actually being deployed, which happens asynchronously.Unfortunately, due to the nature of how Pulumi deploys resources, you cannot directly turn a
pulumi.Output
into a plain string or number as it is dependent on the completion of the asynchronous tasks.If you want to use an Output value in a context where a raw string is needed, you will need to re-architect your code to work with Outputs. Most Pulumi APIs are designed to automatically accept Outputs wherever raw strings are accepted, specifically to make this easier. If there are cases where this is not true, but should be, feel free to file an issue.