How to Create an Autoscaling Group from the CLI

Marshall Hubbard
Nerd For Tech
7 min readApr 1, 2021

--

Thanks for joining me! The purpose of this lab is to know different ways to configure your environment instead of relying solely on the Console. This project will document how to create an Autoscaling Group from the Command Line Interface (CLI) but will also teach you how to create other resources such as Instances, Security Groups among others all from the CLI. To complete this lab, we need to already have the following:

  1. A VPC (Virtual Private Cloud) already configured through AWS Console;
  2. Already have a Linux OS, PuTTy downloaded to access Linux on your Windows computer or access to Terminal on your Mac;
  3. An IAM role already created with full Admin programmatic and console access (I will briefly walk you through this step);
  4. The AWS CLI downloaded to your Terminal; and
  5. Access to the AWS CLI documentation.

Before we get this party started, let’s briefly go over what the AWS CLI is. The AWS CLI is a tool that assists you in managing AWS services outside of the Management Console and the APIs. The AWS CLI has made it possible for users to access AWS services remotely through Linux, MacOS and Windows. With a single step installation and minimal configuration, you can remotely (through SSH or PuTTy) use all of the capabilities offered by the AWS Management Console using Linux Shells and Windows Command Line. This is a HUGE deal for Developers or users with a lot of data in their AWS account. Instead of constantly having to search through multiple services for your data, with the right command, the entire task that you are looking to accomplish will only take a few seconds using the AWS CLI.

Few words of advice before we begin:

  • I already had the AWS CLI downloaded to my Terminal, but if you don’t, I would recommend installing Homebrew to your Terminal using this link, https://brew.sh/. Once Homebrew is downloaded, you can “brew install” a variety of different resources to your Terminal including the AWS CLI. Like my mentor would say, “Homebrew is a MUST."
  • Also, as you move through this lab and in your future AWS CLI endeavors, I HIGHLY encourage you to open a Notepad app on your computer and save your VPC Id, Subnet ID, Instance Id and all other information so that it is easier to copy and paste versus constantly having to look for it. I also use it to label my steps to a specific endpoint so that I don’t skip anything important. Here is an example of what I typically do for my Notepad app to jot down relevant information:

Step 1: Create your IAM role

1a) For this step, go to the search bar at the top of the AWS Console and search for IAM. Then click on “Users” link and follow that with “Add User” which should bring you to this page.

1b) Name your user and then give the user Programmatic and Console access and also give them a custom password. I unchecked the password reset option due to creating this role for the sole purpose of completing this lab. However, it is best practice to check this option so that the user can create their own personal password upon logging in. Once completed, check the permissions and move through the next following steps.

Step 2: Create the resources in your environment

2a) Once the AWS CLI is installed on your Terminal, use the following command listed below to ensure that your current VPC is listed. If it is, we can move on to the following commands to create your environment.

aws ec2 describe-vpcs

2b) Use the following command listed below to create the security group for the VPC.

aws ec2 create-security-group — group-name <name> — description “<description>” — vpc-id <VPC Id>

2c) Once the Security Group ID is listed, save it to your notepad. I also ran this command listed below to open ports 22 and 80 on the security group.

aws ec2 authorize-security-group-ingress \
— group-name MySecurityGroup \
— protocol tcp \
— port <port that you want to open>\
— cidr <your IP>

*If you require assistance finding your IP address, Amazon has a website that you can use “http://checkip.amazonaws.com/”.

2d) The next step that “I” did was use the command listed below and once my subnets were listed, I saved them to my Notepad. This step is optional but I highly recommend it just to have all of your resources identifiers on the Notepad versus constantly having to look them up again for a command.

aws ec2 describe-subnets

2e) I then used the following command listed below to create my Instance and force it to run. This is where it is important to have that previous information saved because now you have to copy and paste it into this command. For the image ID, I grabbed the AMI2 free tier ID from the console and placed it into this command.

aws ec2 run-instances — image-id <imageid> — count 1 — instance-type t2.micro — key-name <key> — security-group-ids (<securitygroup>) — subnet-id <subnet>

2f) I then used the following command listed below to verify that my instance was launched and double checked that with the Instance ID in the AWS console. Once this step is done, we are able to move on.

aws ec2 describe-instances — query “Reservations[].Instances[].InstanceId”

Step 3: Create Your Launch Template

3a) We are creating our Launch Template during this step. Run the command listed below to launch it using the information from your Notepad. Also, create a unique template name due to this being required for the next step. Once this step is complete, and you don’t get an error message like I did a couple times in my screenshot, lol, move on to the next step.

aws ec2 create-launch-template \
— launch-template-name <Name> \
— version-description <Description> \
— launch-template-data ‘{“NetworkInterfaces”:[{“AssociatePublicIpAddress”:true,”DeviceIndex”:0,”Ipv6AddressCount”:1,”SubnetId”:”subnet-7b16de0c”}],”ImageId”:”<image AMI>”,”InstanceType”:”t2.small”,”TagSpecifications”:[{“ResourceType”:”instance”,”Tags”:[{“Key”:”purpose”,”Value”:”webserver”}]}]}’

Step 4: Create your Auto Scaling Group

4a) Alright, don’t mind the error messages on this step, lol. Run the command listed below and make sure to use the template name that you just created. Fill in the brackets using the information that you already saved to your Notepad. Once you get through this step with no errors, move on to the next step.

aws autoscaling create-auto-scaling-group — auto-scaling-group-name <name> — launch-template “LaunchTemplateName=<launch template name>” — min-size <size> — max-size <size> — vpc-zone-identifier “subnet-<subnet>

Step 5: Check your work

5a) After running all of the commands through the CLI, I then went to the console to verify that everything was successfully configured. As you can see, my Autoscaling Group was created with my desired capacity.

Step 6: Clean up your environment

6a) The last thing we are going to do is tear down our environment to avoid any further charges. Use the following command to tear down your ASG:

aws autoscaling delete-auto-scaling-group — auto-scaling-group-name <name>

6b) As you can tell from this screenshot, this next command took some time to figure out, lol. Run the command listed below and wait for it to be stopped before moving.

aws ec2 stop-instances — instance-ids <instanceid>

6c) Once the Instance is stopped, run the command to terminate your Instance.

aws ec2 terminate-instances — instance-ids <instance ID>

6d) The last step to tearing down the environment that we created will be to run this command listed below to fully terminate your Instance. Once this step is complete, your environment will be as clear as mine is outside of the actual VPC itself. I didn’t delete my VPC due to using the same VPC for another project. However, if you want to delete yours, you can check the AWS CLI documentation to delete your resources, Internet Gateway, Security Groups, Route Tables and then the VPC itself. Each command can be found in it’s separate AWS CLI documentation page.

aws ec2 terminate-instances — instance-ids <instanceid>

This concludes my lab. Thanks for reading!

--

--

Marshall Hubbard
Nerd For Tech

LE Professional transitioning to DevOps. Currently learning at Level Up In Tech.