I have two stacks, one for core infrastructure lik...
# general
w
I have two stacks, one for core infrastructure like VPC and one for application stuff. I want to reference the VPC in the other stack with all info like subnets etc. How do I do this with just the vpcId as an Output/Input?
b
w
I am. I am looking for a concrete example how to reference the VPC so it's not recreate in the second stack and still has all info like subnets.
One of my tries:
Copy code
const vpcId = infra.requireOutputValue('vpcId');
const vpc = new awsx.ec2.Vpc('staging', { vpcId });
export const vpcSubnets = vpc.publicSubnetIds;
vpcSubnets will be empty here.
This seems like such a basic thing and I don't know why this is nowhere in the docs. This SO question has the same problem: https://stackoverflow.com/questions/62631286/get-existing-vpc-for-use-within-a-pulumi-stack The solution here seems to suggest you have to copy over stuff like subnetIds as well which seems weird to me. Shouldn't all this stuff be available through the StackReference if I have the vpcId?
b
@wonderful-napkin-50018 you can export the whole resource, using
export const vpc
w
Okay, how do I use that in the other project? In the end I will need an
awsx.ec2.Vpc
object (to pass to other objects)
r
It sounds like you actually want to pass around this
Vpc
object. You can export the vpc’s id as a stack reference and in another stack “import” it into your code with a resource’s
.get()
method. https://www.pulumi.com/docs/reference/pkg/aws/ec2/vpc/#look-up EDIT: I now realize you’re using
awsx
and not
aws
. I actually don’t know if it’s possible to hydrate an
awsx.Vpc
component using an id, since it’s actually a collection of individual resources like Vpc and Subnet amongst others. To use the exact same
awsx.Vpc
object that you have in one project you would probably need to create an npm package of your object and import (as a node module) into your other project like any other javascript code.
c
You would have to export the vpc and the subnets like here. Then you could get the values via stackreference.
💡 1