Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(autoscaling): step scaling without adjustment type fails (#29158)
### Reason for this change Step Scaling without `adjustmentType` fails with CFn error `You must specify an AdjustmentType for policy type: StepScaling`. - Reproduction code ```ts const asg = new autoscaling.AutoScalingGroup(stack, 'ASG', { vpc, instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO), machineImage: new ec2.AmazonLinuxImage({ generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2 }), }); asg.scaleOnMetric('StepScaling', { metric: new cloudwatch.Metric({ namespace: 'AWS/EC2', metricName: 'CPUUtilization', dimensionsMap: { AutoScalingGroupName: asg.autoScalingGroupName } }), scalingSteps: [ { upper: 10, change: -1 }, { lower: 50, change: +1 }, { lower: 90, change: +2 }, ], evaluationPeriods: 10, datapointsToAlarm: 5, metricAggregationType: autoscaling.MetricAggregationType.MAXIMUM, // adjustmentType: autoscaling.AdjustmentType.CHANGE_IN_CAPACITY, }); ``` ### Description of changes According to [the CDK code](https://github.com/aws/aws-cdk/blob/v2.122.0/packages/aws-cdk-lib/aws-autoscaling/lib/step-scaling-policy.ts#L105-L115), `CHANGE_IN_CAPACITY` will be used if `adjustmentType` is not specified in the prop. But the variable is not passed into `StepScalingAction` construct (instead `props.adjustmentType` is passed as is). [The documentation](https://github.com/aws/aws-cdk/blob/v2.122.0/packages/aws-cdk-lib/aws-autoscaling/lib/step-scaling-policy.ts#L26) also says that the default value is `CHANGE_IN_CAPACITY`. So we should use `adjustmentType` instead of `props.adjustmentType`. ```ts const adjustmentType = props.adjustmentType || AdjustmentType.CHANGE_IN_CAPACITY; const changesAreAbsolute = adjustmentType === AdjustmentType.EXACT_CAPACITY; // ... // ... this.lowerAction = new StepScalingAction(this, 'LowerPolicy', { // adjustmentType: props.adjustmentType, adjustmentType, ``` **The fact that the error occurs if `props.adjustmentType` is not specified means that the user's successful existing stack always specifies `props.adjustmentType`. In other words, no change to the existing resource will occur due to this change, so there is no need for a feature flag.** ### Description of how you validated changes ~~Unit tests without integ tests, because this PR could cover this bug by unit tests.~~ Both unit and integ tests. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information