ancient-nightfall-54764
05/19/2022, 8:14 AMstack 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
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.victorious-church-57397
05/19/2022, 10:10 AMrequireOutput
instead of getOutput
.apply
on the outputexport const ruleSgId = secgroup.id.apply((id) => id);
ancient-nightfall-54764
05/19/2022, 1:54 PM.apply
in the output, but the error still same.clever-sunset-76585
05/19/2022, 2:37 PMinfra
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.ancient-nightfall-54764
05/19/2022, 3:55 PMclever-sunset-76585
05/19/2022, 4:14 PMvpc.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.ancient-nightfall-54764
05/20/2022, 1:39 AMclever-sunset-76585
05/20/2022, 2:33 AMvpc.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.ancient-nightfall-54764
05/20/2022, 2:51 AMclever-sunset-76585
05/20/2022, 2:52 AM