Step 1: Create an Amazon Machine Image (AMI)

To use AWS Auto Scaling, you need to create an Amazon Machine Image (AMI) that includes your application and configuration settings. If you already have an AMI, you can skip this step.

To create an AMI using the AWS CLI, use the following command:

aws ec2 create-image --instance-id <instance-id> --name "my-ami" --description "This is my AMI"

Replace <instance-id> with the ID of the instance you want to create an AMI of.

Step 2: Create a Launch Configuration

Next, you need to create a Launch Configuration that specifies the AMI, instance type, and other configuration settings for the instances that will be launched by the Auto Scaling group.

To create a Launch Configuration using the AWS CLI, use the following command:

aws autoscaling create-launch-configuration --launch-configuration-name "my-launch-config" --image-id <image-id> --instance-type t2.micro --security-groups <security-group-id> --key-name <key-pair-name>

Replace <image-id> with the ID of the AMI you created in Step 1. Replace <security-group-id> with the ID of the security group that you want to associate with the instances. Replace <key-pair-name> with the name of the key pair that you want to use to connect to the instances.

Step 3: Create an Auto Scaling Group

Now you can create an Auto Scaling group that will automatically launch and terminate instances based on the rules you define.

To create an Auto Scaling group using the AWS CLI, use the following command:

aws autoscaling create-auto-scaling-group --auto-scaling-group-name "my-auto-scaling-group" --launch-configuration-name "my-launch-config" --min-size 1 --max-size 3 --desired-capacity 2 --availability-zones us-east-1a us-east-1b --vpc-zone-identifier <subnet-id-1>,<subnet-id-2>

Replace <subnet-id-1> and <subnet-id-2> with the IDs of the subnets you want to use. You can specify multiple subnets separated by commas.

Step 4: Set up Scaling Policies

Finally, you need to set up scaling policies that define how the Auto Scaling group should respond to changes in demand.

To create a scaling policy using the AWS CLI, use the following command:

aws autoscaling put-scaling-policy --policy-name "my-scaling-policy" --auto-scaling-group-name "my-auto-scaling-group" --policy-type SimpleScaling --adjustment-type ChangeInCapacity --scaling-adjustment 1

This command creates a scaling policy that adds one instance to the Auto Scaling group whenever the capacity needs to be increased.

You can also create more complex scaling policies using other policy types and adjustment types.

And that’s it! You’ve now set up AWS Auto Scaling using the AWS CLI.