I'm seeing an odd behavior when looping through so...
# getting-started
s
I'm seeing an odd behavior when looping through some AWS resource creation that I can only attribute to getting another stack’s output, specially I think the error is a result of lopping through and getting a stack reference each time within the loop, i.e.
stack
=
new
pulimi.StackReference(stackName)
. More specifically the errors occurs after that but before the next line that follows
myOutput
=
await
stack.requireOutputValue(‘myVal’)
. The error is “Duplicate resource URN ‘`urnpulumicurrentStack:currentProjectpulumipulumi:StackReference:stackINeed`’”. When I only loop one set of resources,
pulumi
preview
works fine. It is when I have 2 resources that iterate through the loop that I get the error. Hence, I can only attribute it to trying to get a reference to the stack that already exists in the current state of running
pulumi preview
. Am I way off in my thought? Thanks!
l
No. This is correct. Stacks have URNs, if you get the same stack twice, you'll have two URNs with the same value, which isn't allowed. You'll need to update your logic to not create StackReference objects for stacks that you already have StackReference ovbject for.
A likely explanation is that you're getting the stack reference object for the current stack. Inside your loop, you could explicitly check for this (
if (stackName == pulumi.getStack())
) and then
continue
.
s
Thanks @little-cartoon-10569 . I did think it could possibly be getting the current stack but I had checked that and it wasn't. So I could only attribute it to not being able to reference the same stack in the next iteration where the previous had called it. So if I understand, when I
pulumi preview
or
pulumi
up
and as the stack is being evaluated to know what to do (create/delete resources), I can't call a new StackReference on the same stack. That's where the collision happens.
l
I don't follow. You've said that it's not the current stack, so what does "I can't call a new StackReference on the same stack. That's where the collision happens." mean?
Also: it's very unusual for one stack to look up values from another stack in the same project. It is much more common for a stack in one project to look up a stack in a different project (e.g. the application dev stack looks up the infra us-west-2 stack, or similar). Are you confident that your stack relationships are optimal?
s
Sorry, I see how my statement was confusing. Yes, it's referencing a stack in another project. I meant that the next iteration of a loop containing a call to the same StackReference (a stack in another project I need it's outputs) is what errors when
pulumi preview
because the internal state of this preview already had this stack reference called:
Copy code
for (config of configs) {
 const stack = new pulumi.StackReference(stackInOtherProject);

 // exits here with Duplicate resource URN error

 const outputVal = await stack.requireOutputValue(‘myVal’);

 // some code here that is reached by config[0] but not config[1]
}
l
This is happening because stackInOtherProject has the same value twice? Is stackInOtherProject a value based on config? Or is it always the same value? If it's always the same value, you can move the stack reference constructor out of the loop.