diff --git a/packages/@aws-cdk/aws-ec2-alpha/lib/ipam.ts b/packages/@aws-cdk/aws-ec2-alpha/lib/ipam.ts index 4e27d67183c90..2d84830103727 100644 --- a/packages/@aws-cdk/aws-ec2-alpha/lib/ipam.ts +++ b/packages/@aws-cdk/aws-ec2-alpha/lib/ipam.ts @@ -573,13 +573,17 @@ function createIpamPool( poolOptions: PoolOptions, scopeId: string, ): IpamPool { - const isLocaleInOperatingRegions = scopeOptions.ipamOperatingRegions - ? scopeOptions.ipamOperatingRegions.map(region => ({ regionName: region })) - .some(region => region.regionName === poolOptions.locale) - : false; - - if (!isLocaleInOperatingRegions) { - throw new Error(`The provided locale '${poolOptions.locale}' is not in the operating regions.`); + // Only check locale if it's provided since it's an optional field + if (poolOptions.locale) { + const isLocaleInOperatingRegions = scopeOptions.ipamOperatingRegions + ? scopeOptions.ipamOperatingRegions.map(region => ({ regionName: region })) + .some(region => region.regionName === poolOptions.locale) + : false; + + if (!isLocaleInOperatingRegions) { + throw new Error(`The provided locale '${poolOptions.locale}' is not in the operating regions (${scopeOptions.ipamOperatingRegions}). ` + + 'If specified, locale must be configured as an operating region for the IPAM.'); + } } return new IpamPool(scope, id, { @@ -592,4 +596,3 @@ function createIpamPool( awsService: poolOptions.awsService, }); } - diff --git a/packages/@aws-cdk/aws-ec2-alpha/test/ipam.test.ts b/packages/@aws-cdk/aws-ec2-alpha/test/ipam.test.ts index 79a945aba0734..46d5f27ebdbc3 100644 --- a/packages/@aws-cdk/aws-ec2-alpha/test/ipam.test.ts +++ b/packages/@aws-cdk/aws-ec2-alpha/test/ipam.test.ts @@ -237,7 +237,11 @@ describe('IPAM Test', () => { publicIpSource: IpamPoolPublicIpSource.AMAZON, locale: 'us-west-1', // Incorrect Region }; - expect(() => ipamRegion.publicScope.addPool('TestPool', poolOptions)).toThrow("The provided locale 'us-west-1' is not in the operating regions."); + expect(() => ipamRegion.publicScope.addPool('TestPool', poolOptions)) + .toThrow( + `The provided locale 'us-west-1' is not in the operating regions (${ipamRegion.operatingRegions}). ` + + 'If specified, locale must be configured as an operating region for the IPAM.', + ); }); test('IPAM handles operating regions correctly', () => {