AWS ECS Docker container and load balancing with service discovery

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.

The steps:

  1.  Make sure that your container have a host port defined of 0. This will make ECS service automatically assign a dynamic port.
  2. Create a target group for each application (client app and all back-end services)
  3. Create the ELB and add rules to your listener, or example:
    1. ClientApp: no rules here, all traffic is assumed to go to the root of the DNS
    2. Backend services: IF rule with path rule of something like “/api/myapi* and associate the wanted target group
      1. This will redirect all traffic that contain /api/myapi to the designated target group
  4. 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.




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.