https://pulumi.com logo
Title
d

damp-lock-9822

02/03/2023, 11:49 PM
Hi all, I'm using pulumi (with python) and need to access an Output property's raw value in order to do a string comparison. Is there any way to accomplish this? Everything I've tried has resulted in the property remaining an object of type Output[T]
m

miniature-musician-31262

02/03/2023, 11:54 PM
Hi there — yes, in order to get at the underlying/resolved value, you’ll need to use
apply
as described here: https://www.pulumi.com/docs/intro/concepts/inputs-outputs/?language=python#apply
d

damp-lock-9822

02/04/2023, 12:00 AM
I thought that the result of the call to apply is another Output object?
m

miniature-musician-31262

02/04/2023, 12:59 AM
Yes — the result of the call to
apply
is an
Output[T]
, but within the callback that’s passed to apply, you can work with the raw `T`:
bucket = s3.Bucket('my-bucket')

def compare(a, b):
    if a == b:
        print("match")
    else:
        print("no match")

bucket.id.apply(lambda id: compare(id, "some-string"))
You can also do something like this, which can be a little nicer to work with:
bucket = s3.Bucket('my-bucket')
some_known_value = "foo"

# Capture the result as an Output[bool].
is_match = bucket.id.apply(lambda id: id == some_known_value)

# Use its apply() to work with the result.  
is_match.apply(lambda m: print("Match!" if m else "No match."))
d

damp-lock-9822

02/06/2023, 6:33 PM
Okay thank you! I'll give that a shot