https://pulumi.com logo
#general
Title
# general
b

broad-doctor-18421

11/28/2023, 9:38 PM
I have a duplicate URN error and it's quite frustrating as even
pulumi preview -v=11
doesn't give me any diagnostic errors, as to where they are both coming from. I've tried introducing a singleton pattern in a stack refs call to ensure I'm only calling it once but that doesn't seem to be compatible with pulumi.
Copy code
Diagnostics:
  pulumi:pulumi:StackReference (organization/vpc/dev):
    error: Duplicate resource URN 'urn:pulumi:dev::LB::pulumi:pulumi:StackReference::organization/vpc/dev'; try giving it a unique name
How do I get more information out of pulumi to help figure out where this is happening?
l

little-cartoon-10569

11/28/2023, 9:45 PM
That seems to have all the information in it. You are creating two stack reference objects referring to the same stack. This isn't allowed. You need to pass the stack reference around.
Good practice is to create stack references only at the top level of your project, so it should be easy to enumerate all the calls to
new StackReference()
and look for the duplicate.
b

broad-doctor-18421

11/28/2023, 9:50 PM
it would seem like it would be easy to find. It does not appear to be - unless my IDE i just not able to do text search lol We have a mono repo, with multiple projects in it. Because of a deficiency of the python/pulumi combo, we have to do this:
Copy code
import sys
sys.path.append("../../")
from account.__main__ import Outputs
Where Outputs are an enum of the output keys. I'm not sure how else to import something from a sister python project, or if this is the culprit.
l

little-cartoon-10569

11/28/2023, 9:52 PM
If I'm understanding the problem correctly, the other project isn't relevant to solving the problem. In the current project, you're constructing the same StackReference object twice. This isn't allowed. It's a local error, not related to the project that contains the stack.
It's like creating two buckets with the same name. It's just not allowed. You need to find the code in the current project and remove the duplication.
b

broad-doctor-18421

11/28/2023, 10:06 PM
ok i will refactor some things and try to simplify. Is there a reason why a singleton pattern wouldn't work with python here? That would be a much easier approach than to pass stacks around to all the components. Thanks for the insight.
l

little-cartoon-10569

11/28/2023, 10:07 PM
Yes, the reason is that stack references are objects in state, just like everything else. Their names are look-up indices into state, and must be unique. There's no reason you can't have multiple references to the same stack, you just need to use a different name for every one.
Also: having StackReferences constructed in unit-testable code makes that unit untestable. Unfortunately, the Pulumi test engine does not handle those sorts of objects well at all.
If you're not writing unit test code, that's not a problem.
b

broad-doctor-18421

11/28/2023, 10:10 PM
so I could do
Copy code
pulumi.StackReference(name="donkey", stack_name="organization/vpc/dev")
        pulumi.StackReference(name="mule", stack_name="organization/vpc/dev")
Those named stack refs aren't in the examples I can find anywhere but it's in the StackReference class
doesn't seem like the code in StackReference handles it the way I might expect from those options.
so I guess not
l

little-cartoon-10569

11/28/2023, 10:10 PM
Re:
Is there a reason why a singleton pattern wouldn't work with python here?
This isn't singleton. You're creating two different objects. The singleton pattern would create only one object, and return it anywhere anyone wanted it.
Sorry, I didn't get that previous question. Can you rephrase? The code you posted looks good.
e

echoing-dinner-19531

11/28/2023, 10:30 PM
so I could do
Yeh that code is fine, the two references are to the same stack but they have unique logical names for the deployment engine to track.
A little pointless (they'll return the same data) but it'll work
b

broad-doctor-18421

11/28/2023, 10:31 PM
here we coalesce non dev stacks to dev stacks so they can use the same vpc, and other stacks will be added here.
I guess in python not all singletons are created equal. There seems to be 3 ways of creating them, maybe some worked in python 2 but I've got around this error for now using this pattern. For posterity:
l

little-cartoon-10569

11/28/2023, 10:33 PM
Yep, so the problem with that code is that you're configuring it with only the name. You need to configure with the name and stack name separately, as in your previous snippet.
e

echoing-dinner-19531

11/28/2023, 10:35 PM
Yeh if you key that StackReference by the current_stack as it's name, then set the name argment to the stack to read I'd think it would then be unique
pulumi.StackReference(f"stackref-{current_stack}, name=f"organization/vpc/{vpc_stack_name}")
I think
b

broad-doctor-18421

11/28/2023, 10:37 PM
but if it'll only exist once, due to the singleton, I should be good ? I found in the outputs, when I gave it a name, it would use the name instead of the "organization/vpc/dev".
Copy code
+   ├─ aws:cloudwatch:LogGroup                       /ec2/authentication/stage/app  create
 +   ├─ aws:iam:Role                                  ec2_instance_profile_role      create
     └─ pulumi:pulumi:StackReference                  donkey                      1 error
but this error was a bit far back in my terminal history so maybe it's not trustworthy
l

little-cartoon-10569

11/28/2023, 10:38 PM
Yes, if it exists only once, you'll be good. I inferred earlier that your pattern needs to be able to create a stack reference in one piece of code but multiple times. If that was the case, you'd either need to pass in a name parameter to that piece of code (and ensure the name is different every time it's called), or use a random name.
b

broad-doctor-18421

11/28/2023, 10:40 PM
alright. I guess it would be nice if pulumi could implement this as a singleton in the back end, so this problem was transparent to users. Really appreciate the help. @echoing-dinner-19531 and @little-cartoon-10569
l

little-cartoon-10569

11/28/2023, 10:41 PM
If they did that, then StackReferences wouldn't be normal Pulumi resources. This solution doesn't work awesomely for your use case, but it does keep the learning curve a little gentler because of the common pattern.