How do I reference a stack that may not exist yet?...
# general
t
How do I reference a stack that may not exist yet? We first deploy a stack for Azure's API Management service (Stack A). We also deploy a stack for Azure Front Door (Stack B). We would like to reference the Front Door ID in created by
B
in stack
A
. Typical chicken and egg problem. One possible solution is to reference the stack and on the first deployment, do not create the resources that need the front door resource id since it does not exist. On the 2nd deployment, create the resources now that the stack (and the front door id output is available). The issue I seem to hit has that uses a stack reference causes an error that I cannot avoid:
Copy code
try {
  const stackRef = new StackReference(`org/project/stack`);
  const frontDoorId = test.getOutput("frontDoorId");
  console.log(frontDoorId);
} catch (error) {
  console.log(error);
  console.log("Stack does not exist yet. Will retry on next deployment.");
}
Every preview I run fails due to:
error: Preview failed: unknown stack "org/project/stack"
@limited-rainbow-51650 any advice here? Am I approaching this the wrong way?
@echoing-dinner-19531 any suggestions?
e
Tricky, but I think what might work is to just shell out to something like "pulumi stack --stack <name>" to see if it exists and then based on that new up the StackReference or fallback to some defaults
In general the chicken and egg, create if not exists, use if present else fallback, are all tricky related problems we keep thinking about
t
🤔 Good idea. Not perfect but at least that removes the need for any manual / click-ops stuff. Thanks Fraser.
are all tricky related problems we keep thinking about
🙏🏽 I know it's not easy to solve. Thanks.