https://pulumi.com logo
Title
w

witty-vegetable-61961

05/30/2022, 11:44 PM
hi all, I am pretty sure this is a comon question but how can I get the value of an output? Essentially the problem here- https://stackoverflow.com/questions/62561660/how-to-convert-pulumi-outputt-to-string
l

little-cartoon-10569

05/30/2022, 11:56 PM
If you are not passing it to another Pulumi resources: use apply, and put your code inside the callback that apply generates. Example in Typescript:
valueAsOutput.apply(value => console.log(`The value is ${value}`));
If you are passing it to another Pulumi resource: don't worry about it, Pulumi resources can use outputs just fine, even though the docs say "string".
If you are passing it to another Pulumi resource but need to change it a bit: use apply, but pass the returned value to the resource. Example in Typescript:
const newResource = new provider.NewResource("example", {
  value: oldResource.apply(old => `Value for new resource is ${old}`)
});
There's a shortcut for strings:
const newResource = new provider.NewResource("example", {
  value: pulumi.interpolate`Value for new resource is ${oldResource}`
});
m

mysterious-hamburger-19066

05/31/2022, 5:01 AM
calling apply on an Output only returns another Output in python. Is the behavior different in typescript?
l

little-cartoon-10569

05/31/2022, 5:03 AM
No. This is correct.
You need to put your code that uses the value inside the apply. Not at the "top" level.
The only things that can access the value in an output are Pulumi resource constructors, and
apply()
e

echoing-dinner-19531

05/31/2022, 6:23 AM
Note that during preview the value of Outputs might not exist (if for example it's the output from a resource not yet created). In those cases a ".value" on Output would either have to throw an error or return some "undefined" value. There's also the risk that once you've got a plain value the SDK loses all tracking of dependencies and secretness that Output can track. Both of these combined led to the decision that it was a better UX to just not give a method to get the value out and ensure users kept things wrapped in Outputs.
w

witty-vegetable-61961

05/31/2022, 9:52 AM
ok let me try this. I will let you know if I have any issues.
Ok so picking this up again, this is the code: var clientId = clientApp.ClientId.Apply(x => secret == x); Would this be correct to gt the client id as an output variable?
l

little-cartoon-10569

06/08/2022, 11:18 PM
No. That will create a new Output<boolean>.
clientApp.ClientId
is the output you want, you don't need to apply to use it.
You do need to apply to get at the string value inside it, if that's what you need to do. What do you want to do with the ClientId? Do you want to pass it to another Pulumi resource?
w

witty-vegetable-61961

06/08/2022, 11:21 PM
Hi,
I will be passing this to a console app which will persist it to Octopus Deploy
l

little-cartoon-10569

06/08/2022, 11:33 PM
Then you will need to run that console app from inside the apply.
Pseudocode:
clientApp.ClientId.Apply(id => "consoleApp".exec(id))
Note that this will run every time
pulumi up
is run. This might not be what you want.
w

witty-vegetable-61961

06/08/2022, 11:35 PM
Ah I see! Is there a way I can build a collection of the values I need and call the console app once?
l

little-cartoon-10569

06/08/2022, 11:36 PM
Yes. There's a function
All()
that does this. Which language are you using?
w

witty-vegetable-61961

06/09/2022, 8:34 AM
c#
l

little-cartoon-10569

06/09/2022, 8:42 AM
I can't find the C# equivalent of pulumi.all().... I'm sure there's something though 🤔
w

witty-vegetable-61961

06/09/2022, 8:48 AM
l

little-cartoon-10569

06/09/2022, 9:12 AM
That's it. Output.All().
e

echoing-dinner-19531

06/09/2022, 9:13 AM
I will be passing this to a console app which will persist it to Octopus Deploy
You might want to give the pulumi command package a try for that (https://www.pulumi.com/registry/packages/command/)
w

witty-vegetable-61961

06/09/2022, 12:41 PM
Ok so I have this code: var clientId = clientApp.ClientId.Apply<string>(x => MyStack.Test("", "")); where Test is a method that can invoke the console app. Using all(), that takes a paramas array and then I can pass that into a method to do whatever I need with the values. I will try the first approach and I can use the command library too. Thanks guys!