https://pulumi.com logo
#python
Title
# python
r

red-market-91256

08/03/2023, 6:51 PM
Very new to Pulumi, I've got what I believe is a super basic question but I'm just not quite understanding from the docs what the SDK is expecting me to do here. I have the following code (org/project name redacted):
Copy code
def myfunc():
    stack_ref = pulumi.StackReference('myorg/myproject/mystack')
    return pulumi.Output.format("{0}", stack_ref.get_output('url'))

print(myfunc())
When I try to run this I get no output and still get the warning:
Copy code
Calling __str__ on an Output[T] is not supported.
From the docs it says that using
format
should be using apply, and hence it should be wait'ed on and I shouldn't have
Output[T]
as a return. Any ideas what I'm missing?
p

proud-art-41399

08/03/2023, 7:20 PM
The docs states, that functions like
format
and
concat
are just wrappers around
apply
to create string outputs in an easier and more intuitive way. However, it still creates an
Output
that you need to use
apply
upon to access the actual value. The helper functions just serve the purpose of preparing the actual value in a form you need, composed of other outputs' values.
r

red-market-91256

08/03/2023, 7:28 PM
So is the recommendation to do something like this?
Copy code
return pulumi.Output.format("{0}", stack_ref.get_output('url')).apply(lambda x: x)
As that produces the same warning
p

proud-art-41399

08/03/2023, 7:31 PM
No, the
myfunc
function is just right. But to print the result, you'd have to use something like
Copy code
o = myfunc()
o.apply(lambda v: print(v))
r

red-market-91256

08/03/2023, 7:37 PM
I had tried that earlier, though one challenge here that I didn't mention is I am doing this within an async API itself. I'm not getting the error with that code but I am also not getting the display (and I changed it to
.apply(lambda v: print("test"))
just to test and it didn't display). I can't tell if this is because the API is completing before the output is returning. I sort of want to
await
this response but not sure the way to do that?
2 Views