https://pulumi.com logo
#getting-started
Title
# getting-started
t

thousands-area-40147

09/05/2022, 2:57 PM
Hey gang, is there any way to check the existence of a stack before creating a
new StackReference()
for it? Asking because the error
StackReference()
throws if the stack doesn't exist seems to be uncatchable. This is for Typescript btw, so maybe I should've asked it in #typescript instead 😅
v

victorious-church-57397

09/05/2022, 3:04 PM
might be a bit hacky, but you could use the pulumi
Command
resource to run
pulumi stack ls
and then you could check in the output of that if the stack exists
t

thousands-area-40147

09/05/2022, 3:05 PM
Yeah, that was my first hack, but unfortunately it doesn't work with the drift detection we're running in the cloud.
v

victorious-church-57397

09/05/2022, 3:06 PM
ah, i dont think theres a method for listing stacks programatically - you have to use the command afaik. have you looked at the automation api? that might have something in it you can use
t

thousands-area-40147

09/05/2022, 3:08 PM
I'll look around some more, thanks for your input 😄
v

victorious-church-57397

09/05/2022, 3:08 PM
Copy code
The Pulumi Automation API is a programmatic interface for running Pulumi programs without the Pulumi CLI. Conceptually, this can be thought of as encapsulating the functionality of the CLI (pulumi up, pulumi preview, pulumi destroy, pulumi stack init, etc.) but with more flexibility. It is a strongly typed and safe way to use Pulumi in embedded contexts such as web servers without having to shell out to a CLI.
l

limited-rainbow-51650

09/06/2022, 6:29 AM
In code, use the Pulumi Service REST API and you can list all stacks, or get a specific stack. Based on the HTTP return code, I think you have what you need: https://www.pulumi.com/docs/reference/service-rest-api/#get-stack
1
🙏 1
6 Views