DEVELOPER BLOG

HOME > DEVELOPER BLOG > AWS Load Balancing and ECS: A Comprehensive Guide for Efficient Application Scaling

AWS Load Balancing and ECS: A Comprehensive Guide for Efficient Application Scaling

1. Introduction

Provide an introduction to the importance of load balancing and auto scaling in modern cloud-based applications. Introduce AWS Load Balancing and Elastic Container Service (ECS) as essential tools for ensuring high availability and scalability.  

2. Purpose

Clearly state the purpose of the article, which is to guide readers in setting up AWS Load Balancing with ECS and understanding auto scaling capabilities. 

3. What is AWS Load Balancing?

Load balancing in AWS refers to the practice of distributing incoming network traffic across multiple instances (virtual servers) to ensure optimal utilization, reliability, and availability of applications. This concept is crucial in cloud environments where applications can experience varying levels of traffic and demand. AWS provides various load balancing solutions to achieve this, including the Elastic Load Balancing (ELB) service. The main role of load balancing in AWS is to achieve the following goals:
  1. Improved Performance and Scalability: By distributing incoming traffic evenly across multiple instances, load balancers prevent any single instance from becoming overwhelmed by excessive traffic. This leads to improved response times and better application performance.
  2. High Availability: Load balancers can be configured to route traffic only to healthy instances. If one instance becomes unavailable due to hardware failure, software issues, or maintenance, the load balancer automatically directs traffic to the remaining healthy instances. This ensures that applications remain available even in the face of instance failures.
  3. Redundancy: Load balancers are designed with redundancy in mind. AWS load balancers are distributed across multiple availability zones, which enhances the overall resilience of the architecture. In the event of an availability zone failure, the load balancer can continue routing traffic to instances in the surviving zones.
  4. Auto Scaling: Load balancers work seamlessly with AWS Auto Scaling. As traffic increases, Auto Scaling can dynamically add new instances to the pool to handle the load. When traffic decreases, unnecessary instances can be terminated. Load balancers distribute traffic to the instances that are currently active and healthy, allowing for efficient resource utilization.
  5. Health Monitoring: Load balancers constantly monitor the health of instances they are responsible for. If an instance becomes unhealthy (due to failed health checks), the load balancer stops routing traffic to it until it becomes healthy again.
  6. SSL Termination: Load balancers can handle Secure Sockets Layer (SSL) encryption and decryption, offloading this resource-intensive task from the instances. This can improve performance on instances and simplify SSL certificate management.
  7. Session Persistence: Some applications require maintaining user sessions on a specific instance. Load balancers can be configured to ensure that subsequent requests from the same user are directed to the same instance, maintaining the session state.
AWS provides three main types of load balancers:
  1. Application Load Balancer (ALB): This layer 7 load balancer operates at the application layer and routes traffic based on content of the requests. It is ideal for distributing traffic to multiple services or microservices within an application.
  2. Network Load Balancer (NLB): This layer 4 load balancer forwards traffic based on IP protocol data. It is well-suited for applications that require ultra-low latency, such as gaming or financial applications.
  3. Classic Load Balancer (CLB): This is the legacy load balancer in AWS and provides basic load balancing across multiple Amazon EC2 instances.

4. What is auto scaling in ECS EC2 launch type?

Define auto scaling and its significance in automatically adjusting the number of EC2 instances based on traffic and demand. Explain how auto scaling works in conjunction with the ECS EC2 launch type to handle varying workloads.   In the context of Amazon Elastic Container Service (ECS) with EC2 launch type, "Auto Scaling" refers to the ability to automatically adjust the number of ECS instances or tasks based on changes in demand or defined metrics. ECS is a container orchestration service that allows you to manage and run Docker containers at scale. The EC2 launch type for ECS involves running containers on a cluster of EC2 instances.
  1. Scaling Policies: When you create an ECS service, you can configure it with an Auto Scaling policy. This policy determines how the number of tasks or instances is adjusted based on predefined metrics such as CPU utilization, memory utilization, or custom CloudWatch metrics.
  2. Dynamic Scaling: As the metrics specified in the scaling policies change, the ECS service automatically adjusts the number of tasks or instances running containers. For example, if CPU utilization increases beyond a certain threshold, more tasks or instances are launched. If the load decreases, excess tasks or instances can be stopped.
  3. Service Discovery and Load Balancing: ECS integrates with AWS Application Load Balancer (ALB) or Network Load Balancer (NLB) to distribute traffic to containers running on different instances.
Auto Scaling in conjunction with the Amazon Elastic Container Service (ECS) EC2 launch type works together to dynamically adjust the number of tasks or instances running containers in response to varying workloads. This combination ensures that your applications can efficiently handle changes in demand while maintaining optimal performance and resource utilization.

5. Creating AWS Load Balancing and ECS with AWS console

This section walks you through the process of creating an Application Load Balancer on AWS Console.   Step 1. Creating target group Each target group is used to route requests to one or more registered targets. When a rule condition is met, traffic is forwarded to the corresponding target group. Go to Load balancer feature in EC2 service → go to Target group → click to Create target group:     For Choose a target type,Instances to register targets by instance ID, IP addresses to register targets by IP address, or Lambda function to register a Lambda function as a target. If your service's task definition uses the awsvpc network mode (which is required for the Fargate launch type), you must choose IP addresses as the target type This is because tasks that use the awsvpc network mode are associated with an elastic network interface, not an Amazon EC2 instance. In this context, we choose Instances type. You can skip the target selection, we will update later. Then, create target group.   Step 2. Creating Load balancer Navigate to Load Balancers → click to Create load balancer:     In this blog, we use Application Load Balancer, please choose it:     Configure basic configuration:    
  • For Scheme, choose Internet-facing or Internal.
An internet-facing load balancer routes requests from clients to targets over the internet. An internal load balancer routes requests to targets using private IP addresses.
  • For IP address type, choose the IP adressing for the containers subnets.
In Network mapping, choose the VPC and the subnets for your load balancer.
  1. For VPC, select the same VPC that you used for the container instances on which you intend to run your service.
  2. For Mappings, select the Availability Zones to use for your load balancer. If there is one subnet for that Availability Zone, it is selected. If there is more than one subnet for that Availability Zone, select one of the subnets. You can select only one subnet per Availability Zone. Your load balancer subnet configuration must include all Availability Zones that your container instances reside in.
  Choose Security group and Target group: Note: Please make sure your security group has enough rules for access from Internet like port 80 and port 443.     Then, create load balancer.   Step 3. Creating ECS service Go to ECS service → Choose Cluster → Click to “Create cluster”:     Enter cluster name Choose the VPC and subnets you want to place your ECS cluster in     Choose Amazon EC2 instances in Infrastructure tab, then configure the Auto scaling group, create a new ASG like image or you can choose your existing ASG:     Click “Create” to create new cluster. The next step is creating ECS task definition, navigate to Task definitions → Click to “Create new task definition”. Enter task definition name and choose infrastructure requirements:     Configure container:     Creating ECS service base on the task definition, go to your ECS cluster → In the Services tab → Choose “Create”. Choose Launch type is EC2 and choose the task definition you just created:     In Networking section, you can leave it by default or configure to your VPC and subnets:     Configure load balancer for ECS service:     Then, create. Check your ECS service and load balancer:     Copy the load balancer’s DNS and paste on browser:     With DevOps knowledge: 8-10 hours Without DevOps knowledge: 4-5 days

6. Creating AWS Load balancing and ECS with Prismscaler

PrismScaler form Introduce Prismscaler as a valuable tool for streamlining the setup of AWS Load Balancing with ECS. Offer a quick and straightforward tutorial on integrating Prismscaler for faster and more efficient deployment.     Prism Scaler provides you with an intuitive model and a concise form, you just need to fill in the necessary information and press create, now PrismScaler will automatically build a simple web application/system that moves on a container on AWS:
  • An ECS cluster with a launch type of EC2, which initializes the task definition and service for ECS
  • An Application Load Balancer is connected to the ECS service on
5-15 minutes (without DevOps knowledge)

7. Describe types of load balancer in AWS

Amazon Web Services (AWS) provides three main types of load balancers: Application Load Balancer (ALB), Network Load Balancer (NLB), and Classic Load Balancer (CLB). Each type has specific use cases and benefits that cater to different scenarios. Here's a breakdown of each type: Application Load Balancer (ALB): Use Cases:
  • Web Applications: ALB is well-suited for routing HTTP/HTTPS traffic and is optimized to distribute traffic to different services within an application. It can route traffic based on content, such as URL path or host header, making it suitable for microservices architectures.
  • Containerized Applications: ALB integrates with Amazon ECS and Amazon EKS to route traffic to container instances or pods based on the service definitions.
Benefits:
  • Layer 7 Features: ALB operates at the application layer, allowing advanced routing decisions based on content.
  • Content-Based Routing: ALB can route requests to different target groups based on URL path or host header, enabling easy handling of multiple services.
  • Path-Based Routing: Useful for routing traffic to different microservices based on specific URL paths.
  • SSL/TLS Termination: ALB can handle SSL termination, offloading the decryption workload from backend instances.
Network Load Balancer (NLB): Use Cases:
  • High Performance: NLB is designed for extreme performance and low latency, making it suitable for scenarios that require consistent, high-throughput, and low-latency connections.
  • TCP/UDP Traffic: NLB is capable of routing TCP and UDP traffic, making it ideal for gaming, IoT, and other applications that require low-latency connections.
Benefits:
  • Layer 4 Load Balancing: NLB operates at the transport layer, forwarding traffic based on IP protocol data.
  • Static IP Address: NLB provides a static IP address that remains constant even if instances are added or removed from the target group.
  • Ultra-Low Latency: NLB is optimized for performance, making it suitable for real-time applications.
Classic Load Balancer (CLB): Use Cases:
  • Legacy Applications: CLB is the original AWS load balancer and may be used for existing applications that were built before ALB and NLB were introduced.
Benefits:
  • Basic Load Balancing: CLB provides basic load balancing across multiple Amazon EC2 instances.
  • Sticky Sessions: CLB supports session stickiness, ensuring that a user's requests are consistently directed to the same backend instance.
Each type of load balancer is designed to cater to specific requirements and use cases. When selecting a load balancer, consider the nature of your application and its traffic patterns. Here's a summary of when to use each type:
  • ALB: Choose ALB when you have web applications, microservices, and require content-based routing, SSL termination, and advanced request handling.
  • NLB: Choose NLB when you need low-latency, high-throughput connections for applications that require TCP/UDP traffic routing, such as gaming, IoT, and financial applications.
  • CLB: Use CLB for legacy applications that require basic load balancing and sticky sessions.

8. Reference

Provide a list of relevant sources and documentation for further exploration. Include links to AWS official documentation and other reputable resources. https://docs.aws.amazon.com/AmazonECS/latest/userguide/create-application-load-balancer.html#alb-configure-routing