Setting up an Autoscaling Group with the AWS CLI.

Matthew Mendez
DevOps Engineer Documentation
5 min readMay 27, 2021

1. What is the AWS CLI?

2. Setting up.

3. Create an EC2 Instance from the AWS CLI.

4. Create an Auto Scaling Group from the EC2 Instance.

5. Clean up.

1. What is the AWS CLI?

The AWS Command Line Interface (AWS CLI) is an Amazon Web Services tool that enables developers to control Amazon public cloud services by typing commands on a specified line. AWS’ version of a command-line interface is one of several methods a developer can use to create and manage AWS tools. Other methods include the AWS Management Console and AWS application programming interfaces.

2. Setting up.

We will first have to sign into the AWS Console and navigate to IAM to create a user and give them programmatic and console access. For AWS best practices and steps on how to create a user, I have pasted the link below.

After creating the user, make sure to download the Access Key and Secret Access key in a safe place as you will use this later.

Next we will need to download the AWS CLI. For AWS CLI installation instructions, please click the link below.

After installing the AWS CLI, we can now quickly configure basic settings that the AWS CLI uses to interact with AWS. For AWS best practices please click the link below. I have also posted a screen shot of my terminal for demonstration.

3. Create an EC2 Instance from the AWS CLI.

We can now get started on creating an EC2 instance from the AWS CLI. There are a few things we will need to do in the AWS Console first.

Navigate to EC2. On the left hand side, scroll down and click on key pairs. Click create key pair.

Name your key pair.

Click create key pair.

Next we will need to create a security group.

In the EC2 console. Scroll down and click security groups.

Click create security group.

We need to name our security group and edit some inbound rules. Below for this demonstration, I named my security group my-cli-ec2-sg and gave my security group inbound rules to allow ssh access on port 22 and http on port 80.

Click create security group.

We can now launch our EC2 instance via the AWS CLI.

From the command line we can now run the command below to launch our instance. You will need your key pair and your security group that you created in the console. I took a screen shot of terminal below for demonstration purposes.

aws ec2 run-instances — image-id ami-173d747e --count 1 --instance-type t1.micro --key-name MyKeyPair --security-groups my-sg

If you are having trouble finding the ami id. You can navigate to the EC2 console and click on launch instance. Here you can find the ami id type as seen below.

After running the command, you should see your instance starting up via your terminal as seen below.

You can also verify a successful launch through the AWS Console.

verifying through the aws console

4. Create an Auto Scaling Group from the EC2 Instance.

Now that we have created an EC2 instance through the AWS CLI and have verified it. We can now use our same instance that we created to also launch an AWS Auto Scaling Group.

To do this, lets grab the Instance ID from the instance we created.

Next lets go back to the terminal and with your instance id, run this command.

aws autoscaling create-auto-scaling-group — auto-scaling-group-name my-asg-from-instance \ --instance-id i-0e69cc3f05f825f4f --min-size 1 --max-size 2 --desired-capacity 2

For my demonstration, I set the min size to 2, the maz size to 5 and desired capacity to 2.

We can go back to the AWS EC2 Console and verify the command worked.

Verifying the Auto Scaling Group created from the AWS CLI
you can see in the activity history that two more instances were created
and just for fun we can go to ec2 console and see our instances launched from the AWS Cli

5. Clean up.

Now that we completed our task, we can now clean up everything by deleting our Auto Scaling Group. For this we can navigate to the AWS EC2 Console and click on Auto Scaling Groups and click delete.

Or you can run the command in the terminal.

aws autoscaling delete-auto-scaling-group — auto-scaling-group-name my-asg

Please double check your environment that everything has been cleaned up.

For more resources on how to create an Auto Scaling Group please check out the link below.

For more information on EC2 Instances please see the link below.

--

--

Matthew Mendez
DevOps Engineer Documentation

Documenting my journey from bartender to a career as a devops engineer