https://pulumi.com logo
Title
s

steep-island-39848

05/24/2021, 10:33 PM
Hi all, I would like to check the length of an Output before using the value (in Python). Specifically, I’d like to write somthing like this, since if I don’t check, I get an index out of bounds when I create the service for the first time (this is for a GCP Cloud Run service):
if len(svc.statuses)>0:
        pulumi.export("url", Output.concat(svc.statuses[0].url))
But Output objects don’t have a length, and a try / catch doesn’t stop Pulumi from crashing. How would I check for that value to be available before exporting it?
1
l

little-cartoon-10569

05/24/2021, 10:35 PM
Output the result of the check. Do the check inside the apply, and output either the correct value, or the correct "there is no value" value (in typescript, that would be undefined).
s

steep-island-39848

05/24/2021, 10:36 PM
That makes sense, I’ll give that a shot, thanks
👍 1
r

red-match-15116

05/24/2021, 10:36 PM
url = svc.statuses.apply(lambda statuses: statuses[0].url if statuses else None)
pulumi.export("url", url)
👍 2
s

steep-island-39848

05/24/2021, 10:42 PM
That worked, thanks! ✌️
😛artypus-8bit: 1