Skip to content

Commit a1685c6

Browse files
authoredNov 29, 2021
feat(ec2): explicit mapPublicIpOnLaunch configuration for public subnets (#17346)
**Issue (Fixes #14194, #16838 When creating a VPC you can define a SubnetConfiguration but it is not possible to define `mapPublicIpOnLaunch` for public subnets. VPC Example: ``` const vpc = new ec2.Vpc(this, 'vpc-id', { maxAzs: 2, subnetConfiguration: [ { name: 'private-subnet-1', subnetType: ec2.SubnetType.PRIVATE, cidrMask: 24, }, { name: 'public-subnet-1', subnetType: ec2.SubnetType.PUBLIC, cidrMask: 24, }, ] }); ``` Proposal: ``` const vpc = new ec2.Vpc(this, 'vpc-id', { maxAzs: 2, subnetConfiguration: [ { name: 'private-subnet-1', subnetType: ec2.SubnetType.PRIVATE, cidrMask: 24, }, { name: 'public-subnet-1', subnetType: ec2.SubnetType.PUBLIC, cidrMask: 24, mapPublicIpOnLaunch: false, // or true }, ] }); ```
1 parent 168a98f commit a1685c6

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed
 

‎packages/@aws-cdk/aws-ec2/lib/vpc.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,13 @@ export interface SubnetConfiguration {
10131013
* @default false
10141014
*/
10151015
readonly reserved?: boolean;
1016+
1017+
/**
1018+
* Controls if a public IP is associated to an instance at launch
1019+
*
1020+
* @default true in Subnet.Public, false in Subnet.Private or Subnet.Isolated.
1021+
*/
1022+
readonly mapPublicIpOnLaunch?: boolean;
10161023
}
10171024

10181025
/**
@@ -1452,12 +1459,23 @@ export class Vpc extends VpcBase {
14521459
return;
14531460
}
14541461

1462+
// mapPublicIpOnLaunch true in Subnet.Public, false in Subnet.Private or Subnet.Isolated.
1463+
let mapPublicIpOnLaunch = false;
1464+
if (subnetConfig.subnetType !== SubnetType.PUBLIC && subnetConfig.mapPublicIpOnLaunch !== undefined) {
1465+
throw new Error(`${subnetConfig.subnetType} subnet cannot include mapPublicIpOnLaunch parameter`);
1466+
}
1467+
if (subnetConfig.subnetType === SubnetType.PUBLIC) {
1468+
mapPublicIpOnLaunch = (subnetConfig.mapPublicIpOnLaunch !== undefined)
1469+
? subnetConfig.mapPublicIpOnLaunch
1470+
: true;
1471+
}
1472+
14551473
const name = subnetId(subnetConfig.name, index);
14561474
const subnetProps: SubnetProps = {
14571475
availabilityZone: zone,
14581476
vpcId: this.vpcId,
14591477
cidrBlock: this.networkBuilder.addSubnet(cidrMask),
1460-
mapPublicIpOnLaunch: (subnetConfig.subnetType === SubnetType.PUBLIC),
1478+
mapPublicIpOnLaunch: mapPublicIpOnLaunch,
14611479
};
14621480

14631481
let subnet: Subnet;

‎packages/@aws-cdk/aws-ec2/test/vpc.test.ts

+77-1
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,82 @@ describe('vpc', () => {
434434

435435
});
436436

437+
test('with public subnets MapPublicIpOnLaunch is true if parameter mapPublicIpOnLaunch is true', () => {
438+
const stack = getTestStack();
439+
new Vpc(stack, 'VPC', {
440+
maxAzs: 1,
441+
subnetConfiguration: [
442+
{
443+
cidrMask: 24,
444+
name: 'ingress',
445+
subnetType: SubnetType.PUBLIC,
446+
mapPublicIpOnLaunch: true,
447+
},
448+
],
449+
});
450+
expect(stack).toCountResources('AWS::EC2::Subnet', 1);
451+
expect(stack).not.toHaveResource('AWS::EC2::NatGateway');
452+
expect(stack).toHaveResource('AWS::EC2::Subnet', {
453+
MapPublicIpOnLaunch: true,
454+
});
455+
});
456+
test('with public subnets MapPublicIpOnLaunch is false if parameter mapPublicIpOnLaunch is false', () => {
457+
const stack = getTestStack();
458+
new Vpc(stack, 'VPC', {
459+
maxAzs: 1,
460+
subnetConfiguration: [
461+
{
462+
cidrMask: 24,
463+
name: 'ingress',
464+
subnetType: SubnetType.PUBLIC,
465+
mapPublicIpOnLaunch: false,
466+
},
467+
],
468+
});
469+
expect(stack).toCountResources('AWS::EC2::Subnet', 1);
470+
expect(stack).not.toHaveResource('AWS::EC2::NatGateway');
471+
expect(stack).toHaveResource('AWS::EC2::Subnet', {
472+
MapPublicIpOnLaunch: false,
473+
});
474+
});
475+
test('with private subnets throw exception if parameter mapPublicIpOnLaunch is defined', () => {
476+
const stack = getTestStack();
477+
expect(() => {
478+
new Vpc(stack, 'VPC', {
479+
maxAzs: 1,
480+
subnetConfiguration: [
481+
{
482+
name: 'public',
483+
subnetType: SubnetType.PUBLIC,
484+
},
485+
{
486+
name: 'private',
487+
subnetType: SubnetType.PRIVATE_WITH_NAT,
488+
mapPublicIpOnLaunch: true,
489+
},
490+
],
491+
});
492+
}).toThrow(/subnet cannot include mapPublicIpOnLaunch parameter/);
493+
});
494+
test('with isolated subnets throw exception if parameter mapPublicIpOnLaunch is defined', () => {
495+
const stack = getTestStack();
496+
expect(() => {
497+
new Vpc(stack, 'VPC', {
498+
maxAzs: 1,
499+
subnetConfiguration: [
500+
{
501+
name: 'public',
502+
subnetType: SubnetType.PUBLIC,
503+
},
504+
{
505+
name: 'private',
506+
subnetType: SubnetType.PRIVATE_ISOLATED,
507+
mapPublicIpOnLaunch: true,
508+
},
509+
],
510+
});
511+
}).toThrow(/subnet cannot include mapPublicIpOnLaunch parameter/);
512+
});
437513
test('maxAZs defaults to 3 if unset', () => {
438514
const stack = getTestStack();
439515
new Vpc(stack, 'VPC');
@@ -1817,4 +1893,4 @@ function hasTags(expectedTags: Array<{Key: string, Value: string}>): (props: any
18171893
throw e;
18181894
}
18191895
};
1820-
}
1896+
}

0 commit comments

Comments
 (0)
Please sign in to comment.