diff --git a/packages/aws-cdk-lib/aws-ec2/lib/instance-types.ts b/packages/aws-cdk-lib/aws-ec2/lib/instance-types.ts index 33e7bc63d3847..c540f8498eb28 100644 --- a/packages/aws-cdk-lib/aws-ec2/lib/instance-types.ts +++ b/packages/aws-cdk-lib/aws-ec2/lib/instance-types.ts @@ -1534,7 +1534,7 @@ export class InstanceType { } public sameInstanceClassAs(other: InstanceType): boolean { - const instanceClass: RegExp = /^([a-z]+\d{1,2}[a-z]*)\.([a-z0-9]+)$/; + const instanceClass: RegExp = /^([a-z]+\d{1,2}[a-z\-]*)\.([a-z0-9\-]+)$/; const instanceClassId = this.instanceTypeIdentifier.match(instanceClass); const otherInstanceClassId = other.instanceTypeIdentifier.match(instanceClass); if (instanceClassId == null || otherInstanceClassId == null) { diff --git a/packages/aws-cdk-lib/aws-ec2/test/instance.test.ts b/packages/aws-cdk-lib/aws-ec2/test/instance.test.ts index 4c0516c655fb9..039ef7188179f 100644 --- a/packages/aws-cdk-lib/aws-ec2/test/instance.test.ts +++ b/packages/aws-cdk-lib/aws-ec2/test/instance.test.ts @@ -688,6 +688,24 @@ test('sameInstanceClassAs compares InstanceTypes correctly regardless of size', expect(largerInstanceType.sameInstanceClassAs(comparitor)).toBeTruthy(); }); +test('sameInstanceClassAs compares InstanceTypes contains dashes', () => { + // GIVEN + const comparitor = InstanceType.of(InstanceClass.M7I_FLEX, InstanceSize.LARGE); + //WHEN + const largerInstanceType = InstanceType.of(InstanceClass.M7I_FLEX, InstanceSize.XLARGE); + //THEN + expect(largerInstanceType.sameInstanceClassAs(comparitor)).toBeTruthy(); +}); + +test('sameInstanceClassAs compares InstanceSize contains dashes', () => { + // GIVEN + const comparitor = new InstanceType('m7a.metal-48xl'); + //WHEN + const largerInstanceType = new InstanceType('m7a.xlarge'); + //THEN + expect(largerInstanceType.sameInstanceClassAs(comparitor)).toBeTruthy(); +}); + test('sameInstanceClassAs compares different InstanceTypes correctly', () => { // GIVEN const comparitor = InstanceType.of(InstanceClass.C4, InstanceSize.LARGE);