Skip to content

Commit

Permalink
updating README
Browse files Browse the repository at this point in the history
  • Loading branch information
shikha372 committed Oct 16, 2024
1 parent 4c0cd44 commit 9d93831
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 19 deletions.
92 changes: 92 additions & 0 deletions packages/@aws-cdk/aws-ec2-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 0 additions & 2 deletions packages/@aws-cdk/aws-ec2-alpha/awslint.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-ec2-alpha/lib/subnet-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-ec2-alpha/lib/vpc-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/@aws-cdk/aws-ec2-alpha/test/integ.test-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [{
Expand Down Expand Up @@ -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',
Expand All @@ -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: [{
Expand Down
21 changes: 10 additions & 11 deletions packages/@aws-cdk/aws-ec2-alpha/test/vpcv2-import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ 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',
});
expect(importedVpc).toBeInstanceOf(VpcV2Base);
});

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: [{
Expand All @@ -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: [
Expand Down Expand Up @@ -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: [
Expand All @@ -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: [{
Expand All @@ -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: [{
Expand All @@ -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: [{
Expand All @@ -186,5 +186,4 @@ describe('Vpc V2 with full control', () => {
Ipv6CidrBlock: '2600:1f24:6c:4000::/64',
});
});

});

0 comments on commit 9d93831

Please sign in to comment.