This repository contains the steps corresponding to an in-depth tutorial available on our YouTube channel, Julien Muke.
If you prefer visual learning, this is the perfect resource for you. Follow my tutorial to learn how to build projects like these step-by-step in a beginner-friendly manner!
In this tutorial, we’ll guide you through the step-by-step process of setting up an Auto Scaling Group for your EC2 instances on AWS. Auto Scaling helps you maintain application availability by automatically adjusting the number of EC2 instances to meet demand, optimize costs, and ensure high performance.
We'll create a launch template that defines your EC2 instances and an Auto Scaling group with a single instance in it. After launching your Auto Scaling group, you'll terminate the instance and verify that the instance was removed from service and replaced. To maintain a constant number of instances, Amazon EC2 Auto Scaling detects and responds to Amazon EC2 health and reachability checks automatically.
The procedure for deploying this architecture on AWS consists of the following steps:
Step 1. Setting up VPC (Virtual Private Cloud)
Step 2. Configuring Internet Gateway
Step 3. Configuring Subnet
Step 4. Creating a Target Group
Step 5. Setting up an Application Load Balancer
Step 6. Create an Auto Scaling group using a launch template
Step 7. Testing our setup
To create a VPC, subnets, and other VPC resources using the console:
- Open the Amazon VPC console at https://console.aws.amazon.com/vpc/
- On the VPC dashboard, choose Create VPC.
- For Resources to create, choose VPC only.
- Enter the Name tag
test-vpc
- For IPv4 CIDR block, enter an IPv4 address range for the VPC
12.0.0.0/16
- Leave the rest as default, choose Create VPC.
To create an internet gateway:
- Open the Amazon VPC console at https://console.aws.amazon.com/vpc/.
- In the navigation pane, choose Internet gateways.
- Choose Create internet gateway.
- Enter a name for your internet gateway.
To use an internet gateway, you must attach it to a VPC
- Choose Actions, Attach to VPC.
- Select an available VPC.
- Choose Attach internet gateway.
To add a subnet to your VPC:
- Open the Amazon VPC console, in the navigation pane, choose Subnets, then choose Create subnet.
- Under VPC ID, choose the VPC for the subnet
test-vpc
Note: We are going to create 2 pubilc subnet in two Availability Zones: us-east-1a
and us-east-1b
- For Subnet name, enter a name for your subnet
test-public-subnet-1a
- Under Availability Zone, Choose the zone in which vour subnet will reside
US East (N. Virginia) / us-east-la
- For IPV4 subnet CIDR block, select Manual input to enter an IPV4 subnet CIDR block for your subnet
12.0.1.0/24
Note: To create another subnet, choose add new suhnet and repeat the same procedure mentioned above but for second Subnet name enter test-public-subnet-1b
, for Availability Zone choose US East (N. Virginia) / us-east-1b
, for IPv4 subnet CIDR block enter 12.0.3.0/24
. When you are done creating 2 subnets, Choose Create subnet.
Let's Determine the route table for a subnet
To determine the route table for a subnet:
- Open the Amazon VPC console, on the left hand side choose Choose the Route table tab.
- Choose Create route table
- Enter Route table name
rt-test-public
- Under VPC choose
test-vpc
Let's associate the route table into the subnet
- On the route table console, choose Subnet associations tab.
- Then choose Edit subnet associations.
- Select both subnets and choose Save associations.
Our route table has been associated with our subnet but this route table is also provide the internet access and that we for that we need to edit our route.
a. On route table section, under Tab choose Routes, then edit routes.
c. Under Edit route, choose add route
d. Enter IP address 0.0.0.0/0
which means any resources associated with route table can be accessed via internet.
e. For Target, choose Internet Gatway, then choose the internet Gateway which we have created previously igw-test
f. Choose Save changes
You register your targets with a target group. By default, the load balancer sends requests to registered targets using the port and protocol that you specified for the target group. You can override this port when you register each target with the target group.
To create a target group using the console:
- Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/
- On the navigation pane, under Load Balancing, choose Target Groups.
- Choose Create target group.
- For Choose a target type, select Instances to register targets by instance ID.
- For Target group name, type a name for the target group
tg-ec2-apache2
- For VPC, select a virtual private cloud (VPC). Note that for IP addresses target types, the VPCs available for selection are those that support the IP address type that you chose in the previous step
test-vpc
- Leave the rest as default, Choose Next.
- Under Register targets >> Available instances, there are no instances which has been created yet those instances will be created by Auto Scale policy.
- choose Create target group.
Now we have created the target group but there is no load balancer associated with this target group yet, we are just going to create that load balancer in the next step.
To create an Application Load Balancer, you must first provide basic configuration information for your load balancer, such as a name, scheme, and IP address type. Then, you provide information about your network, and one or more listeners. A listener is a process that checks for connection requests. It is configured with a protocol and a port for connections from clients to the load balancer.
To configure your load balancer and listener using the console:
- Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
- In the navigation pane, choose Load Balancers.
- Choose Create Load Balancer.
- Under Application Load Balancer, choose Create.
- For Load balancer name, enter a name for your load balancer
alb-ec2-instances-with-asg
- Under Network mapping:
a. For VPC, select the VPC that you used for your EC2 instancestest-vpc
b. For Mappings, enable zones for your load balancer by selecting subnets from two or more Availability Zonestest-public-subnet-1a
andtest-public-subnet-1b
- For Security groups, select an existing security group, or create a new one.
Note: The security group for your load balancer must allow it to communicate with registered targets on both the listener port and the health check port. The console can create a security group for your load balancer on your behalf with rules that allow this communication. You can also create a security group and select it instead.
- Choose create a new security group.
A security group acts as a virtual firewall for your instance to control inbound and outbound traffic. To create a new security group, complete the fields below.
- Enter security group name
alb-sg-for-http-request
- Select the existing VPC
test-vpc
- For Inbound rules, choose Add rule.
A rules with source of 0.0.0.0/0
or :/0 allow all IP addresses to access your instance. We recommend setting security group rules to allow access from known IP addresses only.
- Choose Create security group
- Back to the Application Load Balancer, under security groups, refresh and add the new one
alb-sg-for-http-request
- For Listeners and routing, the default listener accepts HTTP traffic on port
80
. You can keep the default protocol and port, or choose different ones. For Default action, choose the target group that you createdtg-ec2-apache2
- Review the load balancer configurations and make changes if needed. After you finish reviewing the configurations, choose Create load balancer.
When you create an Auto Scaling group, you must specify the necessary information to configure the Amazon EC2 instances, the Availability Zones and VPC subnets for the instances, the desired capacity, and the minimum and maximum capacity limits.
To configure Amazon EC2 instances that are launched by your Auto Scaling group, you can specify a launch template or a launch configuration. The following procedure demonstrates how to create an Auto Scaling group using a launch template.
To create an Auto Scaling group using a launch template (console):
- Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/, and choose Auto Scaling Groups from the navigation pane.
- For Auto Scaling group name, enter a name for your Auto Scaling group
asg-ec2-instances-test-demo
- For Launch template, choose create a Launch template
- Choose Create launch template. Enter a name
it-ec2-instances-apache2
- Under Launch template contents, fill out each required field and any optional fields as needed:
a. Choose the Application and OS Images (Amazon Machine Image) Ubuntu Server
b. For Instance type, choose a single instance type that's compatible with the AMI that you specified t2.micro
c. Key pair (login): For Key pair name, choose an existing key pair, or choose Create new key pair to create a new one.
For more information, see Amazon EC2 key pairs and Linux instances in the Amazon EC2 User Guide.
d. For Subnet choose Don't include in launch template
For security groups let's create a new one:
By default, new security groups start with only an outbound rule that allows all traffic to leave the resource. You must add rules to enable any inbound traffic or to restrict the outbound traffic.
To create a security group using the console:
- Open the Amazon VPC console at https://console.aws.amazon.com/vpc/
- In the navigation pane, choose Security groups.
- Choose Create security group.
- Enter a name
it-sg-ec2-instances-apache2
and description for the security group. You cannot change the name and description of a security group after it is created. - From VPC, choose a VPC. The security group can be used only in the VPC for which it is created
test-vpc
- Under Inbound rules, choose add rule, then add HTTP and SSH with source of
0.0.0.0/0
to allow all IP addresses to access your instance. - Choose Create security group.
e. Back to the Launch template, from VPC, choose the VPC refresh and select the VPC we jsut created it-sg-ec2-instances-apachez
. The security group can be used only in the VPC for which it is created.
f. Enanble Auto-assign public IP
g. For User data, we need to install the Apache and we need to have some custom HTML page so that it can show the host IP, copy and paste in field the user data script below:
#!/bin/bash
yes | sudo apt update
yes | sudo apt install apache2
echo "<h1>Server Details</h1><p><strong>Hostname:</strong> $(hostname)</p><p><strong>IP Address:</strong> $(hostname -I | cut -d" " -f1)</p>" > /var/www/html/index.html
sudo systemctl restart apache2
h. Choose create Launch template
When you done creating the Launch template, let's go back to the Auto Scaling Group and finish the configuration.
- On the Choose launch template or configuration page, for Launch template, refresh and choose an existing launch template that we just created
it-ec2-instances-apache2
then choose Next.
- Under Network, choose the VPC
test-vpc
- For Availability Zones and subnets, add both
test-public-subnet-1a
andtest-public-subnet-1b
then choose Next
- On the Configure advanced options page, under Load balancing, choose Attach to an existing load balancer
- For Attach to an existing load balancer choose
tg-ec2-apache2| HTTP
- Under Health checks, enable Turn on Elastic Load Balancing health checks.
- For Health check grace period, for this demo enter
20
seconds then click on Next
- For Configure group size and scaling, enter Desired capacity to
2
- For Scaling, enter Min desired capacity to
1
and Max desired capacity to3
- For Automatic scaling for this demo choose No scaling policies
- For notification choose Next, because will not add any notification
- Tags are also optional choose Next
- Review all the configuration and choose Create Auto Scaling Group
Note: As soon as you create Autoscaling Group it will automatically start creating the EC2 instances based on the desired capacity which we have specified, in our case it will automatically provision 2 EC2 instances for us.
Once our EC2 instances has been initialized properly, next let's test our load balancer.
- Back to the Load Balancer console, click on
alb-ec2-instances-with-asg
- Copy the DNS name and paste it on a browser
- As you can see below we are able to access our EC2 instances.
- 2 EC2 Instances with:
* First IP Address:12.0.1.239
and if we refresh it will change to;
* Second IP Address:12.0.3.109
- If we go back to the EC2 instance console and verify the IP Addresses we will see the same IP addresses
* First IP Address:12.0.1.239
* Second IP Address: 12.0.3.109
Now the Load Balancer is routing the request to the EC2 instances.
Lastly, we are going to manually delete one EC2 instance, what is going to happen is that the Auto Scaling Group is going to automatically provision another EC2 instance for us so that we get the maximum availability.
Before we do that, as you can see below, for alb-ec2-instances-with-asg
auto scaling target group, under instance management both our EC2 instances are in healthy status right now.
As you can see below, If you terminate on EC2 instance, the auto scaling will automatically provision another one because we specified the Desired capacity to 2
.
All services used are eligible for the AWS Free Tier. However, charges will incur at some point so it's recommended that you shut down resources after completing this tutorial.