AWS EC2 — High Availability using ALB, ASG, Multiple AZ

Akrur Neupane
6 min readJun 16, 2023

--

We are going to launch a website website with High Availability using different AWS services. The AWS architecture in this scenario includes two EC2 instances that are hosted within a Virtual Private Cloud (VPC). The VPC includes both public and private subnets to provide secure and scalable network connectivity. The two EC2 instances are distributed across two different availability zones (AZ) to ensure high availability and fault tolerance.

During peak times, the number of EC2 instances will be increased to four using Amazon Elastic Load Balancing (ALB). This enables the architecture to scale seamlessly based on demand while maintaining high availability and performance.

The database used in this architecture is Amazon DynamoDB, which is protected by AWS IAM (Identity and Access Management). IAM enables you to control who can access your DynamoDB data and resources. This helps ensure that your data is secure and protected from unauthorized access.

File storage is accomplished using Amazon S3, which is a highly scalable and durable object storage service. S3 provides the ability to store and retrieve any amount of data from anywhere on the web, making it an ideal solution for file storage and sharing.

CloudWatch is used for monitoring and alerting in this architecture. CloudWatch provides real-time monitoring of resources, applications, and services in AWS. It also provides alarms to notify you of changes in your environment so that you can take appropriate actions.

Creating a VPC

First you need to create a VPC. If you dont know how to create then go to:

https://medium.com/@neupaneakrur11/creating-a-vpc-with-2-public-and-private-subnet-in-aws-and-launching-ec2-on-there-e4daaf936ab2

Launching an EC2 instance

In this task, you will launch an EC2 instance that hosts the application.

Search for and open EC2.

In the navigation pane, choose Instances.

Create a new instance on your own preferences. Select the VPC that we have created.

On advance details in the User data paste the following script:

#!/bin/bash -ex
yum -y install git
git clone https://github.com/neupaneakrur/flaskapp.git
cd flaskapp/
yum -y install python3-pip
pip install -r requirements.txt
yum -y install stress
export AWS_DEFAULT_REGION=<INSERT REGION HERE>
FLASK_APP=application.py /usr/local/bin/flask run --host=0.0.0.0 --port=80

In the user data code, replace the AWS_DEFAULT_REGION placeholder value with your Region

Launch the instance and In a new browser window, paste the IP address. Make sure to remove the ‘S’ after HTTP so you are using only HTTP instead.

Creating the Application Load Balancer

In the navigation pane, under Load Balancing, choose Load Balancers.

Choose Create Load Balancer.

On the Application Load Balancer card, choose Create.

Configure the following load balancer settings.

  • Load balancer name: app-alb
  • VPC: app-vpc (name of your vpc)
  • Mappings: Select both Availability Zones
  • Example: If you are in US West (Oregon), you would select both us-west-2a and us-west-2b
  • First Availability Zone Subnet: Public Subnet 1
  • Second Availability Zone Subnet: Public Subnet 2

In the Security groups section, remove the default security group (by choosing the X) and choose Create new security group.

Configure the following security group settings:

  • Security group name: load-balancer-sg
  • Description: HTTP access
  • VPC: If needed, paste the VPC ID for app-vpc and choose it when it appears under the box
  • Inbound rules: Add Rule
  • Type: HTTP
  • Source: Anywhere-IPv4

Create security group.

Close the security group browser window or return to the Load balancers window.

For Security groups, add the new load-balancer-sg group. Note: To see the new security group, you might need to refresh the Security groups list.

In Listeners and routing, choose Create target group.

For Specify group details, configure the following settings.

  • Choose a target type: Keep Instances selected
  • Target group name: app-target-group
  • Health checks: Expand Advanced health check settings and configure the following:
  • Healthy threshold: 2
  • Unhealthy threshold: 5
  • Timeout:30
  • Interval: 40

Choose Next.

For Register targets, select the instance that we have created and choose Include as pending below.

Choose Create target group.

Close the target groups window or return to the Load balancers window.

Under Listeners and routing, refresh the available listener and choose app-target-group.

Finally, choose Create load balancer.

Choose View load balancer.

Make sure that app-alb is selected and wait for the load balancer State to become Active.

Copy DNS name and paste it into a text editor of your choice and paste it into a new browser window. (add http:// before the DNS name)

You should see the flask application.

Creating the launch template

Now that you can access your application from a singular DNS name, you can scale the application horizontally. To scale horizontally, you need a launch template. In this task, you will create a launch template.

Open EC2.

In the navigation pane, under Instances, choose Launch Templates.

Choose Create launch template and configure the following settings.

  • Launch template name: app-launch-template
  • Template version description: write description
  • Auto Scaling guidance: Provide guidance to help me set up a template that I can use with EC2 Auto Scaling
  • Application and OS Images (Your EC2 Image) — required: Make AMI from ec2 that we are running. If you dont have AMI then create it from ec2 instances

Choose Create launch template.

Choose View Launch templates.

Creating the Auto Scaling group

In the navigation pane, under Auto Scaling, choose Auto Scaling Groups.

Choose Create Auto Scaling group.

For Choose launch template or configuration, configure these settings:

  • Auto Scaling group name: app-asg
  • Launch template: app-launch-template (launch template that we have created above)

Choose Next.

For Choose instance launch options, configure these settings:

  • VPC: app-vpc
  • Availability Zones and subnets: Choose the Availability Zones with Public Subnet 1 and Public Subnet 2

Choose Next.

For Configure advanced options, use these settings:

  • Load balancing: Attach to an existing load balancer
  • Attach to an existing load balancer: Choose from your load balancer target groups ( that we have created at above)
  • Existing load balancer target groups: app-target-group
  • Health checks: ELB

Choose Next.

For Configure group size and scaling policies, use these settings (you can choose these settings according to your requirements):

  • Desired capacity: 2
  • Minimum capacity: 2
  • Maximum capacity: 4
  • Scaling policies: Target tracking scaling policy
  • Target value: 60
  • Instances need: 300

Choose Next.

For Add notifications, choose Add notification and configure these settings:

  • SNS Topic: Create a topic
  • Send a notification to: app-sns-topic
  • With these recipients: Enter your email address

Choose Next and then choose Next again.

Choose Create Auto Scaling group.

You should receive an AWS Notification — Subscription Confirmation email.

Open this email message and choose Confirm subscription.

(A web browser window should open with a Subscription confirmed! message)

Testing the application

In this task, you will stress-test the application and confirm that it scales.

In the navigation pane, under Load Balancing, choose Target Groups.

Make sure that app-target-group is selected and choose the Targets tab.

You should see two additional instances launching.

Wait until the Status for both instances is healthy.

In the navigation pane, choose Load Balancers and make sure that app-alb is selected.

Again, copy the DNS name, at the beginning of the URL, add http://

In a new browser window, paste the URL

At the end of the URL, add /info.

You should see an Instance Info page, which shows which instance_id and availability_zone you are being routed to.

Refresh the page a few times. Each time, note that the values for instance_id or availability_zone can be different from the previous ones.

Now, you need to test auto scaling by stressing the CPU of the instance.

For Stress cpu, choose 10 min.

The top of the browser window should show a message that says Stressing CPU.

Wait for 10 minutes and after the 10 minutes are over, return to the Amazon EC2 console window.

In the navigation pane, under Load Balancing, choose Target Groups.

Select app-target-group and choose the Targets tab.

You should see additional instances were launched because of the stress test. You should also see a notification email.

--

--