Public Cloud

Seamlessly Integrating ELB and Auto Scaling with EC2 on AWS: A Comprehensive Guide

Damian Igbe, Phd
Sept. 7, 2024, 3:09 p.m.

Subscribe to Newsletter

Be first to know about new blogs, training offers, and company news.

Elastic Load Balancing (ELB) on AWS

 
Connecting an EC2 instance to an Application Load Balancer (ALB) in AWS involves several steps. Here's a high-level overview:
 

1. Create an Application Load Balancer (ALB)

1. Sign in to the AWS Management Console and open the EC2 Dashboard.
2. Navigate to Load Balancers under Load Balancing in the left sidebar.
3. Click on Create Load Balancer.
4. Select Application Load Balancer and click Create.
5. Configure the load balance:
   - Basic Configuration: Enter a name for the ALB, select the scheme (Internet-facing or Internal), and choose the IP address type (IPv4 or dualstack).
   - Network Mapping: Select the VPC and the subnets where you want the ALB to be available. Typically, you select multiple subnets in different Availability Zones for high availability.
6. Configure Security Groups: Assign one or more security groups to your ALB. Ensure the security group allows inbound HTTP/HTTPS traffic on the appropriate ports (80 for HTTP, 443 for HTTPS).
7. Configure Listeners: Define listeners for your ALB. Typically, you would add HTTP (port 80) or HTTPS (port 443) listeners.
8. Configure Routing:
   - Create a new target group or select an existing one. The target group will contain your EC2 instances.
   - Configure health checks for the target group.
9. Register Targets: You can optionally register targets at this step, but this can also be done later.
10. Review and Create: Review your configuration and create the load balancer.
 

2. Create a Target Group

If you didn't create a target group during the ALB setup, you can create one separately:
 
1. In the EC2 Dashboard, go to Target Groups under Load Balancing.
2. Click on Create Target Group.
3. Configure the target group:
   - Target Type: Select `Instance` for EC2 instances.
   - Protocol and Port: Set the protocol (HTTP or HTTPS) and the port on which the application is running.
   - Health Check Settings: Configure the health check path, interval, timeout, and healthy/unhealthy threshold.
4. Register Targets: Select your EC2 instances to be part of this target group.
5. Create Target Group: Review and create the target group.
 

3. Register EC2 Instances with the Target Group

If you didn’t register instances during the target group creation:
 
1. Go to the Target Groups section in the EC2 Dashboard.
2. Select the target group you created.
3. Click on the Targets tab.
4. Click Edit and then Add to select your EC2 instances.
5. Click Save.
 

4. Verify Security Groups and Network ACLs

Ensure that:
-EC2 Security Groups: The security group associated with your EC2 instances allows inbound traffic from the ALB’s security group.
- ALB Security Groups: The security group associated with your ALB allows inbound traffic on the necessary ports (e.g., 80 and 443) and outbound traffic to your EC2 instances.
- Network ACLs: Verify that network ACLs for the VPC subnets are not blocking the traffic.
 

5. Test the Configuration

1. Access the DNS name of the ALB (found on the ALB's description page).
2. Verify that traffic is correctly routed to your EC2 instances and that the instances are responding as expected.
 
That’s it! You’ve now set up an Application Load Balancer with EC2 instance
 
 

Auto Scaling group on AWS

To connect an EC2 instance to an Auto Scaling group on AWS, you'll need to set up an Auto Scaling group and configure it to launch and manage EC2 instances based on certain criteria. Here are the detailed steps:

1. Create a Launch Configuration or Launch Template

Launch Configuration (deprecated but still used in some scenarios):

  1. Sign in to the AWS Management Console and open the EC2 Dashboard.
  2. In the left sidebar, click on Launch Configurations under Auto Scaling.
  3. Click Create launch configuration.
  4. Configure the launch configuration:
    • Choose AMI: Select an Amazon Machine Image (AMI) for your instances.
    • Choose Instance Type: Select the instance type you need.
    • Configure Details: Add a name and configure other details as needed.
    • Add Storage: Configure storage options.
    • Configure Security Groups: Select or create a security group for the instances.
    • Configure Key Pair: Select an existing key pair or create a new one.
  5. Review and Create: Review the configuration and click Create launch configuration.

Launch Template (preferred method):

  1. In the EC2 Dashboard, click on Launch Templates under Instances.
  2. Click Create launch template.
  3. Configure the launch template:
    • Template Name: Enter a name for the template.
    • AMI and Instance Type: Choose the AMI and instance type.
    • Key Pair: Select a key pair.
    • Network and Security: Configure network interfaces and security groups.
    • Storage: Set up storage volumes.
    • Advanced Details: Configure additional settings as needed.
  4. Create Launch Template: Review and create the template.

2. Create an Auto Scaling Group

  1. Open the EC2 Dashboard.
  2. Click on Auto Scaling Groups under Auto Scaling.
  3. Click Create Auto Scaling group.
  4. Choose a Launch Template or Launch Configuration:
    • Select the launch template or configuration you created earlier.
  5. Configure Auto Scaling Group:
    • Group Name: Enter a name for the Auto Scaling group.
    • Network: Choose the VPC and subnets where instances should launch.
    • Group Size: Specify the minimum, maximum, and desired number of instances.
  6. Configure Scaling Policies:
    • Scaling Policies: Set policies for scaling out (adding instances) and scaling in (removing instances). You can define policies based on metrics like CPU utilization or network traffic.
  7. Configure Notifications (optional): Set up notifications to alert you about scaling activities.
  8. Review and Create: Review your configurations and create the Auto Scaling group.

3. Attach Auto Scaling Group to Load Balancer (optional but common)

  1. Open the Auto Scaling Groups section in the EC2 Dashboard.
  2. Select the Auto Scaling group you created.
  3. Click on the Details tab and then Edit.
  4. Attach to Load Balancer:
    • Load Balancer: Select the Application Load Balancer or Network Load Balancer you wish to use.
    • Target Groups: Select the target groups associated with the load balancer.
  5. Save Changes: Save the configuration.

4. Test the Auto Scaling Configuration

  1. Monitor: Check the Auto Scaling Groups page to ensure the Auto Scaling group is functioning correctly and instances are being launched or terminated as per the policies.
  2. Simulate Load: To test scaling policies, you can simulate load by increasing traffic or using AWS CloudWatch alarms to trigger scaling activities.

5. Monitor and Adjust

  • CloudWatch Metrics: Use Amazon CloudWatch to monitor the performance and scaling metrics of your Auto Scaling group.
  • Adjust Policies: Based on performance and needs, adjust the scaling policies or configurations as necessary.

By following these steps, you’ll set up an Auto Scaling group that can automatically adjust the number of EC2 instances based on your specified criteria and policies.

Zero-to-Hero Program: We Train and Mentor you to land your first Tech role