1. Answers
  2. Convert Pulumi StringOutput to string in TypeScript

How do I convert Pulumi StringOutput to string in TypeScript?

In this guide, we will demonstrate how to convert a Pulumi.StringOutput to a plain string in TypeScript. Pulumi uses outputs to represent values that are not available until the infrastructure is provisioned. Sometimes, you need to work with these outputs as plain strings. We will show you how to achieve this using the apply method provided by Pulumi.

import * as pulumi from "@pulumi/pulumi";

// Example resource that produces a StringOutput
const exampleOutput = pulumi.output("Hello, Pulumi!");

// Convert the StringOutput to a plain string
exampleOutput.apply(value => {
    // Here we can use the value as a plain string
    console.log(`The value is: ${value}`);
});

// Export the value to make it available outside the Pulumi program
export const plainString = exampleOutput.apply(value => value);

Key Points

  • Pulumi Outputs: Outputs represent values that are not available until runtime.
  • apply Method: Use the apply method to transform or extract the value from an output.
  • Plain String Conversion: The apply method allows you to work with the value as a plain string within the callback function.

Summary

We demonstrated how to convert a Pulumi.StringOutput to a plain string in TypeScript using the apply method. This approach allows you to work with the value extracted from the output within the callback function, enabling you to perform further operations or export the plain string as needed.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up