Amazon Web Services (AWS) offers a robust and flexible cloud infrastructure, and one of the key components of managing web traffic is using Target Groups in combination with Elastic Load Balancers (ELB) or Auto Scaling Groups (ASG). A Target Group is used to define the targets (like EC2 instances, Lambda functions, or IP addresses) that a load balancer or Auto Scaling group routes traffic to. This allows for highly available, fault-tolerant applications that scale with demand.
What is a Target Group?
A Target Group is a set of resources (EC2 instances, Lambda functions, or IPs) that receive incoming traffic routed by a load balancer or Auto Scaling group. The target group is responsible for monitoring the health of the resources, and only healthy targets will receive traffic. AWS allows you to define different types of targets for different use cases, such as routing traffic to EC2 instances, containers, or Lambda functions.
Prerequisites
Before creating a target group, ensure the following.
- You have an AWS account with sufficient permissions to create resources like load balancers and target groups.
- A running Elastic Load Balancer (ELB) or Auto Scaling Group (ASG) (optional for integration).
- EC2 instances, Lambda functions, or IP addresses that can serve as targets for your target group.
Steps to create a target group
Step 1. Open the Amazon EC2 Console.
- Log into your AWS Management Console.
- Navigate to EC2 by typing “EC2” in the search bar and selecting it from the services list.
Step 2. Access the Target Groups Section.
In the left navigation pane under the Load Balancing section, click on Target Groups.
This will take you to the Target Groups page, where you can view, create, and manage your target groups.
Step 3. Create a New Target Group.
Click the Create Target Group button at the top of the Target Groups page.
You will be prompted to select a target type. Here, you have several options.
- Instances: Routes traffic to EC2 instances.
- IP addresses: Routes traffic to IP addresses (useful for containerized applications or external resources).
- Lambda functions: Routes traffic to AWS Lambda functions (ideal for serverless applications).
- Choose the appropriate target type for your needs.
Step 4. Configure Target Group Settings.
- Name: Provide a name for your target group. Make sure the name is descriptive so that you can easily identify the target group later.
- Protocol: Select the protocol (HTTP, HTTPS, TCP, etc.) for your target group. The protocol determines the communication method between the load balancer and the targets.
- Port: Specify the port your targets will listen on (e.g., port 80 for HTTP or port 443 for HTTPS).
- VPC: Select the Virtual Private Cloud (VPC) where your targets are located. This ensures that the load balancer and targets are within the same networking environment.
Step 5. Health Check Configuration.
- Health Check Protocol: Choose the protocol used to perform health checks on the targets (e.g., HTTP or TCP).
- Health Check Path: If you're using HTTP/HTTPS, specify the path (e.g., /health or /index.html) to use for the health check. AWS will check this path to verify if the target is healthy.
- Health Check Interval: Set the interval between health checks. The default is 30 seconds, but you can adjust it based on your needs.
- Unhealthy Threshold: The number of failed health checks before a target is marked unhealthy.
- Healthy Threshold: The number of successful health checks before a target is considered healthy.
- Timeout and Success Codes: Adjust the timeout period and success codes based on the application’s requirements.
Step 6. Register Targets.
Once you’ve configured the target group, you can register the actual resources (targets) that will receive the traffic.
- Select targets: If you selected EC2 instances as the target type, you can now select the instances you want to register.
- For Lambda functions, you will select an existing function or create a new one.
- For IP addresses, you will manually enter the IPs of the resources you want to include as targets.
- Click Add to registered to confirm the target registration.
- In my article, I have selected an IP address.
Step 7. Review and Create.
After completing all the configuration steps, Review the settings for accuracy, ensuring the name, protocol, VPC, and health check settings are correct.
Once confirmed, click Create to create the target group.
Step 8. Attach the Target Group to a Load Balancer or Auto Scaling Group.
- If you're using a Load Balancer (e.g., ALB or NLB), you will need to associate the target group with the load balancer. When creating or modifying a load balancer, select the target group in the listener rules to route traffic to the appropriate targets.
- If you're using an Auto Scaling Group, you can configure it to automatically add and remove EC2 instances from the target group based on scaling policies.
Conclusion
Creating a Target Group in AWS is a simple yet powerful way to manage and distribute traffic to your resources efficiently. By defining your target group and associating it with a Load Balancer or Auto Scaling Group, you ensure that traffic is routed properly and that your application remains available and responsive.
By following the above steps, you can set up a highly scalable and fault-tolerant infrastructure that can grow with your application needs. Whether you are dealing with EC2 instances, Lambda functions, or IP addresses, AWS Target Groups make load balancing and auto-scaling seamless, reliable, and easy to manage.