Outputs
It’s also possible to return output values from a Pulumi program.
Exporting outputs
At the previous step, we had to look up the Function App name and URL via an az
command. Let’s export the hostname of the App instead. Add the following line at the end of your index.ts
:
export const hostname = app.defaultHostname;
pulumi up
command automatically prints the output value at the end:
Updating (dev):
...
Outputs:
+ hostname: "facc74d2f2.azurewebsites.net"
Using outputs inside your program
If you hover over the hostname
variable in your program, you will see that it has type pulumi.Output<string>
not just string
. That’s because Pulumi runs your program before it creates any infrastructure, and it wouldn’t be able to put an actual string
into the variable. You can think of pulumi.Output<T>
as similar to Promise<T>
, although they are not the same thing.
Suppose, you want to export the full endpoint of your Function App. The following line is NOT CORRECT:
export const endpoint = `https://${app.defaultHostname}/api/hello`;
It fails at runtime because a value of pulumi.Output<string>
is interpolated into the string.
Instead, you should use one of the Pulumi’s helper functions:
// 'apply' can transform an Output to another Output with a function
export const endpoint1 = app.defaultHostname.apply(v => `https://${v}/api/hello`);
// 'interpolate' is a shortcut for Output interpolation
export const endpoint2 = pulumi.interpolate`https://${app.defaultHostname}/api/hello`;
Add these lines to the program and run pulumi up
again.
Mastering Output
s and their friends Input
s does take time. Refer to the docs for more details.
Checkpoint
Use the output endpoint1
or endpoint2
(they are the same) to make another request to your Function App and make sure it still returns a greeting.
If you have any difficulties, compare your code with this sample.