Skip to content

Commit 836c5cf

Browse files
authored
feat(ec2-alpha): implement mapPublicIpOnLaunch prop in SubnetV2 (#34057)
### Issue # (if applicable) Closes #32159 . ### Reason for this change Adding support for `mapPublicIpOnLaunch` prop in SubnetV2. ### Description of changes Added property mapPublicIpOnLaunch in SubnetV2Props, set to undefined by default. ### Describe any new or updated permissions being added NA ### Description of how you validated changes Added unit test and integration tests ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 74cbe27 commit 836c5cf

15 files changed

+31788
-1
lines changed

packages/@aws-cdk/aws-ec2-alpha/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ new VpcV2(this, 'Vpc', {
4343
`SubnetV2` is a re-write of the [`ec2.Subnet`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.Subnet.html) construct.
4444
This new construct can be used to add subnets to a `VpcV2` instance:
4545
Note: When defining a subnet with `SubnetV2`, CDK automatically creates a new route table, unless a route table is explicitly provided as an input to the construct.
46+
To enable the `mapPublicIpOnLaunch` feature (which is `false` by default), set the property to `true` when creating the subnet.
4647

4748
```ts
4849
const stack = new Stack();
@@ -57,7 +58,8 @@ new SubnetV2(this, 'subnetA', {
5758
availabilityZone: 'us-east-1a',
5859
ipv4CidrBlock: new IpCidr('10.0.0.0/24'),
5960
ipv6CidrBlock: new IpCidr('2a05:d02c:25:4000::/60'),
60-
subnetType: SubnetType.PRIVATE_ISOLATED,
61+
subnetType: SubnetType.PUBLIC,
62+
mapPublicIpOnLaunch: true,
6163
})
6264
```
6365

packages/@aws-cdk/aws-ec2-alpha/lib/subnet-v2.ts

+13
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ export interface SubnetV2Props {
9696
*/
9797
readonly assignIpv6AddressOnCreation?: boolean;
9898

99+
/**
100+
* Controls if instances launched into the subnet should be assigned a public IP address.
101+
* This property can only be set for public subnets.
102+
*
103+
* @default - undefined in case not provided as an input
104+
*/
105+
readonly mapPublicIpOnLaunch?: boolean;
106+
99107
}
100108

101109
/**
@@ -276,12 +284,17 @@ export class SubnetV2 extends Resource implements ISubnetV2 {
276284
throw new Error('IPv6 CIDR block is required when assigning IPv6 address on creation');
277285
}
278286

287+
if (props.mapPublicIpOnLaunch === true && props.subnetType !== SubnetType.PUBLIC) {
288+
throw new Error('mapPublicIpOnLaunch can only be set to true for public subnets');
289+
}
290+
279291
const subnet = new CfnSubnet(this, 'Subnet', {
280292
vpcId: props.vpc.vpcId,
281293
cidrBlock: ipv4CidrBlock,
282294
ipv6CidrBlock: ipv6CidrBlock,
283295
availabilityZone: props.availabilityZone,
284296
assignIpv6AddressOnCreation: props.assignIpv6AddressOnCreation ?? false,
297+
mapPublicIpOnLaunch: props.mapPublicIpOnLaunch ?? undefined,
285298
});
286299

287300
this.node.defaultChild = subnet;

packages/@aws-cdk/aws-ec2-alpha/test/integ.subnet-map-public-ip.js.snapshot/SubnetMapPublicIpIntegDefaultTestDeployAssert628B9886.assets.json

+33
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)