Creating An Auto Scaling Group with AWS CLI

Deyonte Turner
4 min readApr 4, 2021

I will be using AWS CLI to create an instance and then I will use the instance id from that instance to create an auto scaling group. The AWS CLI is a very powerful tool that allows us to use a terminal in our local environment and manage AWS services. Although I am still new to the AWS CLI I have already recognized it’s effectiveness and it is definitely growing on me. There will be a few prerequisites for anybody that will be following along.

Prerequisites needed to follow along :

In order to follow along with this project you will need an AWS account and the you will also need to download the AWS CLI to your terminal. Here is a link https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html. The following link I provided is to the AWS CLI command reference that I will be using to find any commands that will be needed to complete this project https://awscli.amazonaws.com/v2/documentation/api/latest/index.html. You will also need to have security groups and subnets already configured if you do not wish to use the defaults.

Time To Dive In!!!

First, I will be creating a user and a key for that user to have programmatic access to my account. We can create the user with the following command once you have downloaded the AWS CLI(replace your desired user name in place of the field <YOUR_USER_NAME>, you will see similar references like this and each time you will need to replace it with your own information into the code):

aws iam create-user –-user-name <YOUR_USER_NAME>

Then you can use the following command to create the access key for programmatic access:

aws iam create-access-key

(Be sure that you don’t already have keys created or you will be presented with the following):

Once we have set the user with programmatic access, it is time to create our instance and we can do that with this command:

aws ec2 run-instances --image-id <YOUR_AMI> --count 1 --instance-type t3.nano --key-name <YOUR_KEY> --security-group-ids <YOUR_SG_ID> --subnet-id <YOUR_SUBNET_ID>

This command will create one t3.nano instance with the security group and subnet I assign it. I went ahead and copied an ami-id I wanted to use, as well as the subnet and security group-ids from the console that I needed for this command. This made it easier for me when I needed to input that information into the command . Be sure to keep any sensitive information stored securely where you can access it as you need to.

You will presented with a screen similar to the one above once we have successfully created our instance.

After we have created the instance we can now create our auto scaling group using the instance id from the instance we just created. We can do this using the following command:

aws autoscaling create-auto-scaling-group --auto-scaling-group-name <YOUR_ASG_NAME> --instance-id <YOUR_INSTANCE_ID> --min-size 2 --max-size 5 --desired-capacity 2

The command I used will create the auto scaling group with a min. size of 2 and max of 5 instances. As you can see the desired capacity is set at 2, and this is the goal the auto scaling group will aim to achieve. We can now check our instances to make sure that the auto scaling group is working correctly with the following command :

aws ec2 describe-instances

This command will list our instances for us.

Instance #1
Instance #2

It may be difficult to read thru all of the information in the two examples above but I have highlighted that both instance types are t3.nano. The instance-ids above the highlighted portion show you these are two different instances.

Time To Clean up!!!

When I do projects like this it is always best to clean up right after I have finished documenting. This will help to ensure that I will not occur any unnecessary charges in case I become busy with any other tasks and forget to do so later. We can do this with the following commands :

aws autoscaling delete-auto-scaling-group --auto-scaling-group-name <YOUR_ASG_NAME> --force-deleteaws ec2 terminate-instances --instance-id <YOUR_INSTANCE_ID>

The first command above will be deleting the auto scaling group and the second will delete the instances. For both commands as mentioned before, you must enter your own information for the portion of the command that is inside the greater/less than signs(<YOUR_ASG_NAME>, <YOUR_INSTANCE_ID>). It is important that you delete the auto scaling group first or you will simply have the auto scaling group replacing the instances you terminate as it is designed to do. Remember ANY time that our instances go down the auto scaling group will replace that instance to achieve the desired performance. This was a simple project but we can see the power of using the AWS CLI and I must admit I am starting to prefer it over the console. Any feedback is welcomed and appreciated as always.

--

--

Deyonte Turner

I am a Junior DevOps Engineer, I have recently enrolled in a program with Level Up In Tech. I have earned my Linux Essentials LPI and AWS CCP certifications.