Skip to content

Commit

Permalink
fix(ec2): Subnet cidr missing for Vpc.from_lookup() (#12878)
Browse files Browse the repository at this point in the history
When importing a VPC using `Vpc.fromLookup()`, the subnets are properly looked up and saved into the `cdk.context.json`. However, the Subnets object within the imported VPC don't have the CIDR attached and trying to access the subnet CIDR within the code triggers the following error:

> You cannot reference an imported Subnet's IPv4 CIDR if it was not supplied. Add the ipv4CidrBlock when importing using Subnet.fromSubnetAttributes().

This PR fixes the issue.

closes #11821

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
DaWyz authored Feb 9, 2021
1 parent 953957a commit 9028269
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-ec2/lib/vpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1886,6 +1886,7 @@ class LookedUpVpc extends VpcBase {
availabilityZone: vpcSubnet.availabilityZone,
subnetId: vpcSubnet.subnetId,
routeTableId: vpcSubnet.routeTableId,
ipv4CidrBlock: vpcSubnet.cidr,
}));
}
return ret;
Expand Down
41 changes: 41 additions & 0 deletions packages/@aws-cdk/aws-ec2/test/vpc.from-lookup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,47 @@ nodeunitShim({

test.done();
},
'subnets in imported VPC has all expected attributes'(test: Test) {
const previous = mockVpcContextProviderWith(test, {
vpcId: 'vpc-1234',
subnetGroups: [
{
name: 'Public',
type: cxapi.VpcSubnetGroupType.PUBLIC,
subnets: [
{
subnetId: 'pub-sub-in-us-east-1a',
availabilityZone: 'us-east-1a',
routeTableId: 'rt-123',
cidr: '10.100.0.0/24',
},
],
},
],
}, options => {
test.deepEqual(options.filter, {
isDefault: 'true',
});

test.equal(options.subnetGroupNameTag, undefined);
});

const stack = new Stack();
const vpc = Vpc.fromLookup(stack, 'Vpc', {
isDefault: true,
});

let subnet = vpc.publicSubnets[0];

test.equal(subnet.availabilityZone, 'us-east-1a');
test.equal(subnet.subnetId, 'pub-sub-in-us-east-1a');
test.equal(subnet.routeTable.routeTableId, 'rt-123');
test.equal(subnet.ipv4CidrBlock, '10.100.0.0/24');


restoreContextProvider(previous);
test.done();
},
},
});

Expand Down

0 comments on commit 9028269

Please sign in to comment.