4
votes

I've started using the AWS CDK to stand up a new VPC, but I am struggling when trying to query other existing VPCs and their CIDR ranges - this is to ensure that my new VPC does not overlap with existing CIDR ranges. The return string is not something I can understand. Could you provide an example on how to query for a list of CIDR ranges in subnets?

Thanks.

1
Are you trying to query VPCs that are already installed on your account? - Elad Ben-Israel
Yes, exactly as I described. - Johnathen Liew
"The return string is not something I can understand." -> can you tell me the command you've been running and the output? Are you using the AWS CLI to get the list of VPCs? - rix0rrr

1 Answers

4
votes

If you are trying to reference an existing VPC into your CDK stack, you should use the VpcNetwork.import static method which doesn't require you to specify the CIDR blocks of the VPC.

You will need other information specified in VpcNetworkRefProps, which shouldn't be too hard to obtain from the AWS Console or the AWS CLI:

Something like:

const externalVpc = VpcNetwork.import(this, 'ExternalVpc', {
  vpcId: 'vpc-bd5656d4',
  availabilityZones: [ 'us-east1a', 'us-east-1b' ],
  publicSubnetIds: [ 'subnet-1111aaaa', 'subnet-2222bbbb' ],
  privateSubnetIds: [ 'subnet-8368fbce', 'subnet-8368abcc' ],
});

We are looking at making this easier (see #506)