Create an Auto-Scaling group for Apache servers, behind an ALB

Christian Talavera
6 min readMay 3, 2021

For this demo, there will be a Launch Configuration created, with Apache web-servers deploying with an Auto-Scaling group. These servers will be deployed in multiple ‘Availability Zones’, which makes them highly available.

Once they are deployed, the scaling policy will be tested for if 0% downtime is achieved by stopping one of the running instances. If the Auto-Scaling policy works, then another instance should be started automatically.

Creating an Application Load Balancer

Navigate to AWS Console>Services>EC2>Load Balancers> Create Load Balancer> Create Application Load Balancer

Define Basic Configuration:

  • Name: sportsblog-alb
  • Scheme: internet-facing
  • IP address type: ipv4

Leave the settings in the Listeners section as-is.

For the Availability Zones section, choose the existing VPC.

Select us-east-1a and us-east-1b.

Click Next to skip Configure Security Settings, so you can configure the Security Groups for the ALB.

  • Assign a security group — select ‘Create a new security group’
  • Security group name — alb-sg-sportsblog
  • Description of security group — alb-sg-sportsblog security group for this demo

Leave the custom TCP rule settings as default and click next to configure routing. In the Target group section, define the following values:

  • Target group: New target group
  • Name: alb-sg-sportsblog
  • Target type: Instance
  • Protocol: HTTP
  • Port: 80

Leave the Health checks section settings with default settings. Under Advanced health check settings, change the Healthy threshold to “2”, but leave the other settings alone.

After this, click next to go to ‘Register Targets’ and leave the settings as default. Then click next to review, and click create to finish creating the Application ELB; this will take about 5–10 minutes to finish.

  • The ALB doesn’t work just yet; to test, copy the DNS name listed in the Description section of the ALB. Paste it into a new browser tab, which will result in a ‘503’ error.

SSH Key Creation

Now a Launch Template must be created; first, an SSH Key Pair must be made.

Navigate to AWS console>Services>EC2>Key Pairs>Create Key Pair. Give it a name of “alb-lt-sportsblog”, but leave the other parameters as default. Click Create key pair. It will automatically download once created. These keys will be needed later, so keep them safe; otherwise, a new pair must be created.

Create Security Group for EC2 Instances

Now the Security Group for the EC2 instances themselves be configured.

Navigate to AWS Console>Services>EC2>Security Groups>Create Security Group:

When creating the Security Group, define the basic details:

  • Security group name: sportsblog-ec2-sg
  • Description: security group for ec2 instances for sportsblog
  • VPC: The demo VPC being used

An ‘inbound rule’ must be created for SSH access; ‘Add rule’ and set the following values:

  • Type: SSH
  • Source: Anywhere

Now the ALB (via it’s Security Group) needs HTTP access to the EC2 instances; Add another rule again, and define these values:

  • Type: HTTP
  • Source type: Custom
  • Source: alb-sg-sportsblog

Leave the other settings as default and Click Create security group to finish:

Create the EC2 Instance Launch Template

The Launch Template responsible for the instance configuration to be used needs to be created.

Navigate to AWS Console>Services>EC2>Launch Templates>Create Launch Template, and set the name and description.

Define these values:

  • Launch template name: sportsblog-lt
  • Template version description: sportsblog launch template demo

Check the ‘Provide guidance to help me set up a template that I can use with EC2 Auto Scaling’ box for easy setup.

The details of the launch template need to be set. In the Launch template contents section, select the AMI image ‘Amazon Linux 2 AMI 64-bit (x86)’

Set the rest of the settings:

  • Instance type: t1.micro
  • Key pair name: alb-lt-sportsblog
  • Network type: VPC
  • Subnet: n/a
  • Security groups: sportsblog-ec2-sg

Lastly, a ‘bootstrap’ script must be defined in the Advanced details section, under the User data box. A bootstrap script allows for commands to be run as an instance is starting up, to automate software installation and configuration.

This script will install update the instance’s packages, download Apache web server, install it, and start the Apache daemon.

#!/bin/bash
yum update -y
yum install -y httpd
service httpd start

Finally, Click Create launch template, and View launch templates to finish.

At this point, all that is left is to create the Auto Scaling Group, which contains the Launch Configuration to define the resources. It will be launched behind the created ALB, and will deploy automated Apache web servers with the parameters set. First, check if the created ALB is active, under AWS Console>Services>EC2>Load Balancers; the created ‘sportsblog-alb’ should be available.

Create the Auto Scaling Group

Lastly, the Auto Scaling group, with the scaling configuration itself, needs to be set. Navigate to AWS Console>Services>EC2>Auto Scaling Groups>Create Auto Scaling Group.

For Choose launch template or configuration, define the following:

  • Auto Scaling group name: asg-sportsblog-1
  • Launch template (created earlier): sportsblog-lt

Click Next, and for Configure settings screen, define the following:

  • Instance purchase options: Adhere to launch template
  • VPC: Use the demo VPC
  • Subnets: us-east-1a and us-east-1b

Then, click Next to get to Configure advanced options screen, and define the following:

  • Load balancing: Attach to an existing load balancer
  • Choose from your load balancer target groups: alb-tg-sportsblog
  • Leave the Health checks settings as default

Under Additional settings, check the ‘Enable group metrics collection within CloudWatch’ box.

Click Next, and for ‘Configure group size and scaling policies’ define the following:

  • Desired capacity: 2
  • Minimum capacity: 2
  • Maximum capacity: 5

Lastly, define the ‘Scaling policies’; Select Target tracking scaling policy and define the following:

  • Scaling policy name: Target Tracking Policy
  • Metric type: Average CPU utilization
  • Target value: 30
  • Duration that instances need to warm up before being included in metric: 300 seconds

Click Next to continue; leave the Add notifications screen as default, the Add tags as default and click next, and finish by clicking Create Auto Scaling group.

Testing

Now, lets try to terminate one of the running EC2 instances to test the auto scaling group:

The first instance was terminated, and after a bit of time, another instance was deployed automatically, confirming that the Auto Scaling group works:

--

--

Christian Talavera

DevOps Engineer writing about breaking into the industry