From 35857d99fa2d9bd39c510551f7060cb127c8e9fa Mon Sep 17 00:00:00 2001 From: shikha372 Date: Fri, 18 Oct 2024 10:24:43 -0700 Subject: [PATCH] adding arn field to populate account and region --- .../@aws-cdk/aws-ec2-alpha/lib/vpc-v2-base.ts | 14 ++++++ packages/@aws-cdk/aws-ec2-alpha/lib/vpc-v2.ts | 45 +++++++++++++------ .../aws-ec2-alpha/test/vpcv2-import.test.ts | 12 +++++ 3 files changed, 58 insertions(+), 13 deletions(-) diff --git a/packages/@aws-cdk/aws-ec2-alpha/lib/vpc-v2-base.ts b/packages/@aws-cdk/aws-ec2-alpha/lib/vpc-v2-base.ts index 2b0757f29f3f4..04938e6fec674 100644 --- a/packages/@aws-cdk/aws-ec2-alpha/lib/vpc-v2-base.ts +++ b/packages/@aws-cdk/aws-ec2-alpha/lib/vpc-v2-base.ts @@ -97,6 +97,20 @@ export interface IVpcV2 extends IVpc { */ readonly ipv4CidrBlock: string; + /** + * Optional to override inferred region + * + * @default - current stack's environment region + */ + readonly region?: string; + + /** + * The ID of the AWS account that owns the VPC + * + * @default - the account id of the parent stack + */ + readonly ownerAccountId?: string; + /** * IPv4 CIDR provisioned under pool * Required to check for overlapping CIDRs after provisioning 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 c1b0c3218b095..a5fee506ca6b9 100644 --- a/packages/@aws-cdk/aws-ec2-alpha/lib/vpc-v2.ts +++ b/packages/@aws-cdk/aws-ec2-alpha/lib/vpc-v2.ts @@ -1,9 +1,9 @@ import { CfnVPC, CfnVPCCidrBlock, DefaultInstanceTenancy, ISubnet } from 'aws-cdk-lib/aws-ec2'; -import { Arn, CfnResource, Lazy, Names, Resource, Stack } from 'aws-cdk-lib/core'; +import { Arn, CfnResource, Lazy, Names, Resource } from 'aws-cdk-lib/core'; import { Construct, DependencyGroup, IDependable } from 'constructs'; import { IpamOptions, IIpamPool } from './ipam'; import { IVpcV2, VpcV2Base } from './vpc-v2-base'; -import { ISubnetV2, ImportedSubnetV2, SubnetV2Attributes } from './subnet-v2';; +import { ISubnetV2, ImportedSubnetV2, SubnetV2Attributes } from './subnet-v2'; /** * Additional props needed for secondary Address @@ -196,18 +196,20 @@ export interface VpcV2Props { */ export interface VpcV2Attributes { - /** - * The region in which the VPC is located - * @default - No region information - */ - readonly region?: string; - /** * The VPC ID * Refers to physical Id of the resource */ readonly vpcId: string; + /** + * Arn of the VPC + * will be used to set value for account and region + * which then later can be used for establishing VPC peering connection + * @default - constructed with stack account and region value + */ + readonly vpcArn?: string; + /** * Primary VPC CIDR Block of the imported VPC * Can only be IPv4 @@ -334,6 +336,16 @@ export class VpcV2 extends VpcV2Base { */ public readonly ipv4IpamProvisionedCidrs?: string[]; + /** + * Region for this VPC + */ + public readonly region?: string; + + /** + * Identifier of the owner for this VPC + */ + public readonly ownerAccountId?: string; + /** * For validation to define IPv6 subnets, set to true in case of * Amazon Provided IPv6 cidr range @@ -379,6 +391,8 @@ export class VpcV2 extends VpcV2Base { resource: 'vpc', resourceName: this.vpcId, }, this.stack); + this.region = this.stack.region; + this.ownerAccountId = this.stack.account; if (props.secondaryAddressBlocks) { const secondaryAddressBlocks: IIpAddresses[] = props.secondaryAddressBlocks; @@ -536,6 +550,8 @@ class ImportedVpcV2 extends VpcV2Base { public readonly isolatedSubnets: ISubnetV2[] = []; public readonly internetConnectivityEstablished: IDependable = new DependencyGroup(); public readonly ipv4CidrBlock: string; + public readonly region?: string; + public readonly ownerAccountId?: string; /* * Reference to all secondary blocks attached @@ -552,15 +568,18 @@ class ImportedVpcV2 extends VpcV2Base { public readonly ipv4IpamProvisionedCidrs: string[] = []; constructor(scope: Construct, id: string, props: VpcV2Attributes) { - super(scope, id, { - region: props. region, - }); + super(scope, id); this.vpcId = props.vpcId, - this.vpcArn = Arn.format({ + this.vpcArn = props.vpcArn ?? Arn.format({ service: 'ec2', resource: 'vpc', resourceName: this.vpcId, - }, Stack.of(this)); + }, this.stack); + // Populate region and account fields that can be used to set up peering connection + // sample vpc Arn - arn:aws:ec2:us-west-2:123456789012:vpc/vpc-0123456789abcdef0 + this.region = this.vpcArn.split(':')[3]; + this.ownerAccountId = this.vpcArn.split(':')[4]; + // Refers to actual VPC Resource attribute in non-imported VPC this.vpcCidrBlock = props.vpcCidrBlock; // Required for subnet range related checks this.ipv4CidrBlock = props.vpcCidrBlock; 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 abfd0f22e2408..8f14c70e8246a 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 @@ -186,4 +186,16 @@ describe('Vpc V2 with full control', () => { Ipv6CidrBlock: '2600:1f24:6c:4000::/64', }); }); + test('Fetch correct account id and region ', () => { + const vpc = VpcV2.fromVpcV2Attributes(stack, 'ImportedVpc', { + vpcId: 'mockVpcID', + vpcCidrBlock: '10.0.0.0/16', + secondaryCidrBlocks: [{ + amazonProvidedIpv6CidrBlock: true, + }], + vpcArn: 'arn:aws:ec2:us-west-2:123456789012:vpc/mockVpcID', + }); + expect(vpc.ownerAccountId).toBe('123456789012'); + expect(vpc.region).toBe('us-west-2'); + }); });