SWS Console: Routing multiple private EC2 instances to AWS ALB.
Published: 2024-02-23 09:33:27

In a previous [post related to EC2 DMS](https://medium.com/@steve_45946/creating-a-tool-to-build-architecture-for-deploying-my-project-on-a-private-ec2-in-aws-cloud-in-397a6ceccdea), I created a feature that I had discussed with a colleague. As I began exploring technical discussions in online communities, I noticed that people deploying projects on EC2 instances were grappling with similar concerns. One of the challenging concepts that emerged was the configuration of a single ALB to route traffic to multiple instances.

To make this clearer, I'll explain using the image below.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8pi850sqs9cjiuq1zblo.png)

The scenario is as follows: Let's assume we are implementing Microservices Architecture (MSA) by deploying payment, logging, and product servers on separate EC2 instances. The crucial aspect here is to route traffic to the appropriate server when a request comes to a single ALB, ensuring that requests reach the intended server.

The limitation of the existing EC2 DMS setup was that it was implemented with a one-to-one mapping of API servers to ALBs. Consequently, the target group rules were broadly defined, directing all requests to a single EC2 instance.

[Visit the Steve Web Service homepage](https://www.stevewebservice.com/)

[Visit EC2 DMS Documentation](https://docs.stevewebservice.com/aws/feature-guide/ec2-dms/)

To address this issue with EC2 DMS, we devised the following flow, as depicted in the image below:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ylp4e5taqkphbnzq8rt3.png)

1. SWS Project (VPC) is selected.

2. Select NEW ALB.

3. Choose CUSTOM for Target Path Pattern and input rules. In this case, users set the prefix path for each server's controller, and specify the rule accordingly. For example, if a request starts with /api/payment, it will be routed to the payment EC2 instance.

4. Enter the Target Health Check Path. For instance, if the path /api/payment/health is requested, a simple text such as "OK" should be displayed without any authentication or authorization checks.

5. Target Priority determines the priority, ranging from 1 to 999. Higher numbers indicate lower priority.

6. The rest follows the creation process of EC2 DMS. → [Go directly to EC2 DMS creation method.](https://docs.stevewebservice.com/aws/feature-guide/ec2-dms/create-ec2-dms-project)

We implemented the above idea, ultimately assisting people in routing traffic to multiple instances on a single ALB through EC2 DMS. By simply selecting the target ALB and following the same process, we could help route traffic to multiple instances on a single ALB.

If users were to set up the above process manually, they would need to learn various aspects, from creating ALBs to setting path patterns in target groups.

However, achieving our goal with just these settings seems straightforward, doesn't it?

[Visit the Steve Web Service homepage](https://www.stevewebservice.com/)

[Go to the documentation on routing multiple instances to an ALB with EC2 DMS](https://docs.stevewebservice.com/aws/feature-guide/ec2-dms/how-to-route-multiple-ec2-single-alb)

We will continue to create tools that enable users to build cloud architectures without extensive AWS Cloud knowledge, allowing developers to focus more on business needs. Furthermore, we aim to provide value so that even small development teams can make the most of AWS resources.

Our next article will address another problem-solving history. End.



[Visit the Steve Web Service homepage](https://www.stevewebservice.com/)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f8mmgzjitoow7th8mt2q.jpg)