AWS CDK Auto Scaling Constructs
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
-
ModuleNotFoundError: No module named 'aws_cdk.aws_autoscaling'
cause Attempting to import `aws_cdk.aws_autoscaling` but the `aws-cdk-aws-autoscaling` package is not installed, or you are using CDK v2 style imports (`from aws_cdk import aws_autoscaling`) without `aws-cdk-lib` installed.fixIf using CDK v1, run `pip install aws-cdk-aws-autoscaling`. If using CDK v2, ensure `pip install aws-cdk-lib` and update your imports to `from aws_cdk import aws_autoscaling`. -
AttributeError: module 'aws_cdk.aws_autoscaling' has no attribute 'AutoScalingGroup'
cause You likely have `aws-cdk-lib` (CDK v2) installed and are trying to access the AutoScalingGroup directly as `aws_cdk.aws_autoscaling.AutoScalingGroup`. In v2, `aws_autoscaling` is a module within the main `aws_cdk` namespace.fixWith CDK v2 (`aws-cdk-lib`), import `aws_autoscaling` directly: `from aws_cdk import aws_autoscaling` and then use `aws_autoscaling.AutoScalingGroup(...)`. -
TypeError: 'NoneType' object is not callable
cause A required property for a construct (e.g., `vpc` for `AutoScalingGroup`) was not provided, or a variable assigned to a property evaluated to `None` when the construct expected a valid object.fixReview the construct's documentation and ensure all required properties are explicitly provided with valid, non-None values. For `AutoScalingGroup`, `vpc`, `instance_type`, and `machine_image` are essential.
Warnings
- breaking AWS CDK v2 consolidates all constructs into a single `aws-cdk-lib` package. This `aws-cdk-aws-autoscaling` package is specifically for CDK v1. If you are using CDK v2, you should install `aws-cdk-lib` and use the v2 import paths.
- gotcha CDK v1 requires installing individual construct libraries (e.g., `aws-cdk.core`, `aws-cdk.aws-ec2`, `aws-cdk-aws-autoscaling`) separately. Missing a dependency will lead to `ModuleNotFoundError`.
- gotcha Python version compatibility for CDK v1.x packages is typically limited to `~=3.7`. Newer Python versions might cause issues or not be supported by older CDK v1 dependencies.
Install
-
pip install aws-cdk-aws-autoscaling aws-cdk.core aws-cdk.aws-ec2 -
pip install aws-cdk-lib
Imports
- AutoScalingGroup
from aws_cdk import aws_autoscaling
from aws_cdk.aws_autoscaling import AutoScalingGroup
- CfnAutoScalingGroup
from aws_cdk.aws_autoscaling import CfnAutoScalingGroup
Quickstart
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()