https://pulumi.com logo
#automation-api
Title
# automation-api
m

most-lighter-95902

11/24/2021, 5:23 AM
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

proud-pizza-80589

11/24/2021, 8:28 AM
I use it a lot, even across non automation and automation programs it works like a charm
c

crooked-pillow-11944

11/24/2021, 12:22 PM
Same
b

bored-oyster-3147

11/24/2021, 1:52 PM
Does the stack that you are referencing reside in the same backend?
m

most-lighter-95902

11/24/2021, 4:31 PM
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

bored-oyster-3147

11/24/2021, 5:07 PM
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

most-lighter-95902

11/24/2021, 5:18 PM
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

bored-oyster-3147

11/24/2021, 6:38 PM
Glad you figured it out!
👍 1
m

most-lighter-95902

11/24/2021, 10:55 PM
@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

bored-oyster-3147

11/24/2021, 10:57 PM
Have you tried an output of a string array instead of an array of string outputs?
m

most-lighter-95902

11/24/2021, 10:59 PM
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

bored-oyster-3147

11/24/2021, 11:03 PM
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

most-lighter-95902

11/24/2021, 11:21 PM
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

bored-oyster-3147

11/25/2021, 4:43 AM
Not within the program, you would either need to check beforehand or use automation api.
m

most-lighter-95902

11/25/2021, 4:57 AM
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

crooked-pillow-11944

11/25/2021, 1:36 PM
You can have as many as you like
👍 1
☝️ 1
m

most-lighter-95902

11/26/2021, 3:48 AM
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

bored-oyster-3147

12/04/2021, 4:15 AM
Do they have the same name? You can't have two resources with the same name