@here can someone help answer <@U01DA1DQSKT>’s que...
# python
a
@here can someone help answer @jolly-camera-35709’s question? I have the same issue. I cannot seem to get the actual string value from a StackReference output.
b
That's because it's not a true string, it's an Output https://www.pulumi.com/docs/intro/concepts/programming-model/#outputs
w
To add to this, basically you need to use a pulumi mechanism to “print” out the value. For example, this code works:
Copy code
import pulumi
from pulumi import StackReference

other = StackReference(f"goo/foo/dev")
other_output = other.get_output("bucket_name");

# Export the name of the bucket
pulumi.export('other bucket_name', other_output
in that it outputs the output from stack
goo/foo/dev
to the terminal.
And here’s a hacky-hacky version that also does a print of the value - I can’t just do
print(other_output)
because it’s not a string out there in the main body of the code. But in the apply block, it is a string and so I can treat it as such:
Copy code
import pulumi
from pulumi import StackReference

other = StackReference(f"goo/foo/dev")
other_output = other.get_output("bucket_name");

# Export the name of the bucket
pulumi.export('other bucket_name', other_output)

def printval(arg):
  print("printing output")
  print(arg)
other_output.apply(lambda other_output: printval(other_output))
a
Thanks for the help! How would you persist that str value so that you can do some logic based on it?
Outside of passing this into another pulumi resource is it just impossible to perform logic on a stackreference output within your main.py?
w
Yes, but what’s the use-case exactly? If you are looking to make decisions about stacks based on outputs from other stacks, then the automation api may be the better answer. It would allow you to orchestrate actions across multiple stacks.
a
Admittedly I may have a contrived use-case as I'm simply exploring pulumi to get a feel for it. But, in my little demo I wanted to throw a simple hello world webserver that displayed the dns for a different pulumi stack webserver. I am generating the user_data for the ec2 instance and passing that to the pulumi ec2 resource.
The other stack dns is just empty bc I get None when I use interpolation on the user_data string with the output.
Given that this is a stack reference, it seems counterintuitive that pulumi doesn't know the value until main exits and kicks to pulumi
w
I think you would be able to use interpolation to create the user_data containing the dns name and set the user_data for the vm with that concatenated value. See
Output.concat()
example here: https://www.pulumi.com/docs/intro/concepts/programming-model/#outputs-and-strings