AWSCDK::ApplicationAutoScaling

29 types

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:

The general pattern of autoscaling will look like this:

resource = nil


capacity = resource.auto_scale_capacity({
    min_capacity: 5,
    max_capacity: 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:

capacity = nil
cpu_utilization = nil # AWSCDK::CloudWatch::Metric


capacity.scale_on_metric("ScaleToCPU", {
    metric: cpu_utilization,
    scaling_steps: [
        {upper: 10, change: -1},
        {lower: 50, change: +1},
        {lower: 70, change: +3},
    ],

    # Change this to AdjustmentType.PercentChangeInCapacity to interpret the
    # 'change' numbers before as percentages instead of capacity counts.
    adjustment_type: AWSCDK::ApplicationAutoScaling::AdjustmentType::CHANGE_IN_CAPACITY,
})

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 evaluation_periods and datapoints_to_alarm properties:

capacity = nil
cpu_utilization = nil # AWSCDK::CloudWatch::Metric


capacity.scale_on_metric("ScaleToCPUWithMultipleDatapoints", {
    metric: cpu_utilization,
    scaling_steps: [
        {upper: 10, change: -1},
        {lower: 50, change: +1},
        {lower: 70, change: +3},
    ],

    # if the cpuUtilization metric has a period of 1 minute, then data points
    # in the last 10 minutes will be evaluated
    evaluation_periods: 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
    datapoints_to_alarm: 6,
})

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:

require 'aws-cdk-lib'

table = nil # AWSCDK::DynamoDB::Table


read_capacity = table.auto_scale_read_capacity({
    min_capacity: 10,
    max_capacity: 1000,
})
read_capacity.scale_on_utilization({
    target_utilization_percent: 60,
})

Scheduled Scaling

This type of scaling is used to change capacities based on time. It works by changing the min_capacity and max_capacity of the attribute, and so can be used for two purposes:

The following schedule expressions can be used:

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:

require 'aws-cdk-lib'
resource = nil


capacity = resource.auto_scale_capacity({
    min_capacity: 1,
    max_capacity: 50,
})

capacity.scale_on_schedule("PrescaleInTheMorning", {
    schedule: AWSCDK::ApplicationAutoScaling::Schedule.cron({hour: "8", minute: "0"}),
    min_capacity: 20,
    time_zone: AWSCDK::TimeZone.AMERICA_DENVER,
})

capacity.scale_on_schedule("AllowDownscalingAtNight", {
    schedule: AWSCDK::ApplicationAutoScaling::Schedule.cron({hour: "20", minute: "0"}),
    min_capacity: 1,
    time_zone: AWSCDK::TimeZone.AMERICA_DENVER,
})

Examples

Lambda Provisioned Concurrency Auto Scaling

require 'aws-cdk-lib'

code = nil # AWSCDK::Lambda::Code


handler = AWSCDK::Lambda::Function.new(self, "MyFunction", {
    runtime: AWSCDK::Lambda::Runtime.PYTHON_3_12,
    handler: "index.handler",
    code: code,

    reserved_concurrent_executions: 2,
})

fn_ver = handler.current_version

target = AWSCDK::ApplicationAutoScaling::ScalableTarget.new(self, "ScalableTarget", {
    service_namespace: AWSCDK::ApplicationAutoScaling::ServiceNamespace::LAMBDA,
    max_capacity: 100,
    min_capacity: 10,
    resource_id: "function:#{handler.function_name}:#{fn_ver.version}",
    scalable_dimension: "lambda:function:ProvisionedConcurrency",
})

target.scale_to_track_metric("PceTracking", {
    target_value: 0.9,
    predefined_metric: AWSCDK::ApplicationAutoScaling::PredefinedMetric::LAMBDA_PROVISIONED_CONCURRENCY_UTILIZATION,
})

ElastiCache Redis shards scaling with target value

shards_scalable_target = AWSCDK::ApplicationAutoScaling::ScalableTarget.new(self, "ElastiCacheRedisShardsScalableTarget", {
    service_namespace: AWSCDK::ApplicationAutoScaling::ServiceNamespace::ELASTICACHE,
    scalable_dimension: "elasticache:replication-group:NodeGroups",
    min_capacity: 2,
    max_capacity: 10,
    resource_id: "replication-group/main-cluster",
})

shards_scalable_target.scale_to_track_metric("ElastiCacheRedisShardsCPUUtilization", {
    target_value: 20,
    predefined_metric: AWSCDK::ApplicationAutoScaling::PredefinedMetric::ELASTICACHE_PRIMARY_ENGINE_CPU_UTILIZATION,
})

SageMaker variant provisioned concurrency utilization with target value

target = AWSCDK::ApplicationAutoScaling::ScalableTarget.new(self, "SageMakerVariantScalableTarget", {
    service_namespace: AWSCDK::ApplicationAutoScaling::ServiceNamespace::SAGEMAKER,
    scalable_dimension: "sagemaker:variant:DesiredProvisionedConcurrency",
    min_capacity: 2,
    max_capacity: 10,
    resource_id: "endpoint/MyEndpoint/variant/MyVariant",
})

target.scale_to_track_metric("SageMakerVariantProvisionedConcurrencyUtilization", {
    target_value: 0.9,
    predefined_metric: AWSCDK::ApplicationAutoScaling::PredefinedMetric::SAGEMAKER_VARIANT_PROVISIONED_CONCURRENCY_UTILIZATION,
})

API Reference

Classes 8

BaseScalableAttributeRepresent an attribute for which autoscaling can be configured. CfnScalableTargetThe `AWS::ApplicationAutoScaling::ScalableTarget` resource specifies a resource that Appli CfnScalingPolicyThe `AWS::ApplicationAutoScaling::ScalingPolicy` resource defines a scaling policy that Ap ScalableTargetDefine a scalable target. ScheduleSchedule for scheduled scaling actions. StepScalingActionDefine a step scaling action. StepScalingPolicyDefine a scaling strategy which scales depending on absolute values of some metric. TargetTrackingScalingPolicy

Interfaces 17

AdjustmentTierAn adjustment. BaseScalableAttributePropsProperties for a ScalableTableAttribute. BaseTargetTrackingPropsBase interface for target tracking props. BasicStepScalingPolicyProps BasicTargetTrackingScalingPolicyPropsProperties for a Target Tracking policy that include the metric but exclude the target. CfnScalableTargetPropsProperties for defining a `CfnScalableTarget`. CfnScalingPolicyPropsProperties for defining a `CfnScalingPolicy`. CronOptionsOptions to configure a cron expression. EnableScalingPropsProperties for enabling Application Auto Scaling. IScalableTarget ScalableTargetAttributesAttributes for importing a scalable target. ScalableTargetPropsProperties for a scalable target. ScalingIntervalA range of metric values in which to apply a certain scaling operation. ScalingScheduleA scheduled scaling action. StepScalingActionPropsProperties for a scaling policy. StepScalingPolicyProps TargetTrackingScalingPolicyPropsProperties for a concrete TargetTrackingPolicy.

Enums 4

AdjustmentTypeHow adjustment numbers are interpreted. MetricAggregationTypeHow the scaling metric is going to be aggregated. PredefinedMetricOne of the predefined autoscaling metrics. ServiceNamespaceThe service that supports Application AutoScaling.