From 9d93831d4e996cb77fce6367051da95b476de800 Mon Sep 17 00:00:00 2001 From: shikha372 Date: Wed, 16 Oct 2024 10:50:43 -0700 Subject: [PATCH] updating README --- packages/@aws-cdk/aws-ec2-alpha/README.md | 92 +++++++++++++++++++ packages/@aws-cdk/aws-ec2-alpha/awslint.json | 2 - .../@aws-cdk/aws-ec2-alpha/lib/subnet-v2.ts | 2 +- packages/@aws-cdk/aws-ec2-alpha/lib/vpc-v2.ts | 4 +- .../aws-ec2-alpha/test/integ.test-import.ts | 6 +- .../aws-ec2-alpha/test/vpcv2-import.test.ts | 21 ++--- 6 files changed, 108 insertions(+), 19 deletions(-) diff --git a/packages/@aws-cdk/aws-ec2-alpha/README.md b/packages/@aws-cdk/aws-ec2-alpha/README.md index 32852b802bb05..515f739ce1fd2 100644 --- a/packages/@aws-cdk/aws-ec2-alpha/README.md +++ b/packages/@aws-cdk/aws-ec2-alpha/README.md @@ -366,3 +366,95 @@ myVpc.addInternetGateway({ ipv4Destination: '192.168.0.0/16', }); ``` + +## Importing an existing VPC + +You can import an existing VPC and its subnets using the `VpcV2.fromVpcV2Attributes()` and `SubnetV2.fromSubnetV2Attributes()` methods. This is useful when you want to use existing resources in your CDK stack. + +### Importing a VPC + +To import an existing VPC, use the `VpcV2.fromVpcV2Attributes()` method. You'll need to provide the VPC ID, CIDR block, and information about the subnets. This method allows you to integrate existing AWS resources into your CDK stack. + +Here's a comprehensive example of how to import a VPC with multiple CIDR blocks, IPv6 support, and different subnet types: + +In this example, we're importing a VPC with: + + - A primary CIDR block (10.1.0.0/16) + - Two secondary IPv4 CIDR blocks (10.2.0.0/16 and 10.3.0.0/16) + - An Amazon-provided IPv6 CIDR block + - An isolated subnet in us-west-2a + - A public subnet in us-west-2b + +```ts + +const stack = new Stack(); + +const importedVpc = VpcV2.fromVpcV2Attributes(this, 'ImportedVPC', { + vpcId: 'vpc-08193db3ccc4f909f', + vpcCidrBlock: '10.1.0.0/16', + secondaryCidrBlocks: [ + { + cidrBlock: '10.2.0.0/16', + cidrBlockName: 'ImportedBlock1', + }, + { + cidrBlock: '10.3.0.0/16', + cidrBlockName: 'ImportedBlock2', + }, + { + amazonProvidedIpv6CidrBlock: true, + } + ], + isolatedSubnets: [{ + subnetName: 'IsolatedSubnet2', + subnetId: 'subnet-03cd773c0fe08ed26', + subnetType: SubnetType.PRIVATE_ISOLATED, + availabilityZone: 'us-west-2a', + ipv4CidrBlock: '10.2.0.0/24', + routeTableId: 'rtb-0871c310f98da2cbb', + }], + publicSubnets: [{ + subnetId: 'subnet-0fa477e01db27d820', + subnetType: SubnetType.PUBLIC, + availabilityZone: 'us-west-2b', + ipv4CidrBlock: '10.3.0.0/24', + routeTableId: 'rtb-014f3043098fe4b96', + }], +}); + +// You can now use the imported VPC in your stack + +// Adding a new subnet to the imported VPC +const importedSubnet = new SubnetV2(this, 'NewSubnet', { + availabilityZone: 'us-west-2a', + ipv4CidrBlock: new IpCidr('10.2.2.0/24'), + vpc: importedVpc, + subnetType: SubnetType.PUBLIC, +}); + +// Adding gateways to the imported VPC +importedVpc.addInternetGateway(); +importedVpc.addNatGateway({ subnet: importedSubnet }); +importedVpc.addEgressOnlyInternetGateway(); +``` + +You can add more subnets as needed by including additional entries in the `isolatedSubnets`, `publicSubnets`, or other subnet type arrays (e.g., `privateSubnets`). + +### Importing Subnets + +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. + +Here's an example of how to import a subnet: + +```ts + +SubnetV2.fromSubnetV2Attributes(this, 'ImportedSubnet', { + subnetId: 'subnet-0123456789abcdef0', + availabilityZone: 'us-west-2a', + ipv4CidrBlock: '10.2.0.0/24', + routeTableId: 'rtb-0871c310f98da2cbb', + subnetType: SubnetType.PRIVATE_ISOLATED, +}); +``` + +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. diff --git a/packages/@aws-cdk/aws-ec2-alpha/awslint.json b/packages/@aws-cdk/aws-ec2-alpha/awslint.json index 6ea89091a597a..ec8b3e125eeea 100644 --- a/packages/@aws-cdk/aws-ec2-alpha/awslint.json +++ b/packages/@aws-cdk/aws-ec2-alpha/awslint.json @@ -1,8 +1,6 @@ { "exclude": [ - "from-method:@aws-cdk/aws-ec2-alpha.VpcV2", "attribute-tag:@aws-cdk/aws-ec2-alpha.RouteTable.routeTableId", - "from-method:@aws-cdk/aws-ec2-alpha.SubnetV2", "from-method:@aws-cdk/aws-ec2-alpha.Route" ] } \ No newline at end of file diff --git a/packages/@aws-cdk/aws-ec2-alpha/lib/subnet-v2.ts b/packages/@aws-cdk/aws-ec2-alpha/lib/subnet-v2.ts index 38704031b2418..6aa404a89e61c 100644 --- a/packages/@aws-cdk/aws-ec2-alpha/lib/subnet-v2.ts +++ b/packages/@aws-cdk/aws-ec2-alpha/lib/subnet-v2.ts @@ -123,7 +123,7 @@ export class SubnetV2 extends Resource implements ISubnetV2 { /** * Import an existing subnet to the VPC */ - public static fromSubnetV2attributes(scope: Construct, id: string, attrs: SubnetV2Attributes) : ISubnetV2 { + public static fromSubnetV2Attributes(scope: Construct, id: string, attrs: SubnetV2Attributes) : ISubnetV2 { return new ImportedSubnetV2(scope, id, attrs); } diff --git a/packages/@aws-cdk/aws-ec2-alpha/lib/vpc-v2.ts b/packages/@aws-cdk/aws-ec2-alpha/lib/vpc-v2.ts index f43e316572453..2b0bfffc5c4ba 100644 --- a/packages/@aws-cdk/aws-ec2-alpha/lib/vpc-v2.ts +++ b/packages/@aws-cdk/aws-ec2-alpha/lib/vpc-v2.ts @@ -257,8 +257,8 @@ export class VpcV2 extends VpcV2Base { /** * Create a VPC from existing attributes */ - public static fromVpcV2attributes(scope: Construct, id: string, options: VpcV2Attributes): IVpcV2 { - return new ImportedVpcV2(scope, id, options); + public static fromVpcV2Attributes(scope: Construct, id: string, attrs: VpcV2Attributes): IVpcV2 { + return new ImportedVpcV2(scope, id, attrs); } /** diff --git a/packages/@aws-cdk/aws-ec2-alpha/test/integ.test-import.ts b/packages/@aws-cdk/aws-ec2-alpha/test/integ.test-import.ts index 1631cce1140ba..9b6e250397c08 100644 --- a/packages/@aws-cdk/aws-ec2-alpha/test/integ.test-import.ts +++ b/packages/@aws-cdk/aws-ec2-alpha/test/integ.test-import.ts @@ -19,7 +19,7 @@ const stack = new cdk.Stack(app, 'vpcv2-import-integ-test', { * Once created, change the subnet and VPCID * according to the one alloted on creation */ -const imported_new_vpc = VpcV2.VpcV2.fromVpcV2attributes(stack, 'ImportedNewVPC', { +const imported_new_vpc = VpcV2.VpcV2.fromVpcV2Attributes(stack, 'ImportedNewVPC', { vpcId: 'vpc-08193db3ccc4f909f', //VPC id vpcCidrBlock: '10.1.0.0/16', secondaryCidrBlocks: [{ @@ -69,7 +69,7 @@ new SubnetV2(stack, 'AddnewImportedSubnet2', { subnetType: SubnetType.PUBLIC, }); -const ImportedSubnet = SubnetV2.fromSubnetV2attributes(stack, 'IsolatedSubnet1', { +const ImportedSubnet = SubnetV2.fromSubnetV2Attributes(stack, 'IsolatedSubnet1', { subnetId: 'subnet-0d441651f6653d4a7', subnetType: SubnetType.PRIVATE_ISOLATED, availabilityZone: 'us-west-2b', @@ -87,7 +87,7 @@ imported_new_vpc.addNatGateway({ imported_new_vpc.addEgressOnlyInternetGateway(); // Import another IPAM enabled VPC -const ipamvpc = VpcV2.VpcV2.fromVpcV2attributes(stack, 'ImportedIPAMVPC', { +const ipamvpc = VpcV2.VpcV2.fromVpcV2Attributes(stack, 'ImportedIPAMVPC', { vpcId: 'vpc-02407f4a207815a97', vpcCidrBlock: '10.0.0.0/16', secondaryCidrBlocks: [{ diff --git a/packages/@aws-cdk/aws-ec2-alpha/test/vpcv2-import.test.ts b/packages/@aws-cdk/aws-ec2-alpha/test/vpcv2-import.test.ts index 1e130d2c514ce..c90f846ce7458 100644 --- a/packages/@aws-cdk/aws-ec2-alpha/test/vpcv2-import.test.ts +++ b/packages/@aws-cdk/aws-ec2-alpha/test/vpcv2-import.test.ts @@ -16,16 +16,16 @@ describe('Vpc V2 with full control', () => { stack = new cdk.Stack(app); }); - test('VpcV2.fromVpcV2attributes creates correct vpcArn', () => { - const importedVpc = VpcV2.fromVpcV2attributes(stack, 'VpcWithArn', { + test('VpcV2.fromVpcV2Attributes creates correct vpcArn', () => { + const importedVpc = VpcV2.fromVpcV2Attributes(stack, 'VpcWithArn', { vpcId: 'vpc-12345', vpcCidrBlock: '10.0.0.0/16', }); expect(importedVpc.vpcArn).toBe(`arn:${cdk.Stack.of(stack).partition}:ec2:${cdk.Stack.of(stack).region}:${cdk.Stack.of(stack).account}:vpc/vpc-12345`); }); - test('VpcV2.fromVpcV2attributes returns an instance of IVpcV2', () => { - const importedVpc = VpcV2.fromVpcV2attributes(stack, 'VpcInstance', { + test('VpcV2.fromVpcV2Attributes returns an instance of IVpcV2', () => { + const importedVpc = VpcV2.fromVpcV2Attributes(stack, 'VpcInstance', { vpcId: 'vpc-12345', vpcCidrBlock: '10.0.0.0/16', }); @@ -33,7 +33,7 @@ describe('Vpc V2 with full control', () => { }); test('Import VPC successfully', () => { - const vpc = VpcV2.fromVpcV2attributes(stack, 'ImportedVpc', { + const vpc = VpcV2.fromVpcV2Attributes(stack, 'ImportedVpc', { vpcId: 'XXXXXXXXX', vpcCidrBlock: '10.1.0.0/16', publicSubnets: [{ @@ -52,7 +52,7 @@ describe('Vpc V2 with full control', () => { }); test('Import different type of subnets successfully', () => { - const importedVpc = VpcV2.fromVpcV2attributes(stack, 'ImportedVpc', { + const importedVpc = VpcV2.fromVpcV2Attributes(stack, 'ImportedVpc', { vpcId: 'vpc-12345', vpcCidrBlock: '10.0.0.0/16', secondaryCidrBlocks: [ @@ -93,7 +93,7 @@ describe('Vpc V2 with full control', () => { }); test('Import VPC with secondary address Ipv4 successfully', () => { - const vpc = VpcV2.fromVpcV2attributes(stack, 'ImportedVpc', { + const vpc = VpcV2.fromVpcV2Attributes(stack, 'ImportedVpc', { vpcId: 'mockVpcID', vpcCidrBlock: '10.0.0.0/16', secondaryCidrBlocks: [ @@ -115,7 +115,7 @@ describe('Vpc V2 with full control', () => { }); test('Import VPC with IPAM IPv4', () => { - const vpc = VpcV2.fromVpcV2attributes(stack, 'ImportedVpc', { + const vpc = VpcV2.fromVpcV2Attributes(stack, 'ImportedVpc', { vpcId: 'mockVpcID', vpcCidrBlock: '10.0.0.0/16', secondaryCidrBlocks: [{ @@ -137,7 +137,7 @@ describe('Vpc V2 with full control', () => { }); test('Import VPC with IPAM IPv6', () => { - const vpc = VpcV2.fromVpcV2attributes(stack, 'ImportedVpc', { + const vpc = VpcV2.fromVpcV2Attributes(stack, 'ImportedVpc', { vpcId: 'mockVpcID', vpcCidrBlock: '10.0.0.0/16', secondaryCidrBlocks: [{ @@ -164,7 +164,7 @@ describe('Vpc V2 with full control', () => { }); test('Import VPC with secondary address amazon provided Ipv6 successfully', () => { - const vpc = VpcV2.fromVpcV2attributes(stack, 'ImportedVpc', { + const vpc = VpcV2.fromVpcV2Attributes(stack, 'ImportedVpc', { vpcId: 'mockVpcID', vpcCidrBlock: '10.0.0.0/16', secondaryCidrBlocks: [{ @@ -186,5 +186,4 @@ describe('Vpc V2 with full control', () => { Ipv6CidrBlock: '2600:1f24:6c:4000::/64', }); }); - });