Skip to content

How to prepare architecture for 1 public elastic IP and multiple ec2 instances with routing based on port

0

I want to achieve:

  • 1 public static IP (eip)
  • multiple ec2 instances (one for http and https, other for application endpoints)
  • If I access port 80 or 443 on public IP I want to route traffic to first ec2 instance
  • If I access specific port (e.g. 1234) on public IP I want to route traffic to second ec2 instance What components should I use in order to achieve it?

asked 2 years ago252 views

2 Answers
4

Hello,

Based on what you’ve shared, one simple approach could be to use a Network Load Balancer. You can configure it to forward traffic on ports 80 and 443 to your application EC2, while directing traffic on the other port to the second EC2.

Some docs:

Hope that helps! 😊

EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

0

To achieve the architecture you're describing with one public Elastic IP (EIP) and multiple EC2 instances with routing based on ports, you'll need to use a combination of AWS services. Here's a recommended approach:

  1. Elastic IP (EIP): Allocate one EIP to serve as your static public IP address.

  2. Network Load Balancer (NLB): Use an NLB as your entry point. NLBs operate at the transport layer (TCP/SSL) and can route traffic based on ports. They also support Elastic IP addresses.

  3. EC2 Instances: Set up your EC2 instances as described - one for HTTP/HTTPS traffic and another for your application endpoints.

  4. Target Groups: Create separate target groups for each EC2 instance.

  5. Listener Rules: Configure listener rules on the NLB to route traffic based on the incoming port:

    • Route traffic on ports 80 and 443 to the target group containing your HTTP/HTTPS EC2 instance.
    • Route traffic on port 1234 (and any other specific ports) to the target group containing your application endpoint EC2 instance.
  6. Security Groups: Configure appropriate security groups for your EC2 instances to allow incoming traffic on the necessary ports.

  7. VPC and Subnets: Ensure your NLB and EC2 instances are in the same VPC, with the NLB in public subnets and EC2 instances in private subnets for enhanced security.

This setup allows you to use a single Elastic IP while routing traffic to different EC2 instances based on the incoming port. The Network Load Balancer will handle the routing, and you can easily scale by adding more instances to the target groups as needed.

Remember to consider high availability by deploying your EC2 instances across multiple Availability Zones and configuring your NLB to be multi-AZ as well.
Sources
Use a Network Load Balancer for Amazon ECS - Amazon Elastic Container Service
Second PUBLIC IPv4 Address Associated in one Instance | AWS re:Post

answered 2 years ago

EXPERT

reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.