`StackReference` doesn’t seem to work inside Autom...
# automation-api
m
StackReference
doesn’t seem to work inside Automation API (at least when it’s not run inside the same program) - is this expected? If it’s not available, what options do I have for accessing another stack’s outputs?
p
I use it a lot, even across non automation and automation programs it works like a charm
c
Same
b
Does the stack that you are referencing reside in the same backend?
m
Hmm interesting - so maybe StackReference isn’t the problem. It’s using the same backend but in different endpoints.
Copy code
const clusterStackRef = new pulumi.StackReference('<org>/<project>/cluster')
Copy code
const clusterName = clusterStackRef.getOutput('clusterName').apply(t => console.log(t))
This outputs
undefined
b
When you say “same backend but different endpoints” what do you mean? Are you using the Pulumi hosted backend or self hosted? If you are using self hosted, are the backend paths provided to each referencing project identical? For instance if you have a project in s3://my-bucket/folder-one And s3://my-bucket/folder-two Those are separate backends even though they are in the same bucket, so your stack references won't work.
m
Actually it’s local (and Pulumi hosted) but using automation API to run multiple stacks separately. So the main program is calling the run function (where
LocalWorkspace.createOrSelectStack
and
stack.up
is being called) multiple times in sequence to set up multiple stacks
i.e.
Copy code
run('cluster')
run('cluster_autoscaler')
run('db')
run('app')
...
If I run
pulumi stack ls
the created stacks show up fine
Doesn’t that mean
StackReference
should work then?
Also, I’m using the
StackReference
inside the program (i.e.
program
arg of
await LocalWorkspace.createOrSelectStack
). That’s supposed to work assuming this local setup, correct?
Oh wait, I found something strange
In Pulumi dashboard, there seems to be two separate projects with the same name?
One of them shows the cluster stack and another one shows cluster_autoscaler where I’m using StackReference
But when I click on the one with cluster stack, it shows all stacks
Also
pulumi stack ls
shows all stacks so maybe this is just a UI thing
@bored-oyster-3147 I figured it out… it was a silly mistake on my part. I was outputting the values from the stack into { <stack name>: outputs } so when I do getOutput for a particular output value, it was
undefined
. Thanks for looking into this!
b
Glad you figured it out!
👍 1
m
@bored-oyster-3147 Since I have you, I have another quick question - I’m exporting an array as a stack output from a custom component resource which has a type (
Promise<pulumi.Output<string>[]>
). But when I do a
StackReference.getOutput
, the type is
pulumi.Output<any>
. I can’t seem to use
as pulumi.Output<string>[]
on this, so what’s the best practice in keeping the type consistent when using
StackReference
outputs?
b
Have you tried an output of a string array instead of an array of string outputs?
m
I’m a big of a noob in Typescript - isn’t
pulumi.Output<string>[]
not an output of a string array?
Also, no matter what I output,
StackReference.getOutput
seems to output
pulumi.Output<any>
- am I wrong?
b
I use the dotnet sdk primarily so I would have to look at the reference for TS and see how you're supposed to cast it and I'm away from my machine currently. But no,
pulumi.Output<string>[]
is an array of string outputs.
pulumi.Output<string[]>
would be an output of string array. See the difference?
m
Ah ok I’ll try that - thanks!
@bored-oyster-3147 sorry to keep bugging you, but is there a way to check if a stack exists without throwing an error? Right now, if I use a
StackReference
and the stack doesn’t exist yet, it throws an error and I’m not sure what the best way would be to handle it
b
Not within the program, you would either need to check beforehand or use automation api.
m
Hmm OK thanks
And just to confirm you can’t have multiple
new pulumi.StackReference
in the same program right? You can only instantiate it once?
c
You can have as many as you like
👍 1
☝️ 1
m
Thanks!
@crooked-pillow-11944 I’m getting this error when I do
new StackReference
more than once:
Copy code
error: resource 'urn:pulumi:app-staging::ckc-test::pulumi:pulumi:StackReference::seunggs/ckc-test/app-build' registered twice (read and read)
What am I doing wrong here?
b
Do they have the same name? You can't have two resources with the same name