I was told that I can do `new StackReference` as m...
# general
I was told that I can do
new StackReference
as many times I need, but when I put it in a function and it gets called twice, I get
resource 'urn:pulumi:app-staging::ckc-test::pulumi:pulumi:StackReference::seunggs/ckc-test/app-build' registered twice (read and read)
error and
pulumi up
fails during preview. What am I doing wrong?
it needs to have a unique name, can you share your code?
Copy code
const main = async () => {

    const stack = pulumi.getStack()

    const checkStackExists = (qualifiedStackName: string) => {
        try {
            const stackRef = new pulumi.StackReference(qualifiedStackName)
            return true
        } catch (err) {
            return false

    const checkAppBuildStackExists = () => {
        return checkStackExists(`${organization}/${project}/app-build`)

    const getAppBuildStackOutputs = () => {
        const appBuildStackRef = new pulumi.StackReference(`${organization}/${project}/app-build`)
        const appEcrImageUrl = appBuildStackRef.getOutput('imageUrl') as pulumi.Output<string>
        return appEcrImageUrl

    if (stack === 'app-staging' || stack === 'app-prod') {
        const appEcrImageUrl = checkAppBuildStackExists ? getAppBuildStackOutputs() : ''

        const { AppStack } = await import('./pulumi/stacks/app')
        const appStackOutput = new AppStack('app-stack', {
            imageUrl: appEcrImageUrl,
        }, { provider: k8sProvider })
        return appStackOutput


export = main
This is roughly the code - not sure what you mean by unique name? I thought you get stack reference by stack’s fully qualified name which is unique of course (i.e. no two stacks with the same name)
Thanks for looking into this BTW
@billowy-army-68599 still blocked by this - any idea why this is happening?
sory I'm at re:invent this week so slow to respond. It's working by design, you'll need to modify your code to only populate the stack ref once
Oh right - re:invent. No worries. So you CANNOT do
new StackReference
multiple times then?
So if this is the case, any way to check if the stack exists? The problem is I cannot call
new StackReference
freely because it errors out if the stack doesn’t exist.
You can do multiple StackReferences if you specify a unique identifier as a separate parameter. StackReference acts as a resource, so you can't declare it twice.
Here's an example in Python:
Copy code
stack = pulumi.StackReference("unique_id_1", stack_name="blabla")
same_stack_again = pulumi.StackReference("unique_id_2", stack_name="blabla")
So I'm referencing the stack
twice. The other way of doing it is changing your code so that you only need to reference it once, but this give you options.