Package software.amazon.awscdk.services.applicationautoscaling
AWS Auto Scaling Construct Library
Application AutoScaling is used to configure autoscaling for all services other than scaling EC2 instances. For example, you will use this to scale ECS tasks, DynamoDB capacity, Spot Fleet sizes, Comprehend document classification endpoints, Lambda function provisioned concurrency and more.
As a CDK user, you will probably not have to interact with this library directly; instead, it will be used by other construct libraries to offer AutoScaling features for their own constructs.
This document will describe the general autoscaling features and concepts; your particular service may offer only a subset of these.
AutoScaling basics
Resources can offer one or more attributes to autoscale, typically representing some capacity dimension of the underlying service. For example, a DynamoDB Table offers autoscaling of the read and write capacity of the table proper and its Global Secondary Indexes, an ECS Service offers autoscaling of its task count, an RDS Aurora cluster offers scaling of its replica count, and so on.
When you enable autoscaling for an attribute, you specify a minimum and a maximum value for the capacity. AutoScaling policies that respond to metrics will never go higher or lower than the indicated capacity (but scheduled scaling actions might, see below).
There are three ways to scale your capacity:
- In response to a metric (also known as step scaling); for example, you might want to scale out if the CPU usage across your cluster starts to rise, and scale in when it drops again.
- By trying to keep a certain metric around a given value (also known as target tracking scaling); you might want to automatically scale out an in to keep your CPU usage around 50%.
- On a schedule; you might want to organize your scaling around traffic flows you expect, by scaling out in the morning and scaling in in the evening.
The general pattern of autoscaling will look like this:
SomeScalableResource resource;
ScalableAttribute capacity = resource.autoScaleCapacity(new Caps()
.minCapacity(5)
.maxCapacity(100)
);
Step Scaling
This type of scaling scales in and out in deterministic steps that you configure, in response to metric values. For example, your scaling strategy to scale in response to CPU usage might look like this:
Scaling -1 (no change) +1 +3
│ │ │ │ │
├────────┼───────────────────────┼────────┼────────┤
│ │ │ │ │
CPU usage 0% 10% 50% 70% 100%
(Note that this is not necessarily a recommended scaling strategy, but it's a possible one. You will have to determine what thresholds are right for you).
You would configure it like this:
ScalableAttribute capacity;
Metric cpuUtilization;
capacity.scaleOnMetric("ScaleToCPU", BasicStepScalingPolicyProps.builder()
.metric(cpuUtilization)
.scalingSteps(List.of(ScalingInterval.builder().upper(10).change(-1).build(), ScalingInterval.builder().lower(50).change(+1).build(), ScalingInterval.builder().lower(70).change(+3).build()))
// Change this to AdjustmentType.PercentChangeInCapacity to interpret the
// 'change' numbers before as percentages instead of capacity counts.
.adjustmentType(AdjustmentType.CHANGE_IN_CAPACITY)
.build());
The AutoScaling construct library will create the required CloudWatch alarms and AutoScaling policies for you.
Scaling based on multiple datapoints
The Step Scaling configuration above will initiate a scaling event when a single
datapoint of the scaling metric is breaching a scaling step breakpoint. In cases
where you might want to initiate scaling actions on a larger number of datapoints
(ie in order to smooth out randomness in the metric data), you can use the
optional evaluationPeriods and datapointsToAlarm properties:
ScalableAttribute capacity;
Metric cpuUtilization;
capacity.scaleOnMetric("ScaleToCPUWithMultipleDatapoints", BasicStepScalingPolicyProps.builder()
.metric(cpuUtilization)
.scalingSteps(List.of(ScalingInterval.builder().upper(10).change(-1).build(), ScalingInterval.builder().lower(50).change(+1).build(), ScalingInterval.builder().lower(70).change(+3).build()))
// if the cpuUtilization metric has a period of 1 minute, then data points
// in the last 10 minutes will be evaluated
.evaluationPeriods(10)
// Only trigger a scaling action when 6 datapoints out of the last 10 are
// breaching. If this is left unspecified, then ALL datapoints in the
// evaluation period must be breaching to trigger a scaling action
.datapointsToAlarm(6)
.build());
Target Tracking Scaling
This type of scaling scales in and out in order to keep a metric (typically representing utilization) around a value you prefer. This type of scaling is typically heavily service-dependent in what metric you can use, and so different services will have different methods here to set up target tracking scaling.
The following example configures the read capacity of a DynamoDB table to be around 60% utilization:
import software.amazon.awscdk.services.dynamodb.*;
Table table;
IScalableTableAttribute readCapacity = table.autoScaleReadCapacity(EnableScalingProps.builder()
.minCapacity(10)
.maxCapacity(1000)
.build());
readCapacity.scaleOnUtilization(UtilizationScalingProps.builder()
.targetUtilizationPercent(60)
.build());
Scheduled Scaling
This type of scaling is used to change capacities based on time. It works
by changing the minCapacity and maxCapacity of the attribute, and so
can be used for two purposes:
- Scale in and out on a schedule by setting the
minCapacityhigh or themaxCapacitylow. - Still allow the regular scaling actions to do their job, but restrict
the range they can scale over (by setting both
minCapacityandmaxCapacitybut changing their range over time).
The following schedule expressions can be used:
at(yyyy-mm-ddThh:mm:ss)-- scale at a particular moment in timerate(value unit)-- scale every minute/hour/daycron(mm hh dd mm dow)-- scale on arbitrary schedules
Of these, the cron expression is the most useful but also the most
complicated. A schedule is expressed as a cron expression. The Schedule class has a cron method to help build cron expressions.
The following example scales the fleet out in the morning, and lets natural scaling take over at night:
import software.amazon.awscdk.TimeZone;
SomeScalableResource resource;
ScalableAttribute capacity = resource.autoScaleCapacity(new Caps()
.minCapacity(1)
.maxCapacity(50)
);
capacity.scaleOnSchedule("PrescaleInTheMorning", ScalingSchedule.builder()
.schedule(Schedule.cron(CronOptions.builder().hour("8").minute("0").build()))
.minCapacity(20)
.timeZone(TimeZone.AMERICA_DENVER)
.build());
capacity.scaleOnSchedule("AllowDownscalingAtNight", ScalingSchedule.builder()
.schedule(Schedule.cron(CronOptions.builder().hour("20").minute("0").build()))
.minCapacity(1)
.timeZone(TimeZone.AMERICA_DENVER)
.build());
Examples
Lambda Provisioned Concurrency Auto Scaling
import software.amazon.awscdk.services.lambda.*;
Code code;
Function handler = Function.Builder.create(this, "MyFunction")
.runtime(Runtime.PYTHON_3_12)
.handler("index.handler")
.code(code)
.reservedConcurrentExecutions(2)
.build();
Version fnVer = handler.getCurrentVersion();
ScalableTarget target = ScalableTarget.Builder.create(this, "ScalableTarget")
.serviceNamespace(ServiceNamespace.LAMBDA)
.maxCapacity(100)
.minCapacity(10)
.resourceId(String.format("function:%s:%s", handler.getFunctionName(), fnVer.getVersion()))
.scalableDimension("lambda:function:ProvisionedConcurrency")
.build();
target.scaleToTrackMetric("PceTracking", BasicTargetTrackingScalingPolicyProps.builder()
.targetValue(0.9)
.predefinedMetric(PredefinedMetric.LAMBDA_PROVISIONED_CONCURRENCY_UTILIZATION)
.build());
ElastiCache Redis shards scaling with target value
ScalableTarget shardsScalableTarget = ScalableTarget.Builder.create(this, "ElastiCacheRedisShardsScalableTarget")
.serviceNamespace(ServiceNamespace.ELASTICACHE)
.scalableDimension("elasticache:replication-group:NodeGroups")
.minCapacity(2)
.maxCapacity(10)
.resourceId("replication-group/main-cluster")
.build();
shardsScalableTarget.scaleToTrackMetric("ElastiCacheRedisShardsCPUUtilization", BasicTargetTrackingScalingPolicyProps.builder()
.targetValue(20)
.predefinedMetric(PredefinedMetric.ELASTICACHE_PRIMARY_ENGINE_CPU_UTILIZATION)
.build());
SageMaker variant provisioned concurrency utilization with target value
ScalableTarget target = ScalableTarget.Builder.create(this, "SageMakerVariantScalableTarget")
.serviceNamespace(ServiceNamespace.SAGEMAKER)
.scalableDimension("sagemaker:variant:DesiredProvisionedConcurrency")
.minCapacity(2)
.maxCapacity(10)
.resourceId("endpoint/MyEndpoint/variant/MyVariant")
.build();
target.scaleToTrackMetric("SageMakerVariantProvisionedConcurrencyUtilization", BasicTargetTrackingScalingPolicyProps.builder()
.targetValue(0.9)
.predefinedMetric(PredefinedMetric.SAGEMAKER_VARIANT_PROVISIONED_CONCURRENCY_UTILIZATION)
.build());
-
ClassDescriptionAn adjustment.A builder for
AdjustmentTierAn implementation forAdjustmentTierHow adjustment numbers are interpreted.Represent an attribute for which autoscaling can be configured.Properties for a ScalableTableAttribute.A builder forBaseScalableAttributePropsAn implementation forBaseScalableAttributePropsBase interface for target tracking props.A builder forBaseTargetTrackingPropsAn implementation forBaseTargetTrackingPropsExample:A builder forBasicStepScalingPolicyPropsAn implementation forBasicStepScalingPolicyPropsProperties for a Target Tracking policy that include the metric but exclude the target.A builder forBasicTargetTrackingScalingPolicyPropsAn implementation forBasicTargetTrackingScalingPolicyPropsTheAWS::ApplicationAutoScaling::ScalableTargetresource specifies a resource that Application Auto Scaling can scale, such as an AWS::DynamoDB::Table or AWS::ECS::Service resource.A fluent builder forCfnScalableTarget.ScalableTargetActionspecifies the minimum and maximum capacity for theScalableTargetActionproperty of the AWS::ApplicationAutoScaling::ScalableTarget ScheduledAction property type.A builder forCfnScalableTarget.ScalableTargetActionPropertyAn implementation forCfnScalableTarget.ScalableTargetActionPropertyScheduledActionis a property of the AWS::ApplicationAutoScaling::ScalableTarget resource that specifies a scheduled action for a scalable target.A builder forCfnScalableTarget.ScheduledActionPropertyAn implementation forCfnScalableTarget.ScheduledActionPropertySuspendedStateis a property of the AWS::ApplicationAutoScaling::ScalableTarget resource that specifies whether the scaling activities for a scalable target are in a suspended state.A builder forCfnScalableTarget.SuspendedStatePropertyAn implementation forCfnScalableTarget.SuspendedStatePropertyProperties for defining aCfnScalableTarget.A builder forCfnScalableTargetPropsAn implementation forCfnScalableTargetPropsTheAWS::ApplicationAutoScaling::ScalingPolicyresource defines a scaling policy that Application Auto Scaling uses to adjust the capacity of a scalable target.A fluent builder forCfnScalingPolicy.Contains customized metric specification information for a target tracking scaling policy for Application Auto Scaling.A builder forCfnScalingPolicy.CustomizedMetricSpecificationPropertyAn implementation forCfnScalingPolicy.CustomizedMetricSpecificationPropertyMetricDimensionspecifies a name/value pair that is part of the identity of a CloudWatch metric for theDimensionsproperty of the AWS::ApplicationAutoScaling::ScalingPolicy CustomizedMetricSpecification property type.A builder forCfnScalingPolicy.MetricDimensionPropertyAn implementation forCfnScalingPolicy.MetricDimensionPropertyContains predefined metric specification information for a target tracking scaling policy for Application Auto Scaling.A builder forCfnScalingPolicy.PredefinedMetricSpecificationPropertyAn implementation forCfnScalingPolicy.PredefinedMetricSpecificationPropertyRepresents a CloudWatch metric of your choosing for a predictive scaling policy.An implementation forCfnScalingPolicy.PredictiveScalingCustomizedCapacityMetricPropertyThe customized load metric specification.An implementation forCfnScalingPolicy.PredictiveScalingCustomizedLoadMetricPropertyOne or more metric data queries to provide data points for a metric specification.An implementation forCfnScalingPolicy.PredictiveScalingCustomizedScalingMetricPropertyThe metric data to return.An implementation forCfnScalingPolicy.PredictiveScalingMetricDataQueryPropertyDescribes the dimension of a metric.An implementation forCfnScalingPolicy.PredictiveScalingMetricDimensionPropertyDescribes the scaling metric.A builder forCfnScalingPolicy.PredictiveScalingMetricPropertyAn implementation forCfnScalingPolicy.PredictiveScalingMetricPropertyThis structure specifies the metrics and target utilization settings for a predictive scaling policy.An implementation forCfnScalingPolicy.PredictiveScalingMetricSpecificationPropertyThis structure defines the CloudWatch metric to return, along with the statistic and unit.A builder forCfnScalingPolicy.PredictiveScalingMetricStatPropertyAn implementation forCfnScalingPolicy.PredictiveScalingMetricStatPropertyRepresents a predictive scaling policy configuration.An implementation forCfnScalingPolicy.PredictiveScalingPolicyConfigurationPropertyDescribes a load metric for a predictive scaling policy.An implementation forCfnScalingPolicy.PredictiveScalingPredefinedLoadMetricPropertyRepresents a metric pair for a predictive scaling policy.An implementation forCfnScalingPolicy.PredictiveScalingPredefinedMetricPairPropertyDescribes a scaling metric for a predictive scaling policy.An implementation forCfnScalingPolicy.PredictiveScalingPredefinedScalingMetricPropertyStepAdjustmentspecifies a step adjustment for theStepAdjustmentsproperty of the AWS::ApplicationAutoScaling::ScalingPolicy StepScalingPolicyConfiguration property type.A builder forCfnScalingPolicy.StepAdjustmentPropertyAn implementation forCfnScalingPolicy.StepAdjustmentPropertyStepScalingPolicyConfigurationis a property of the AWS::ApplicationAutoScaling::ScalingPolicy resource that specifies a step scaling policy configuration for Application Auto Scaling.A builder forCfnScalingPolicy.StepScalingPolicyConfigurationPropertyAn implementation forCfnScalingPolicy.StepScalingPolicyConfigurationPropertyThe metric data to return.A builder forCfnScalingPolicy.TargetTrackingMetricDataQueryPropertyAn implementation forCfnScalingPolicy.TargetTrackingMetricDataQueryPropertyTargetTrackingMetricDimensionspecifies a name/value pair that is part of the identity of a CloudWatch metric for theDimensionsproperty of the AWS::ApplicationAutoScaling::ScalingPolicy TargetTrackingMetric property type.A builder forCfnScalingPolicy.TargetTrackingMetricDimensionPropertyAn implementation forCfnScalingPolicy.TargetTrackingMetricDimensionPropertyRepresents a specific metric for a target tracking scaling policy for Application Auto Scaling.A builder forCfnScalingPolicy.TargetTrackingMetricPropertyAn implementation forCfnScalingPolicy.TargetTrackingMetricPropertyThis structure defines the CloudWatch metric to return, along with the statistic and unit.A builder forCfnScalingPolicy.TargetTrackingMetricStatPropertyAn implementation forCfnScalingPolicy.TargetTrackingMetricStatPropertyTargetTrackingScalingPolicyConfigurationis a property of the AWS::ApplicationAutoScaling::ScalingPolicy resource that specifies a target tracking scaling policy configuration for Application Auto Scaling.An implementation forCfnScalingPolicy.TargetTrackingScalingPolicyConfigurationPropertyProperties for defining aCfnScalingPolicy.A builder forCfnScalingPolicyPropsAn implementation forCfnScalingPolicyPropsOptions to configure a cron expression.A builder forCronOptionsAn implementation forCronOptionsProperties for enabling Application Auto Scaling.A builder forEnableScalingPropsAn implementation forEnableScalingPropsInternal default implementation forIScalableTarget.A proxy class which represents a concrete javascript instance of this type.(experimental) Indicates that this resource can be referenced as a ScalableTarget.Internal default implementation forIScalableTargetRef.A proxy class which represents a concrete javascript instance of this type.(experimental) Indicates that this resource can be referenced as a ScalingPolicy.Internal default implementation forIScalingPolicyRef.A proxy class which represents a concrete javascript instance of this type.How the scaling metric is going to be aggregated.One of the predefined autoscaling metrics.Define a scalable target.A fluent builder forScalableTarget.Properties for a scalable target.A builder forScalableTargetPropsAn implementation forScalableTargetPropsA reference to a ScalableTarget resource.A builder forScalableTargetReferenceAn implementation forScalableTargetReferenceA range of metric values in which to apply a certain scaling operation.A builder forScalingIntervalAn implementation forScalingIntervalA reference to a ScalingPolicy resource.A builder forScalingPolicyReferenceAn implementation forScalingPolicyReferenceA scheduled scaling action.A builder forScalingScheduleAn implementation forScalingScheduleSchedule for scheduled scaling actions.The service that supports Application AutoScaling.Define a step scaling action.A fluent builder forStepScalingAction.Properties for a scaling policy.A builder forStepScalingActionPropsAn implementation forStepScalingActionPropsDefine a scaling strategy which scales depending on absolute values of some metric.A fluent builder forStepScalingPolicy.Example:A builder forStepScalingPolicyPropsAn implementation forStepScalingPolicyPropsExample:A fluent builder forTargetTrackingScalingPolicy.Properties for a concrete TargetTrackingPolicy.A builder forTargetTrackingScalingPolicyPropsAn implementation forTargetTrackingScalingPolicyProps