Skip to content

Auto Scaling

Auto Scaling helps you maintain application availability and allows you to automatically add or remove EC2 instances according to the demand. You can use Auto Scaling to ensure that you are running your desired number of instances.

LocalStack allows you to use the Auto Scaling APIs locally to create and manage Auto Scaling groups locally. The supported APIs are available on our API Coverage section, which provides information on the extent of Auto Scaling’s integration with LocalStack.

This guide is designed for users new to Auto Scaling and assumes basic knowledge of the AWS CLI and our awslocal wrapper script.

Start your LocalStack container using your preferred method. We will demonstrate how you can create a launch template, an Auto Scaling group, and attach an instance to the Auto Scaling group using the AWS CLI.

You can create a launch template that defines the launch configuration for the instances in the Auto Scaling group using the CreateLaunchTemplate API. Run the following command to create a launch template:

Terminal window
awslocal ec2 create-launch-template \
--launch-template-name my-template-for-auto-scaling \
--version-description version1 \
--launch-template-data '{"ImageId":"ami-ff0fea8310f3","InstanceType":"t2.micro"}'
Output
{
"LaunchTemplate": {
"LaunchTemplateId": "lt-5ccdf1a84f178ba44",
"LaunchTemplateName": "my-template-for-auto-scaling",
"CreateTime": "2024-07-12T07:59:08+00:00",
"CreatedBy": "arn:aws:iam::000000000000:root",
"DefaultVersionNumber": 1,
"LatestVersionNumber": 1,
"Tags": []
}
}

Before creating an Auto Scaling group, you need to fetch the subnet ID. Run the following command to describe the subnets:

Terminal window
awslocal ec2 describe-subnets --output text --query Subnets[0].SubnetId

Copy the subnet ID from the output and use it to create the Auto Scaling group. Run the following command to create an Auto Scaling group using the CreateAutoScalingGroup API:

Terminal window
awslocal autoscaling create-auto-scaling-group \
--auto-scaling-group-name my-asg \
--launch-template LaunchTemplateId=lt-5ccdf1a84f178ba44 \
--min-size 1 \
--max-size 5 \
--vpc-zone-identifier 'subnet-d4d16268'

You can describe the Auto Scaling group using the DescribeAutoScalingGroups API. Run the following command to describe the Auto Scaling group:

Terminal window
awslocal autoscaling describe-auto-scaling-groups
Output
{
"AutoScalingGroups": [
{
"AutoScalingGroupName": "my-asg",
"AutoScalingGroupARN": "arn:aws:autoscaling:us-east-1:000000000000:autoScalingGroup:74b4ffac-4588-4a7c-86b1-9fa992f49c8e:autoScalingGroupName/my-asg",
"LaunchTemplate": {
"LaunchTemplateId": "lt-5ccdf1a84f178ba44",
"LaunchTemplateName": "my-template-for-auto-scaling"
},
"MinSize": 1,
"MaxSize": 5,
...
"Instances": [
{
"InstanceId": "i-fc01551d496fc363f",
"InstanceType": "t2.micro",
"AvailabilityZone": "us-east-1a",
...
}
],
...
"TerminationPolicies": [
"Default"
],
...
"CapacityRebalance": false
}
]
}

Attach an instance to the Auto Scaling group

Section titled “Attach an instance to the Auto Scaling group”

You can attach an instance to the Auto Scaling group using the AttachInstances API.

Before that, create an EC2 instance using the RunInstances API. Run the following command to create an EC2 instance locally:

Terminal window
awslocal ec2 run-instances \
--image-id ami-ff0fea8310f3 --count 1

Fetch the instance ID from the output and use it to attach the instance to the Auto Scaling group. Run the following command to attach the instance to the Auto Scaling group:

Terminal window
awslocal autoscaling attach-instances \
--instance-ids i-0d678c4ecf6018dde \
--auto-scaling-group-name my-asg

Replace i-0d678c4ecf6018dde with the instance ID that you fetched from the output.

LocalStack does not support the docker/libvirt VM manager for EC2. It only works with the mock VM manager.

OperationImplementedImage
Page 1 of 0