I want to ask something regarding pulumi stack reference. I have different stack (infra and dev), I just want to get output from infra stack and using that into my ec2 resource.
So I have code like this
Thanks for putting up the code on GH. If I understood what you are trying to do, it seems that you have two stacks from the same codebase that creates different resources based on the stack name. That's certainly a unique way of doing it. Why not have the two stacks as two separate Pulumi projects instead?The way you are trying to access the vpc export from your
won't work because you have the variable in that file guarded by an if-condition that would only be true when this stack is
. When you are running as the
stack it will be
. So the issue doesn't seem to be because of stack references. It's an issue with line 7 in your
file at the root of your repo. To make it work you could change it to add an if-condition in
too to see what the current stack is.Structuring your app the way you have it now is confusing to be honest. Is there a reason you do not want to have the infra stack as its own Pulumi project? The code is likely to get more complicated with more conditional branches all over the place the way it is right now. Have a look at the
repo for comparison of how to have multiple Pulumi projects within the same folder.
4 months ago
Haha sorry, I am not follow the structure of file pattern, because I just want to save my resource (even though different env ) in one folder and I have vpc for global that used by different env resource. So the point is my code isn’t work because if-else condition in stack? and I need to restructure the code to separate the resource in different pulumi project at each folder. let me try first.
Thank you for your advice @clever-sunset-76585
4 months ago
Well, to be precise, the reason your code isn't working is because the if-condition in
only runs when your stack name is
. So when you run the same code for your
stack, the export in that file is never assigned a value even though the vpc does get created in the
stack. There is a way to get it to work but honestly I think you'll find it more confusing to work with it over time.