Skip to content

Amazon EC2 Auto Scaling helps you ensure that you have the correct number of Amazon EC2 instances available to handle the load for your application.

Notifications You must be signed in to change notification settings

julien-muke/aws-ec2-auto-scaling-group

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 

Repository files navigation

aws How to Create an Auto Scaling Group of EC2 Instances.

This tutorial provides a hands-on introduction to Amazon EC2 Auto Scaling through the AWS Management Console. You'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.

AWS EC2 Auto Scaling

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

➡️ Step 1 - Setting up VPC (Virtual Private Cloud)

To create a VPC, subnets, and other VPC resources using the console:

  1. Open the Amazon VPC console at https://console.aws.amazon.com/vpc/
  2. On the VPC dashboard, choose Create VPC.
  3. For Resources to create, choose VPC only.
  4. Enter the Name tag test-vpc
  5. For IPv4 CIDR block, enter an IPv4 address range for the VPC 12.0.0.0/16
  6. Leave the rest as default, choose Create VPC.

1

➡️ Step 2 - Configuring Internet Gateway

To create an internet gateway:

  1. Open the Amazon VPC console at https://console.aws.amazon.com/vpc/.
  2. In the navigation pane, choose Internet gateways.
  3. Choose Create internet gateway.
  4. Enter a name for your internet gateway.

2

To use an internet gateway, you must attach it to a VPC

  1. Choose Actions, Attach to VPC.

3

  1. Select an available VPC.
  2. Choose Attach internet gateway.

4

➡️ Step 3 - Configuring Subnet

To add a subnet to your VPC:

  1. Open the Amazon VPC console, in the navigation pane, choose Subnets, then choose Create subnet.
  2. Under VPC ID, choose the VPC for the subnet test-vpc

5 copy

Note: We are going to create 2 pubilc subnet in two Availability Zones: us-east-1a and us-east-1b

  1. For Subnet name, enter a name for your subnet test-public-subnet-1a
  2. Under Availability Zone, Choose the zone in which vour subnet will reside US East (N. Virginia) / us-east-la
  3. For IPV4 subnet CIDR block, select Manual input to enter an IPV4 subnet CIDR block for your subnet 12.0.1.0/24

5 copy 2

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:

  1. Open the Amazon VPC console, on the left hand side choose Choose the Route table tab.
  2. Choose Create route table
  3. Enter Route table name rt-test-public
  4. Under VPC choose test-vpc

6

Let's associate the route table into the subnet

  1. On the route table console, choose Subnet associations tab.
  2. Then choose Edit subnet associations.

7

  1. Select both subnets and choose Save associations.

8

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.

9

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

10

➡️ Step 4 - Creating a Target Group

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:

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/
  2. On the navigation pane, under Load Balancing, choose Target Groups.
  3. Choose Create target group.
  4. For Choose a target type, select Instances to register targets by instance ID.

11

  1. For Target group name, type a name for the target group tg-ec2-apache2

11 copy

  1. 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
  2. Leave the rest as default, Choose Next.
  3. Under Register targets >> Available instances, there are no instances which has been created yet those instances will be created by Auto Scale policy.
  4. choose Create target group.

11 copy 2

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.

➡️ Step 5 - Setting up an Application Load

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:

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.
  2. In the navigation pane, choose Load Balancers.
  3. Choose Create Load Balancer.
  4. Under Application Load Balancer, choose Create.
  5. For Load balancer name, enter a name for your load balancer alb-ec2-instances-with-asg

12

  1. Under Network mapping:
    a. For VPC, select the VPC that you used for your EC2 instances test-vpc
    b. For Mappings, enable zones for your load balancer by selecting subnets from two or more Availability Zones test-public-subnet-1a and test-public-subnet-1b

12 copy

  1. 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.

  1. Choose create a new security group.

12 copy 5

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.

  1. Enter security group name alb-sg-for-http-request
  2. Select the existing VPC test-vpc

13

  1. 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.

  1. Choose Create security group

13 copy

  1. Back to the Application Load Balancer, under security groups, refresh and add the new one alb-sg-for-http-request

12 copy 2

  1. 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 created tg-ec2-apache2

12 copy 3

  1. Review the load balancer configurations and make changes if needed. After you finish reviewing the configurations, choose Create load balancer.

➡️ Step 6 - Create an Auto Scaling group using a launch template

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):

  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/, and choose Auto Scaling Groups from the navigation pane.
  2. For Auto Scaling group name, enter a name for your Auto Scaling group asg-ec2-instances-test-demo
  3. For Launch template, choose create a Launch template

14

  1. Choose Create launch template. Enter a name it-ec2-instances-apache2

15

  1. 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.

16

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

15 copy 2

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

15 copy 3

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.

  1. 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.

14 copy

  1. Under Network, choose the VPC test-vpc
  2. For Availability Zones and subnets, add both test-public-subnet-1a and test-public-subnet-1b then choose Next

17

  1. On the Configure advanced options page, under Load balancing, choose Attach to an existing load balancer
  2. For Attach to an existing load balancer choose tg-ec2-apache2| HTTP

18

  1. Under Health checks, enable Turn on Elastic Load Balancing health checks.
  2. For Health check grace period, for this demo enter 20 seconds then click on Next

18 copy

  1. For Configure group size and scaling, enter Desired capacity to 2
  2. For Scaling, enter Min desired capacity to 1 and Max desired capacity to 3
  3. For Automatic scaling for this demo choose No scaling policies

19

  1. For notification choose Next, because will not add any notification
  2. Tags are also optional choose Next
  3. 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.

➡️ Step 7 - Testing our setup

Once our EC2 instances has been initialized properly, next let's test our load balancer.

  1. Back to the Load Balancer console, click on alb-ec2-instances-with-asg
  2. Copy the DNS name and paste it on a browser

20

  1. As you can see below we are able to access our EC2 instances.
  2. 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

21 22

  1. 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

23


* Second IP Address: 12.0.3.109

24

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.

25

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.

26

💰 Cost

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.

About

Amazon EC2 Auto Scaling helps you ensure that you have the correct number of Amazon EC2 instances available to handle the load for your application.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published