AWS CDK Application Auto Scaling

1.204.0 · active · verified Thu Apr 16

The `aws-cdk-aws-applicationautoscaling` library provides constructs for defining application auto-scaling policies within the AWS Cloud Development Kit (CDK). It enables users to programmatically configure scalable targets and scaling policies for various AWS services like ECS services, DynamoDB tables, and Aurora clusters. This library is part of the AWS CDK v1, which receives frequent updates and is distinct from the consolidated v2 package.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to define a `ScalableTarget` for an ECS service's desired count and apply a CPU utilization scaling policy. Remember to replace the placeholder `resource_id` with your actual service details and ensure your environment variables `CDK_DEFAULT_ACCOUNT` and `CDK_DEFAULT_REGION` are set.

import os
from aws_cdk import App, Stack, Duration
from aws_cdk import aws_applicationautoscaling as appautoscaling

class ApplicationAutoScalingStack(Stack):
    def __init__(self, scope: App, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        # Define a scalable target for an ECS service's desired count.
        # In a real application, 'resource_id' would refer to an existing
        # ECS service ARN or logical ID. Adjust 'resource_id' to your target service.
        scalable_target = appautoscaling.ScalableTarget(
            self, "MyEcsScalableTarget",
            service_namespace=appautoscaling.ServiceNamespace.ECS,
            min_capacity=1,
            max_capacity=10,
            # IMPORTANT: Replace with your actual ECS Cluster/Service name
            resource_id="service/YourEcsClusterName/YourEcsServiceName",
            scalable_dimension="ecs:service:DesiredCount"
        )

        # Scale out based on CPU utilization
        scalable_target.scale_on_cpu_utilization(
            "CpuScalingPolicy",
            target_utilization_percent=70,
            scale_in_cooldown=Duration.seconds(60),
            scale_out_cooldown=Duration.seconds(60)
        )

app = App()
ApplicationAutoScalingStack(app, "ApplicationAutoScalingStack",
    env={
        'account': os.environ.get('CDK_DEFAULT_ACCOUNT', '123456789012'),
        'region': os.environ.get('CDK_DEFAULT_REGION', 'us-east-1')
    }
)
app.synth()

view raw JSON →