Create an Auto Scaling Group using the AWS Command Line Interface

Create IAM User

User will require programmatic and console access.

  • Access type — Select both Programmatic access and AWS Management Console access
  • Console password — Select custom password
  • Require password reset — Uncheck User must create a new password at next sign-in

Create User Groups

1. Navigate to User Groups, we will create a group to grant our new user permissions.

  • Add User to the Group — Select User Name created to grant permissions
  • Attach Permission Policies — Select AmazonEC2FullAccess

Install AWS CLI

The following steps are what you will use if installing on MacOS Terminal, but you could find direction here if you wish to use other options or platforms.

curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"sudo installer -pkg AWSCLIV2.pkg -target /
which awsaws --version
aws configure
  • AWS Secret Access Key [None]: Enter Secret access key
  • Default region name [None]: us-east-1
  • Default output format [None]: Leave blank

Create Security Group

Security Group will be created for our instance that will allow HTTP from 0.0.0.0/0 in order to access our site:

aws ec2 create-security-group --group-name SG-WebServer --description "WebServer Secuirty Group"
aws ec2 authorize-security-group-ingress --group-id sg-0150ceaa8eac75827 --protocol tcp --port 80 --cidr  0.0.0.0/0

Create EC2 Instance

1. Create a file titled UserData.txt containing the following:

#!/bin/bash
yum update -y
yum install -y httpd
echo '<h1>This is our test web server</h1>' >
/var/www/html/index.html
systemctl start httpd
systemctl enable httpd
aws ec2 run-instances --image-id ami-0d5eff06f840b45e9 --count 1 --instance-type t2.micro --tag-specifications 'ResourceType=instance, Tags=[{ Key=Name,Value=MyInstance}]' --security-group-ids sg-0150ceaa8eac75827 --user-data file://UserData.txtNote: You will need to go into console as if you were creating a new instance to get AMI information and for the security group provide the information for GroupId you created.
aws ec2 describe-instances --filters "Name=tag:Name,Values=MyInstance" --query "Reservations[].Instances[].InstanceId"

Create Auto Scaling Group

Now that we have created EC2 instance and verified it we will create Auto Scaling Group using the same instance. You will need the Instance ID from the instance created.

aws autoscaling create-auto-scaling-group --auto-scaling-group-name WebServerASG-from-instance \
--instance-id i-0703f458446de3150 --min-size 2 --max-size 5 --desired-capacity 2
aws ec2 describe-instances --filters "Name=tag:aws:autoscaling:groupName,Values=WebServerASG-from-instance" --query "Reservations[].Instances[].InstanceId"

Clean Up

We may now delete everything we created since we completed our task and no longer needed. This could be done from the Console or Terminal.

aws ec2 terminate-instances --instance-ids i-0703f458446de3150
aws autoscaling delete-auto-scaling-group --auto-scaling-group-name WebServerASG-from-instance --force-delete
aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" --query "Reservations[].Instances[].InstanceId"
aws ec2 delete-security-group --group-name SG-WebServer

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store