Setting Up an Auto Scaling Group with AWS CLI

Helen Campbell
4 min readMay 7, 2021

Like my last project, we are setting up an Auto Scaling group, but this time through our terminal. An AWS account is needed for this project, along with the AWS CLI downloaded to your terminal. This was performed on a macOS, so the download process may slightly differ on other operating systems.

For all other OS environments, follow this link to download AWS CLI correctly to your environment: https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html

Download AWS CLI to your terminal

curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
sudo installer -pkg AWSCLIV2.pkg -target /

Run the code below to verify that the AWS CLI can be found.

which aws
aws --version

These steps will allow you to locate where the AWS CLI is on your computer and which version you have downloaded.

Create a user and give them programmatic and console access

aws iam create-user --user-name <NEW_USER-NAME>

You will be returned code in JSON format indicating your user name, user ID, ARN (Amazon Resource Name), and the user's creation date. Note: you will see your AWS account ID in the ARN output information.

I already have an Admin group created in my AWS account; therefore giving programmatic and console access to my new user is simply writing code and adding them to my Admin group.

aws iam add-user-to-group --user-name <USER-NAME> --group-name <GROUP-NAME>

The code below will verify that your new user was successfully placed in your group.

aws iam get-group --group-name <GROUP-NAME> 

Download the Access Key and Secret Access Key in a safe place

aws iam create-access-key --user-name <USER-NAME>

Your information is returned in JSON format. It consists of your user name, access key ID, status, secret access key, and the creation date. Saving your access and secret access key is highly important.

vim ~/.aws/credentials

The above code opens a file for you to input the new user’s access key and secret access key. Copy and paste the two keys into this vim file from the returned information in the JSON format above. You will see that the default user is also there. Knowing how to do this is essential when handling multiple credentials in further projects you may be given.

Create an EC2 Instance (t3.nano, Amazon 2 AMI)

In this step, the default settings are used for the security group and subnet ID that may have previously been set up in our AWS account. We are asked to use Amazon 2 AMI in this project, which is pasted below in the code, along with our instance type.

aws ec2 run-instances --image-id ami-0742b4e673072066f --count 1 --instance-type t3.nano --key-name <EXISTING_KEY_NAME> --security-group-ids sg--<YOUR_SG_ID> subnet-id subnet-<DEFAULT_SUBNET_ID>

List the Instance to ensure creation

aws ec2 describe-instances --filters "Name=instance-type,Values=t3.nano" --query "Reservations[].Instances[].InstanceId"

The output above in our terminal proves that our Instance has been successfully created, and we can go into the AWS Console to double-check that success.

Create an Auto Scaling group with a minimum of 2 and a maximum of 5

aws autoscaling create-auto-scaling-group --auto-scaling-group-name <ASG_NAME> --launch-template LaunchTemplateId=<LT_ID> --min-size 2 --max-size 5 --vpc-zone-identifier "<SUBNET>, <SUBNET>, <SUBNET>"

The code above will create instances you can check in your console, as seen below. In sticking with the CLI, the code below will provide you with your Auto Scaling group with the given name.

aws autoscaling describe-auto-scaling-groups
Double-checking the Auto Scaling group on the Console.

Clean up environment

Now that we have successfully completed our task, it is time to undo everything by following these two steps:

  1. Delete launch template
aws ec2 delete-launch-template --launch-template-id <lt-0abcd290751193123>

2. Delete group while terminating any unwanted EC2 Instances

aws autoscaling terminate-instance-in-auto-scaling-group --instance-id <i-056a54b876bb83220> --no-should-decrement-desired-capacity

Again, double checking that your environment has been cleaned up never hurts, and may help you avoid unnecessary costs garnered while attempting this project.

--

--

Helen Campbell

building, breaking, and blogging — all things cloud