diff --git a/packages/@aws-cdk/aws-applicationautoscaling/lib/step-scaling-action.ts b/packages/@aws-cdk/aws-applicationautoscaling/lib/step-scaling-action.ts index d889522658977..860ac7c1f609b 100644 --- a/packages/@aws-cdk/aws-applicationautoscaling/lib/step-scaling-action.ts +++ b/packages/@aws-cdk/aws-applicationautoscaling/lib/step-scaling-action.ts @@ -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, diff --git a/packages/@aws-cdk/aws-applicationautoscaling/lib/step-scaling-policy.ts b/packages/@aws-cdk/aws-applicationautoscaling/lib/step-scaling-policy.ts index 55180dc345aec..80cb19e90e620 100644 --- a/packages/@aws-cdk/aws-applicationautoscaling/lib/step-scaling-policy.ts +++ b/packages/@aws-cdk/aws-applicationautoscaling/lib/step-scaling-policy.ts @@ -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, @@ -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, diff --git a/packages/@aws-cdk/aws-applicationautoscaling/test/test.step-scaling-policy.ts b/packages/@aws-cdk/aws-applicationautoscaling/test/test.step-scaling-policy.ts index 6e60c3cbd6067..1455fa0e84689 100644 --- a/packages/@aws-cdk/aws-applicationautoscaling/test/test.step-scaling-policy.ts +++ b/packages/@aws-cdk/aws-applicationautoscaling/test/test.step-scaling-policy.ts @@ -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'); @@ -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(); + } }; /**