You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(ec2-alpha): adding imports for SubnetV2 and VpcV2(WIP) (#31765)
### Issue # (if applicable)
Tracking #30762.
### Reason for this change
Allow users to define imports for a VPC or subnet defined outside current stack definition.
### Description of changes
- Added new methods under VpcV2 and Subnet
`VpcV2.fromVpcV2Attributes()` and `SubnetV2.fromSubnetV2Attributes()`
- Added new L2 for VPCCidrBlock to allow import of secondary addresses.
`VPCCidrBlock`
- Added new integration test and unit test file to check import related functionality.
- Updated Readme.
- Fixed an earlier issue with subnet range check, fixed to include IPAM defined IPv4 address as well
### Description of how you validated changes
Deployed and tested for below scenarios in account:
1. Import a VPC with primary IPv4
2. Import a subnet with primary IPv4
3. Import a VPC with multiple secondary IPv4
4. Import a VPC with Amazon provided IPV6
5. Import a VPC with Ipam provided IPv6/IPv4
7. Import subnet individually using fromSubnetV2attributes
8. Imported different type of multiple subnets
9. Add gateways/endpoint to imported vpc
### 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)
BREAKING CHANGE: The new `VpcCidrBlock` L2 construct replaces `CfnVPCCidrBlock`. This change alters the logical ID of `AWS::EC2::VPCCidrBlock` resources in CloudFormation templates. Existing deployments will see errors like `CIDR range conflicts with x.xx.xx.xx/xx with association ID vpc-cidr-assoc-ABCD`. To resolve this, you must recreate your existing stacks to use the new module.
----
*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Copy file name to clipboardExpand all lines: packages/@aws-cdk/aws-ec2-alpha/README.md
+132-4
Original file line number
Diff line number
Diff line change
@@ -37,8 +37,6 @@ new VpcV2(this, 'Vpc', {
37
37
38
38
`VpcV2` does not automatically create subnets or allocate IP addresses, which is different from the `Vpc` construct.
39
39
40
-
Importing existing VPC in an account into CDK as a `VpcV2` is not yet supported.
41
-
42
40
## SubnetV2
43
41
44
42
`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.
@@ -62,8 +60,6 @@ new SubnetV2(this, 'subnetA', {
62
60
})
63
61
```
64
62
65
-
Same as `VpcV2`, importing existing subnets is not yet supported.
66
-
67
63
## IP Addresses Management
68
64
69
65
By default `VpcV2` uses `10.0.0.0/16` as the primary CIDR if none is defined.
@@ -366,3 +362,135 @@ myVpc.addInternetGateway({
366
362
ipv4Destination: '192.168.0.0/16',
367
363
});
368
364
```
365
+
366
+
## Importing an existing VPC
367
+
368
+
You can import an existing VPC and its subnets using the `VpcV2.fromVpcV2Attributes()` method or an individual subnet using `SubnetV2.fromSubnetV2Attributes()` method.
369
+
370
+
### Importing a VPC
371
+
372
+
To import an existing VPC, use the `VpcV2.fromVpcV2Attributes()` method. You'll need to provide the VPC ID, primary CIDR block, and information about the subnets. You can import secondary address as well created through IPAM, BYOIP(IPv4) or enabled through Amazon Provided IPv6. You must provide VPC Id and its primary CIDR block for importing it.
373
+
374
+
If you wish to add a new subnet to imported VPC, new subnet's IP range(IPv4) will be validated against provided secondary and primary address block to confirm that it is within the the range of VPC.
375
+
376
+
Here's an example of importing a VPC with only the required parameters
In case of cross account or cross region VPC, its recommended to provide region and ownerAccountId so that these values for the VPC can be used to populate correct arn value for the VPC. If a VPC region and account ID is not provided, then region and account configured in the stack will be used. Furthermore, these fields will be referenced later while setting up VPC peering connection, so its necessary to set these fields to a correct value.
390
+
391
+
Below is an example of importing a cross region and cross acount VPC, VPC arn for this case would be 'arn:aws:ec2:us-west-2:123456789012:vpc/mockVpcID'
You can add more subnets as needed by including additional entries in the `isolatedSubnets`, `publicSubnets`, or other subnet type arrays (e.g., `privateSubnets`).
478
+
479
+
### Importing Subnets
480
+
481
+
You can also import individual subnets using the `SubnetV2.fromSubnetV2Attributes()` method. This is useful when you need to work with specific subnets independently of a VPC.
By importing existing VPCs and subnets, you can easily integrate your existing AWS infrastructure with new resources created through CDK. This is particularly useful when you need to work with pre-existing network configurations or when you're migrating existing infrastructure to CDK.
0 commit comments