EC2 Auto Scaling & Elastic Load Balancer | AWS in Action

4 min read 7 months ago
Published on Aug 06, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

In this tutorial, we will explore how to set up EC2 Auto Scaling and Elastic Load Balancer in AWS. This is essential for managing workloads effectively, especially for applications with unpredictable traffic. Auto Scaling allows your application to adapt to varying traffic demands by automatically increasing or decreasing the number of instances. The Elastic Load Balancer ensures that traffic is distributed evenly across these instances, preventing any single instance from becoming overwhelmed.

Chapter 1: The Demo Project

  • We will use a simple Node.js REST API as our demo application.
  • The API has two endpoints that simulate heavy workloads by reading a file in a loop, which can overwhelm a single server under high traffic conditions.
  • The goal is to demonstrate how Auto Scaling can mitigate performance issues.

Chapter 2: Running the Code on an EC2 Instance

  1. Launch an EC2 Instance

    • Go to the AWS EC2 dashboard.
    • Launch a new instance using the Amazon Linux base AMI.
    • Select the t2.nano instance type to intentionally create a lightweight server that can be overwhelmed.
    • Opt for an existing Security Group that allows HTTP traffic on port 80.
    • Skip creating a key pair since we won’t connect directly to the instance.
  2. Add User Data Script

    • In the advanced details section, paste the User Data script to install Git, Node.js, and set up the demo application.
    • This script should download the demo code and start the web server in the background.
  3. Launch the Instance

    • Click on the launch button and wait for the instance to be up and running.

Chapter 3: Testing and Overwhelming the Demo Application

  1. Access the Instance

    • Retrieve the public IP or DNS of your running EC2 instance.
  2. Send Test Requests

    • Use a simple script to send a high volume of requests to the application running on the instance. This simulates heavy traffic and demonstrates how the instance struggles to handle it.
    • Monitor the instance's performance metrics in the AWS Management Console.

Chapter 4: The Need for Scaling

  • As the application experiences heavy load, the t2.nano instance will be overwhelmed.
  • You can choose to scale vertically by selecting a more powerful instance type or horizontally by adding more instances.

Chapter 5: Introducing the Auto Scaling Service

  1. Terminate the Current Instance

    • Go to the EC2 dashboard and terminate the overloaded instance.
  2. Create a Launch Template

    • Navigate to "Launch Templates" on the left menu.
    • Click "Create launch template."
    • Name the template (e.g., demo1) and include the same AMI and instance type (t2.nano).
    • Add the User Data script for setting up the application.
    • Save the launch template.

Chapter 6: Configuring Auto Scaling

  1. Create Auto Scaling Group

    • Click on "Auto Scaling Groups" in the EC2 dashboard.
    • Create a new Auto Scaling group and select the launch template created earlier.
    • Choose the appropriate VPC and subnets for your instances.
  2. Add Load Balancer

    • During the Auto Scaling configuration, enable the option for a Load Balancer.
    • Use the Application Load Balancer for HTTP/HTTPS traffic.
    • Set it to be internet-facing and specify the same subnets as the Auto Scaling group.
  3. Configure Scaling Parameters

    • Set the desired, minimum, and maximum capacities for the Auto Scaling group.
    • Define a target tracking scaling policy to add new instances when average CPU utilization exceeds a specified threshold (e.g., 50%).

Chapter 7: Auto Scaling in Action

  • After configuring the Auto Scaling group, one instance will be launched by default.
  • Simulate traffic again to verify that the Auto Scaling service responds correctly by adding more instances as needed.
  • Use the Load Balancer’s DNS name to send requests, allowing it to distribute the traffic across multiple instances.

Conclusion

In this tutorial, we covered the essentials of setting up EC2 Auto Scaling and Elastic Load Balancer in AWS. By following these steps, you can ensure that your applications can handle variable workloads efficiently. Remember to monitor your Auto Scaling group and make adjustments to the scaling parameters as necessary to optimize performance. For further exploration, consider integrating notifications for scaling events and experimenting with different instance types based on your application's requirements.