AWS CDK Auto Scaling Constructs

1.204.0 · active · verified Fri Apr 17

The `aws-cdk-aws-autoscaling` package provides CDK constructs for AWS Auto Scaling services, enabling infrastructure-as-code deployment of Auto Scaling Groups, lifecycle hooks, and scaling policies. This specific package is part of the AWS CDK v1 ecosystem. As of version 1.204.0, it is actively maintained within the v1 branch but the primary development focus has shifted to AWS CDK v2, which consolidates all constructs into `aws-cdk-lib`. CDK releases are frequent, typically weekly or bi-weekly.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a basic AWS Auto Scaling Group using AWS CDK v1 constructs. It creates a new VPC, an Auto Scaling Group with a specified instance type and Amazon Linux 2 AMI, and sets min/max capacity. To deploy, run `cdk bootstrap` (if not already done) and then `cdk deploy`.

import os
from aws_cdk import (
    core as cdk,
    aws_ec2 as ec2,
    aws_autoscaling as autoscaling,
)

class MyAutoScalingStack(cdk.Stack):
    def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        # Create a VPC for the Auto Scaling Group
        vpc = ec2.Vpc(self, "MyVpc", max_azs=2) 

        # Define an Auto Scaling Group
        asg = autoscaling.AutoScalingGroup(
            self, "ASG",
            vpc=vpc,
            instance_type=ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.MICRO),
            machine_image=ec2.AmazonLinuxImage(generation=ec2.AmazonLinuxGeneration.AMAZON_LINUX_2),
            min_capacity=1,
            max_capacity=1,
            group_metrics=[autoscaling.GroupMetrics.all()], # Example for monitoring
            health_check=autoscaling.HealthCheck.ec2()
        )

app = cdk.App()
MyAutoScalingStack(app, "MyAutoScalingStack",
                     env=cdk.Environment(account=os.environ.get('CDK_DEFAULT_ACCOUNT', ''), 
                                         region=os.environ.get('CDK_DEFAULT_REGION', ''))
)
app.synth()

view raw JSON →