diff --git a/packages/aws-cdk-lib/aws-autoscaling/lib/auto-scaling-group.ts b/packages/aws-cdk-lib/aws-autoscaling/lib/auto-scaling-group.ts index e38c2456ebcb5..e0105d02ce4dd 100644 --- a/packages/aws-cdk-lib/aws-autoscaling/lib/auto-scaling-group.ts +++ b/packages/aws-cdk-lib/aws-autoscaling/lib/auto-scaling-group.ts @@ -1759,6 +1759,9 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements if (props.blockDevices) { throw new Error('Setting \'blockDevices\' must not be set when \'launchTemplate\' or \'mixedInstancesPolicy\' is set'); } + if (props.requireImdsv2) { + throw new Error('Setting \'requireImdsv2\' must not be set when \'launchTemplate\' or \'mixedInstancesPolicy\' is set'); + } } /** diff --git a/packages/aws-cdk-lib/aws-autoscaling/test/auto-scaling-group.test.ts b/packages/aws-cdk-lib/aws-autoscaling/test/auto-scaling-group.test.ts index 1ccbdd5f423ba..2b78904dd300c 100644 --- a/packages/aws-cdk-lib/aws-autoscaling/test/auto-scaling-group.test.ts +++ b/packages/aws-cdk-lib/aws-autoscaling/test/auto-scaling-group.test.ts @@ -2947,6 +2947,26 @@ describe('InstanceMaintenancePolicy', () => { }); }).toThrow(/The difference between minHealthyPercentage and maxHealthyPercentage cannot be greater than 100, got 200/); }); + + test('throws if requireImdsv2 set when launchTemplate is set', () => { + // GIVEN + const stack = new cdk.Stack(); + stack.node.setContext(AUTOSCALING_GENERATE_LAUNCH_TEMPLATE, true); + const vpc = mockVpc(stack); + const lt = LaunchTemplate.fromLaunchTemplateAttributes(stack, 'imported-lt', { + launchTemplateId: 'test-lt-id', + versionNumber: '0', + }); + + // THEN + expect(() => { + new autoscaling.AutoScalingGroup(stack, 'MyFleet', { + vpc, + launchTemplate: lt, + requireImdsv2: true, + }); + }).toThrow(/Setting \'requireImdsv2\' must not be set when \'launchTemplate\' or \'mixedInstancesPolicy\' is set/); + }); }); function mockSecurityGroup(stack: cdk.Stack) {