https://pulumi.com logo
#getting-started
Title
# getting-started
a

ancient-nightfall-54764

05/19/2022, 8:14 AM
Hi Everyone, 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
Copy code
stack infra
index.ts
....
export const ruleSgId = secgroup.id;
export const subnetA = subneta.id;


stack dev
index.ts
...
const stackRef = new pulumi.StackReference("blabla/blabla/infra");
const secg = stackRef.getOutput("ruleSgId");
const subnet = stackRef.getOutput("subnetA");

subnetId: subnet,
vpcSecurityGroupIds: [ secg, ],
...
But when I run the code, the result was error like this
Copy code
error: Running program '/Users/mhw/WS/repos/pulumi' failed with an unhandled exception:
    TypeError: Cannot read properties of undefined (reading 'id')
https://www.pulumi.com/learn/building-with-pulumi/stack-references/ Thank you.
v

victorious-church-57397

05/19/2022, 10:10 AM
try using
requireOutput
instead of
getOutput
ah actually, the error is coming from the stack infra, try using
.apply
on the output
export const ruleSgId = secgroup.id.apply((id) => id);
a

ancient-nightfall-54764

05/19/2022, 1:54 PM
I added
.apply
in the output, but the error still same.
did you know example code like my case or something docs that explain like that.
c

clever-sunset-76585

05/19/2022, 2:37 PM
Based on the error message it seems that the
infra
stack is having a problem exporting the output due to
secgroup
or
subneta
being undefined. It is not clear which one is the issue without seeing more code.
a

ancient-nightfall-54764

05/19/2022, 3:55 PM
Hello @clever-sunset-76585, sorry I just push example code into github https://github.com/handrywahyudi/infra-using-pulumi. Maybe there is something wrong on my code. Thank you.
c

clever-sunset-76585

05/19/2022, 4:14 PM
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
vpc.ts
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
infra
. When you are running as the
dev
stack it will be
undefined
. So the issue doesn't seem to be because of stack references. It's an issue with line 7 in your
index.ts
file at the root of your repo. To make it work you could change it to add an if-condition in
index.ts
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
examples
repo for comparison of how to have multiple Pulumi projects within the same folder.
a

ancient-nightfall-54764

05/20/2022, 1:39 AM
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
c

clever-sunset-76585

05/20/2022, 2:33 AM
Well, to be precise, the reason your code isn't working is because the if-condition in
vpc.ts
only runs when your stack name is
infra
. So when you run the same code for your
dev
stack, the export in that file is never assigned a value even though the vpc does get created in the
infra
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.
a

ancient-nightfall-54764

05/20/2022, 2:51 AM
I restructured the code like this https://github.com/handrywahyudi/infra-using-pulumi and created project to each folder. It works 😁 Thank you, really appreciated πŸ™
c

clever-sunset-76585

05/20/2022, 2:52 AM
Great! πŸŽ‰πŸ‘
6 Views