If you have a micro service architecture in AWS and you want to direct trafifc and balance the traffic you need and Elastic Load Balancer with target groups.
To my experiment this is what you need to do in order to direct traffic from a single ELB FQDN to multiple applications/containers.
This setup assumes that you have a one webapp/client and one or more back-end services to which the client talks to.
- Make sure that your container have a host port defined of 0. This will make ECS service automatically assign a dynamic port.
- Create a target group for each application (client app and all back-end services)
- Create the ELB and add rules to your listener, or example:
- ClientApp: no rules here, all traffic is assumed to go to the root of the DNS
- Backend services: IF rule with path rule of something like “/api/myapi* and associate the wanted target group
- This will redirect all traffic that contain /api/myapi to the designated target group
- Next go to ECS and in your cluster create a service or each client and back-end service that you want to redirect traffic. The reason you have to create a service or each app is that you can only associate one ELB and Target Group for each container and it’s port, even if you have multiple container in your task definition only one container can capture your traffic unless you do other configuration in your docker host.