Skip to content

Commit

Permalink
fix(appscaling): fix StepScaling (#2522)
Browse files Browse the repository at this point in the history
Add ScalingTargetId and default AdjustmentType.
  • Loading branch information
piradeepk authored and rix0rrr committed May 20, 2019
1 parent 67960f8 commit 1f004f6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,13 @@ export class StepScalingAction extends cdk.Construct implements cloudwatch.IAlar
constructor(scope: cdk.Construct, id: string, props: StepScalingActionProps) {
super(scope, id);

// Cloudformation requires either the ResourceId, ScalableDimension, and ServiceNamespace
// properties, or the ScalingTargetId property, but not both.
// https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-applicationautoscaling-scalingpolicy.html
const resource = new CfnScalingPolicy(this, 'Resource', {
policyName: props.policyName || this.node.uniqueId,
policyType: 'StepScaling',
scalingTargetId: props.scalingTarget.scalableTargetId,
stepScalingPolicyConfiguration: {
adjustmentType: props.adjustmentType,
cooldown: props.cooldownSec,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class StepScalingPolicy extends cdk.Construct {
const threshold = intervals[alarms.lowerAlarmIntervalIndex].upper;

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

this.upperAction = new StepScalingAction(this, 'UpperPolicy', {
adjustmentType: props.adjustmentType,
adjustmentType,
cooldownSec: props.cooldownSec,
metricAggregationType: aggregationTypeFromMetric(props.metric),
minAdjustmentMagnitude: props.minAdjustmentMagnitude,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { expect, haveResource } from '@aws-cdk/assert';
import { SynthUtils } from '@aws-cdk/assert';
import cloudwatch = require('@aws-cdk/aws-cloudwatch');
import cdk = require('@aws-cdk/cdk');
Expand Down Expand Up @@ -116,6 +117,43 @@ export = {

test.done();
},

'test step scaling on metric'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const target = createScalableTarget(stack);

// WHEN
target.scaleOnMetric('Tracking', {
metric: new cloudwatch.Metric({ namespace: 'Test', metricName: 'Metric' }),
scalingSteps: [
{ upper: 0, change: -1 },
{ lower: 100, change: +1 },
{ lower: 500, change: +5 }
]
});

// THEN
expect(stack).to(haveResource('AWS::ApplicationAutoScaling::ScalingPolicy', {
PolicyType: "StepScaling",
ScalingTargetId: {
Ref: "Target3191CF44"
},
StepScalingPolicyConfiguration: {
AdjustmentType: "ChangeInCapacity",
MetricAggregationType: "Average",
StepAdjustments: [
{
MetricIntervalUpperBound: 0,
ScalingAdjustment: -1
}
]
}

}));

test.done();
}
};

/**
Expand Down

0 comments on commit 1f004f6

Please sign in to comment.