Skip to content

Commit 1f004f6

Browse files
piradeepkrix0rrr
authored andcommitted
fix(appscaling): fix StepScaling (#2522)
Add ScalingTargetId and default AdjustmentType.
1 parent 67960f8 commit 1f004f6

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

packages/@aws-cdk/aws-applicationautoscaling/lib/step-scaling-action.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,13 @@ export class StepScalingAction extends cdk.Construct implements cloudwatch.IAlar
8383
constructor(scope: cdk.Construct, id: string, props: StepScalingActionProps) {
8484
super(scope, id);
8585

86+
// Cloudformation requires either the ResourceId, ScalableDimension, and ServiceNamespace
87+
// properties, or the ScalingTargetId property, but not both.
88+
// https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-applicationautoscaling-scalingpolicy.html
8689
const resource = new CfnScalingPolicy(this, 'Resource', {
8790
policyName: props.policyName || this.node.uniqueId,
8891
policyType: 'StepScaling',
92+
scalingTargetId: props.scalingTarget.scalableTargetId,
8993
stepScalingPolicyConfiguration: {
9094
adjustmentType: props.adjustmentType,
9195
cooldown: props.cooldownSec,

packages/@aws-cdk/aws-applicationautoscaling/lib/step-scaling-policy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export class StepScalingPolicy extends cdk.Construct {
8585
const threshold = intervals[alarms.lowerAlarmIntervalIndex].upper;
8686

8787
this.lowerAction = new StepScalingAction(this, 'LowerPolicy', {
88-
adjustmentType: props.adjustmentType,
88+
adjustmentType,
8989
cooldownSec: props.cooldownSec,
9090
metricAggregationType: aggregationTypeFromMetric(props.metric),
9191
minAdjustmentMagnitude: props.minAdjustmentMagnitude,
@@ -115,7 +115,7 @@ export class StepScalingPolicy extends cdk.Construct {
115115
const threshold = intervals[alarms.upperAlarmIntervalIndex].lower;
116116

117117
this.upperAction = new StepScalingAction(this, 'UpperPolicy', {
118-
adjustmentType: props.adjustmentType,
118+
adjustmentType,
119119
cooldownSec: props.cooldownSec,
120120
metricAggregationType: aggregationTypeFromMetric(props.metric),
121121
minAdjustmentMagnitude: props.minAdjustmentMagnitude,

packages/@aws-cdk/aws-applicationautoscaling/test/test.step-scaling-policy.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { expect, haveResource } from '@aws-cdk/assert';
12
import { SynthUtils } from '@aws-cdk/assert';
23
import cloudwatch = require('@aws-cdk/aws-cloudwatch');
34
import cdk = require('@aws-cdk/cdk');
@@ -116,6 +117,43 @@ export = {
116117

117118
test.done();
118119
},
120+
121+
'test step scaling on metric'(test: Test) {
122+
// GIVEN
123+
const stack = new cdk.Stack();
124+
const target = createScalableTarget(stack);
125+
126+
// WHEN
127+
target.scaleOnMetric('Tracking', {
128+
metric: new cloudwatch.Metric({ namespace: 'Test', metricName: 'Metric' }),
129+
scalingSteps: [
130+
{ upper: 0, change: -1 },
131+
{ lower: 100, change: +1 },
132+
{ lower: 500, change: +5 }
133+
]
134+
});
135+
136+
// THEN
137+
expect(stack).to(haveResource('AWS::ApplicationAutoScaling::ScalingPolicy', {
138+
PolicyType: "StepScaling",
139+
ScalingTargetId: {
140+
Ref: "Target3191CF44"
141+
},
142+
StepScalingPolicyConfiguration: {
143+
AdjustmentType: "ChangeInCapacity",
144+
MetricAggregationType: "Average",
145+
StepAdjustments: [
146+
{
147+
MetricIntervalUpperBound: 0,
148+
ScalingAdjustment: -1
149+
}
150+
]
151+
}
152+
153+
}));
154+
155+
test.done();
156+
}
119157
};
120158

121159
/**

0 commit comments

Comments
 (0)