Skip to content

Commit

Permalink
addressing comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shikha372 committed Oct 24, 2024
1 parent 13d1458 commit 6ec70f6
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 190 deletions.
34 changes: 31 additions & 3 deletions packages/@aws-cdk/aws-ec2-alpha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,34 @@ To import an existing VPC, use the `VpcV2.fromVpcV2Attributes()` method. You'll

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.

Here's an example of importing a VPC with only the required parameters

``` ts

const stack = new Stack();

const importedVpc = VpcV2.fromVpcV2Attributes(stack, 'ImportedVpc', {
vpcId: 'mockVpcID',
vpcCidrBlock: '10.0.0.0/16',
});

```

In case of cross account or cross region VPC, its recommended to provide VPC arn so that the region and accountId values for the VPC can be fetched from given arn value. If a VPC arn is not provided, arn will be populated using region and account configured in the stack.

``` ts

const stack = new Stack();

//Importing a cross acount or cross region VPC
const importedVpc = VpcV2.fromVpcV2Attributes(stack, 'ImportedVpc', {
vpcId: 'mockVpcID',
vpcCidrBlock: '10.0.0.0/16',
vpcArn: 'arn:aws:ec2:us-west-2:123456789012:vpc/vpc-0123abcd4567efgh8',
});

```

Here's an 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:
Expand Down Expand Up @@ -410,15 +438,15 @@ const importedVpc = VpcV2.fromVpcV2Attributes(this, 'ImportedVPC', {
amazonProvidedIpv6CidrBlock: true,
}
],
isolatedSubnets: [{
subnets: [{
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',
Expand Down
135 changes: 56 additions & 79 deletions packages/@aws-cdk/aws-ec2-alpha/lib/subnet-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,62 @@ export class SubnetV2 extends Resource implements ISubnetV2 {
* Import an existing subnet to the VPC
*/
public static fromSubnetV2Attributes(scope: Construct, id: string, attrs: SubnetV2Attributes) : ISubnetV2 {
return new ImportedSubnetV2(scope, id, attrs);
/**
* Class to define an import for an existing subnet
* @resource AWS::EC2::Subnet
*/
class ImportedSubnetV2 extends Resource implements ISubnetV2 {

/**
* The IPv6 CIDR Block assigned to this subnet
*/
public readonly ipv6CidrBlock?: string = attrs.ipv6CidrBlock;

/**
* The type of subnet (eg. public or private) that this subnet represents.
*/
public readonly subnetType?: SubnetType = attrs.subnetType;

/**
* The Availability Zone in which subnet is located
*/
public readonly availabilityZone: string = attrs.availabilityZone;

/**
* The subnetId for this particular subnet
* Refers to the physical ID created
*/
public readonly subnetId: string = attrs.subnetId;

/**
* Dependable that can be depended upon to force internet connectivity established on the VPC
*/
public readonly internetConnectivityEstablished: IDependable = new DependencyGroup();

/**
* The IPv4 CIDR block assigned to this subnet
*/
public readonly ipv4CidrBlock: string = attrs.ipv4CidrBlock;

/**
* Current route table associated with this subnet
*/
public readonly routeTable: IRouteTable = { routeTableId: attrs.routeTableId! }

/**
* Associate a Network ACL with this subnet
* Required here since it is implemented in the ISubnetV2
*/
public associateNetworkAcl(aclId: string, networkAcl: INetworkAcl) {
const aclScope = networkAcl instanceof Construct ? networkAcl : this;
const other = networkAcl instanceof Construct ? this : networkAcl;
new SubnetNetworkAclAssociation(aclScope, aclId + Names.nodeUniqueId(other.node), {
networkAcl,
subnet: this,
});
}
}
return new ImportedSubnetV2(scope, id);
}

/**
Expand Down Expand Up @@ -334,84 +389,6 @@ export interface SubnetV2Attributes {

}

/**
* Properties required to import a subnet
*/
export interface ImportedSubnetV2Props extends SubnetV2Attributes {}

/**
* Class to define an import for an existing subnet
* @resource AWS::EC2::Subnet
*/
export class ImportedSubnetV2 extends Resource implements ISubnetV2 {

/**
* The IPv6 CIDR Block assigned to this subnet
*/
public readonly ipv6CidrBlock?: string;

/**
* The type of subnet (eg. public or private) that this subnet represents.
*/
public readonly subnetType?: SubnetType;

/**
* The Availability Zone in which subnet is located
*/
public readonly availabilityZone: string;

/**
* The subnetId for this particular subnet
* Refers to the physical ID created
*/
public readonly subnetId: string;

/**
* Dependable that can be depended upon to force internet connectivity established on the VPC
*/
public readonly internetConnectivityEstablished: IDependable = new DependencyGroup();

/**
* The IPv4 CIDR block assigned to this subnet
*/
public readonly ipv4CidrBlock: string;

/**
* Current route table associated with this subnet
*/
public readonly routeTable: IRouteTable;

constructor(scope: Construct, id: string, props: ImportedSubnetV2Props) {
super(scope, id);

if (!props.routeTableId) {
throw new Error('Route Table ID is required');
}

this.ipv4CidrBlock = props.ipv4CidrBlock;
this.availabilityZone = props.availabilityZone;
this.subnetType = props.subnetType;
this.ipv6CidrBlock = props.ipv6CidrBlock;
this.subnetId = props.subnetId;
this.routeTable = {
routeTableId: props.routeTableId!,
};
}

/**
* Associate a Network ACL with this subnet
* Required here since it is implemented in the ISubnetV2
*/
public associateNetworkAcl(id: string, networkAcl: INetworkAcl) {
const scope = networkAcl instanceof Construct ? networkAcl : this;
const other = networkAcl instanceof Construct ? this : networkAcl;
new SubnetNetworkAclAssociation(scope, id + Names.nodeUniqueId(other.node), {
networkAcl,
subnet: this,
});
}
}

const subnetTypeMap = {
[SubnetType.PRIVATE_ISOLATED]: (vpc: IVpcV2, subnet: SubnetV2) => vpc.isolatedSubnets.push(subnet),
[SubnetType.PUBLIC]: (vpc: IVpcV2, subnet: SubnetV2) => vpc.publicSubnets.push(subnet),
Expand Down
14 changes: 12 additions & 2 deletions packages/@aws-cdk/aws-ec2-alpha/lib/vpc-v2-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ export interface IVpcV2 extends IVpc {
*
* @default - current stack's environment region
*/
readonly region?: string;
readonly region: string;

/**
* The ID of the AWS account that owns the VPC
*
* @default - the account id of the parent stack
*/
readonly ownerAccountId?: string;
readonly ownerAccountId: string;

/**
* IPv4 CIDR provisioned under pool
Expand Down Expand Up @@ -220,6 +220,16 @@ export abstract class VpcV2Base extends Resource implements IVpcV2 {
*/
public abstract readonly ipv4CidrBlock: string;

/**
* Region for this VPC
*/
public abstract readonly region: string;

/**
* Identifier of the owner for this VPC
*/
public abstract readonly ownerAccountId: string;

/**
* If this is set to true, don't error out on trying to select subnets
*/
Expand Down
Loading

0 comments on commit 6ec70f6

Please sign in to comment.