https://pulumi.com logo
m

most-lighter-95902

12/02/2021, 12:58 AM
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?
b

billowy-army-68599

12/02/2021, 1:15 AM
it needs to have a unique name, can you share your code?
m

most-lighter-95902

12/02/2021, 2:57 AM
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)
@billowy-army-68599
Thanks for looking into this BTW
@billowy-army-68599 still blocked by this - any idea why this is happening?
b

billowy-army-68599

12/02/2021, 11:18 PM
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
m

most-lighter-95902

12/03/2021, 3:15 AM
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.
a

ambitious-father-68746

12/06/2021, 10:11 PM
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
blabla
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.
20 Views