Skip to content

Commit 13e1c7f

Browse files
authored
fix(vpc): Vpc.fromLookup should throw if subnet group name tag is explicitly given and does not exist (#18714)
Currently if `subnetGroupNameTag` is provided in `Vpc.fromLookup()` and a tag with that key does not exist, the error that is returned is very generic and just indicates that the VPC could not be found. This makes it very hard to troubleshoot what the real issue is (invalid subnetGroupNameTag). Now if the user provides a `subnetGroupNameTag` and a tag with that Key does not exist an error is thrown indicating that an invalid `subnetGroupNameTag` was provided fixes #13962 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 164d50b commit 13e1c7f

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

Diff for: packages/aws-cdk/lib/context-providers/vpcs.ts

+4
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ export class VpcNetworkContextProviderPlugin implements ContextProviderPlugin {
8080
throw new Error(`Subnet ${subnet.SubnetArn} has invalid subnet type ${type} (must be ${SubnetType.Public}, ${SubnetType.Private} or ${SubnetType.Isolated})`);
8181
}
8282

83+
if (args.subnetGroupNameTag && !getTag(args.subnetGroupNameTag, subnet.Tags)) {
84+
throw new Error(`Invalid subnetGroupNameTag: Subnet ${subnet.SubnetArn} does not have an associated tag with Key='${args.subnetGroupNameTag}'`);
85+
}
86+
8387
const name = getTag(args.subnetGroupNameTag || 'aws-cdk:subnet-name', subnet.Tags) || type;
8488
const routeTableId = routeTables.routeTableIdForSubnetId(subnet.SubnetId);
8589

Diff for: packages/aws-cdk/test/context-providers/vpcs.test.ts

+138
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,144 @@ test('throws when no such VPC is found', async () => {
110110
})).rejects.toThrow(/Could not find any VPCs matching/);
111111
});
112112

113+
test('throws when subnet with subnetGroupNameTag not found', async () => {
114+
// GIVEN
115+
const filter = { foo: 'bar' };
116+
const provider = new VpcNetworkContextProviderPlugin(mockSDK);
117+
118+
mockVpcLookup({
119+
subnets: [
120+
{ SubnetId: 'sub-123456', AvailabilityZone: 'bermuda-triangle-1337', MapPublicIpOnLaunch: true },
121+
{ SubnetId: 'sub-789012', AvailabilityZone: 'bermuda-triangle-1337', MapPublicIpOnLaunch: false },
122+
],
123+
routeTables: [
124+
{
125+
Associations: [{ SubnetId: 'sub-123456' }],
126+
RouteTableId: 'rtb-123456',
127+
Routes: [
128+
{
129+
DestinationCidrBlock: '1.1.1.1/24',
130+
GatewayId: 'local',
131+
Origin: 'CreateRouteTable',
132+
State: 'active',
133+
},
134+
{
135+
DestinationCidrBlock: '0.0.0.0/0',
136+
GatewayId: 'igw-xxxxxx',
137+
Origin: 'CreateRoute',
138+
State: 'active',
139+
},
140+
],
141+
},
142+
{
143+
Associations: [{ SubnetId: 'sub-789012' }],
144+
RouteTableId: 'rtb-789012',
145+
Routes: [
146+
{
147+
DestinationCidrBlock: '1.1.2.1/24',
148+
GatewayId: 'local',
149+
Origin: 'CreateRouteTable',
150+
State: 'active',
151+
},
152+
{
153+
DestinationCidrBlock: '0.0.0.0/0',
154+
NatGatewayId: 'nat-xxxxxx',
155+
Origin: 'CreateRoute',
156+
State: 'active',
157+
},
158+
],
159+
},
160+
],
161+
vpnGateways: [{ VpnGatewayId: 'gw-abcdef' }],
162+
});
163+
164+
// WHEN
165+
await expect(provider.getValue({
166+
account: '1234',
167+
region: 'us-east-1',
168+
subnetGroupNameTag: 'DOES_NOT_EXIST',
169+
filter,
170+
})).rejects.toThrow(/Invalid subnetGroupNameTag: Subnet .* does not have an associated tag with Key='DOES_NOT_EXIST'/);
171+
});
172+
173+
test('does not throw when subnet with subnetGroupNameTag is found', async () => {
174+
// GIVEN
175+
const filter = { foo: 'bar' };
176+
const provider = new VpcNetworkContextProviderPlugin(mockSDK);
177+
178+
mockVpcLookup({
179+
subnets: [
180+
{ SubnetId: 'sub-123456', AvailabilityZone: 'bermuda-triangle-1337', MapPublicIpOnLaunch: true, Tags: [{ Key: 'DOES_EXIST', Value: 'SubnetName1' }] },
181+
{ SubnetId: 'sub-789012', AvailabilityZone: 'bermuda-triangle-1337', MapPublicIpOnLaunch: false, Tags: [{ Key: 'DOES_EXIST', Value: 'SubnetName2' }] },
182+
],
183+
routeTables: [
184+
{
185+
Associations: [{ SubnetId: 'sub-123456' }],
186+
RouteTableId: 'rtb-123456',
187+
Routes: [
188+
{
189+
DestinationCidrBlock: '1.1.1.1/24',
190+
GatewayId: 'local',
191+
Origin: 'CreateRouteTable',
192+
State: 'active',
193+
},
194+
{
195+
DestinationCidrBlock: '0.0.0.0/0',
196+
GatewayId: 'igw-xxxxxx',
197+
Origin: 'CreateRoute',
198+
State: 'active',
199+
},
200+
],
201+
},
202+
{
203+
Associations: [{ SubnetId: 'sub-789012' }],
204+
RouteTableId: 'rtb-789012',
205+
Routes: [
206+
{
207+
DestinationCidrBlock: '1.1.2.1/24',
208+
GatewayId: 'local',
209+
Origin: 'CreateRouteTable',
210+
State: 'active',
211+
},
212+
{
213+
DestinationCidrBlock: '0.0.0.0/0',
214+
NatGatewayId: 'nat-xxxxxx',
215+
Origin: 'CreateRoute',
216+
State: 'active',
217+
},
218+
],
219+
},
220+
],
221+
vpnGateways: [{ VpnGatewayId: 'gw-abcdef' }],
222+
});
223+
224+
// WHEN
225+
const result = await provider.getValue({
226+
account: '1234',
227+
region: 'us-east-1',
228+
subnetGroupNameTag: 'DOES_EXIST',
229+
filter,
230+
});
231+
232+
// THEN
233+
expect(result).toEqual({
234+
vpcId: 'vpc-1234567',
235+
vpcCidrBlock: '1.1.1.1/16',
236+
availabilityZones: ['bermuda-triangle-1337'],
237+
isolatedSubnetIds: undefined,
238+
isolatedSubnetNames: undefined,
239+
isolatedSubnetRouteTableIds: undefined,
240+
privateSubnetIds: ['sub-789012'],
241+
privateSubnetNames: ['SubnetName2'],
242+
privateSubnetRouteTableIds: ['rtb-789012'],
243+
publicSubnetIds: ['sub-123456'],
244+
publicSubnetNames: ['SubnetName1'],
245+
publicSubnetRouteTableIds: ['rtb-123456'],
246+
vpnGatewayId: 'gw-abcdef',
247+
subnetGroups: undefined,
248+
});
249+
});
250+
113251
test('throws when multiple VPCs are found', async () => {
114252
// GIVEN
115253
const filter = { foo: 'bar' };

0 commit comments

Comments
 (0)