diff --git a/packages/aws-cdk-lib/aws-ec2/lib/ip-addresses.ts b/packages/aws-cdk-lib/aws-ec2/lib/ip-addresses.ts index 63eb714da3173..3cd4574698054 100644 --- a/packages/aws-cdk-lib/aws-ec2/lib/ip-addresses.ts +++ b/packages/aws-cdk-lib/aws-ec2/lib/ip-addresses.ts @@ -139,18 +139,20 @@ export interface AllocateCidrRequest { } /** - * Request for allocation of the VPC IPv6 CIDR. + * Internal interface to get VPC context to IpAddresses. + * + * @internal */ -export interface AllocateVpcIpv6CidrRequest { +export interface _VpcContext { /** - * The VPC construct to attach to. + * VPC construct. */ - readonly scope: Construct; + scope: Construct; /** - * The id of the VPC. + * Id of the VPC. */ - readonly vpcId: string; + vpcId: string; } /** @@ -446,18 +448,18 @@ export class Ipv6Addresses { */ export interface IIpv6Addresses { /** - * Whether the IPv6 CIDR is Amazon provided or not. + * Internal function to get scope and context from VPC. * - * Note this is specific to the IPv6 CIDR. + * @internal */ - amazonProvided: boolean, + _bind(context: _VpcContext): void; /** * Called by VPC to allocate IPv6 CIDR. * * Note this is specific to the IPv6 CIDR. */ - allocateVpcIpv6Cidr(input: AllocateVpcIpv6CidrRequest): CfnVPCCidrBlock; + allocateVpcIpv6Cidr(): CfnVPCCidrBlock; /** * Split IPv6 CIDR block up for subnets. @@ -483,11 +485,32 @@ class AmazonProvided implements IIpv6Addresses { /** * Whether the IPv6 CIDR is Amazon provided or not. */ - amazonProvided: boolean; + private readonly amazonProvided: boolean; + + /** + * Internal VPC construct. + */ + private scope?: Construct; + + /** + * Internal id of the VPC. + */ + private vpcId?: string; constructor() { this.amazonProvided = true; } + + /** + * Internal function to get scope and context from VPC. + * + * @internal + */ + _bind(context: _VpcContext): void { + this.scope = context.scope; + this.vpcId = context.vpcId; + } + /** * Called by VPC to allocate IPv6 CIDR. * @@ -495,10 +518,12 @@ class AmazonProvided implements IIpv6Addresses { * * Note this is specific to the IPv6 CIDR. */ - allocateVpcIpv6Cidr(input: AllocateVpcIpv6CidrRequest): CfnVPCCidrBlock { - //throw new Error(`vpcId not found, got ${(scope as any).vpcId}`); - return new CfnVPCCidrBlock(input.scope, 'ipv6cidr', { - vpcId: input.vpcId, + allocateVpcIpv6Cidr(): CfnVPCCidrBlock { + if (this.scope === undefined || this.vpcId === undefined) { + throw new Error('Context is unset when trying to allocate VPC IPv6 CIDR block'); + } + return new CfnVPCCidrBlock(this.scope, 'ipv6cidr', { + vpcId: this.vpcId, amazonProvidedIpv6CidrBlock: this.amazonProvided, }); } diff --git a/packages/aws-cdk-lib/aws-ec2/lib/vpc.ts b/packages/aws-cdk-lib/aws-ec2/lib/vpc.ts index 62019ec81a694..06c9b5954329c 100644 --- a/packages/aws-cdk-lib/aws-ec2/lib/vpc.ts +++ b/packages/aws-cdk-lib/aws-ec2/lib/vpc.ts @@ -1594,11 +1594,13 @@ export class Vpc extends VpcBase { if (this.useIpv6) { this.ipv6Addresses = props.ipv6Addresses ?? Ipv6Addresses.amazonProvided(); - this.ipv6CidrBlock = this.ipv6Addresses.allocateVpcIpv6Cidr({ + this.ipv6Addresses._bind({ scope: this, vpcId: this.vpcId, }); + this.ipv6CidrBlock = this.ipv6Addresses.allocateVpcIpv6Cidr(); + this.ipv6SelectedCidr = Fn.select(0, this.resource.attrIpv6CidrBlocks); }