Bootstrap FreeKB - Amazon Web Services (AWS) - Create Auto Scaling Group using the AWS CLI
Amazon Web Services (AWS) - Create Auto Scaling Group using the AWS CLI


This assumes you have already configured the aws command line tool. If not, check out my article on Getting Started with the AWS CLI.

An Auto Scaling Group let's you automatically start and stop EC2 instances based on some condition, such as creating and starting up EC2 instances when requests load is high or stopping and terminating EC2 instances when requests load is low. This is often used in conjunction with CloudWatch Alarms, where the Alarm checks some condition such as requests load or CPU Utilization and then invokes the Auto Scaling Group to create/startup EC2 instances or to stop/terminate EC2 instances. For example, you may have an Auto Scaling Group that has minimum 1 and maximum 3, meaning that the Auto Scaling Group will have 1 EC2 instance when load is low and scale up to 3 EC2 instances when load is high.

An Auto Scaling Group often uses an EC2 Launch Template. An EC2 Launch Template is a way to set certain defaults for launching a new EC2 instance, such as the SSH Key Pairs, Network Subnets, Security Groups, Amazon Machine Image, et cetera that the EC2 Instance will have. Check out my articles Create a Launch Template using the AWS CLI and List Launch Templates using the AWS CLI. Let's say you have an EC2 Launch Template with ID lt-0706d84b6a79fd1d6.

~]$ aws ec2 describe-launch-templates
{
    "LaunchTemplates": [
        {
            "LaunchTemplateId": "lt-0706d84b6a79fd1d6",
            "LaunchTemplateName": "my-launch-template",
            "CreateTime": "2024-02-07T02:45:54+00:00",
            "CreatedBy": "arn:aws:iam::123456789012:user/johndoe",
            "DefaultVersionNumber": 1,
            "LatestVersionNumber": 1
        }
    ]
}

 

Additionally, the EC2 Auto Scaling Group will be in one or more subnets in one of your Virtual Private Clouds (VPCs). Check out my articles Create Subnet using the AWS CLI and List Subnets using the AWS CLI.

~]$ aws ec2 describe-subnets --filter "Name=vpc-id,Values=vpc-0a9d4cb29e2748444" | egrep -i 'SubnetId|CidrBlock'
            "CidrBlock": "10.0.0.16/28",
            "SubnetId": "subnet-0f015da3a1e164304",
            "Ipv6CidrBlockAssociationSet": [],
            "CidrBlock": "10.31.0.0/20",
            "SubnetId": "subnet-0d2d8580c46d6d280",
            "Ipv6CidrBlockAssociationSet": [],
            "CidrBlock": "10.0.0.0/28",
            "SubnetId": "subnet-02b9845e7366bdf89",
            "Ipv6CidrBlockAssociationSet": [],
            "CidrBlock": "10.0.0.32/28",
            "SubnetId": "subnet-075d4be5a8a07c818",
            "Ipv6CidrBlockAssociationSet": [],

 

The aws autoscaling create-auto-scaling-group command can be used to create an EC2 Auto Scaling Group. In this example, the EC2 Auto Scaling Group uses Launch Template lt-0706d84b6a79fd1d6 and a few of the subnets.

aws autoscaling create-auto-scaling-group \
    --auto-scaling-group-name my-auto-scaling-group \
    --launch-template LaunchTemplateId=lt-0706d84b6a79fd1d6 \
    --min-size 1 \
    --max-size 3 \
    --vpc-zone-identifier "subnet-02b9845e7366bdf89,subnet-0f015da3a1e164304,subnet-02b9845e7366bdf89"

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 3a0138 in the box below so that we can be sure you are a human.