Search code examples
amazon-web-servicesamazon-ecsaws-cdk

Route subdomains to separate ECS Services without Route 53


Is it possible to have 2 ECS Services in the same task, accessible from different subdomains without Route53? I am using Fargate but that is not necessary if it makes a difference.

For example:

  • foo.example.com is routed to MyService1
  • bar.example.com is routed to MyService2

I'm using CDK/Typescript loosely based on this stack.

I have found there are solutions using Route53, but these domains must remain on Cloudflare.


Solution

  • Is it possible to have 2 ECS Services in the same task

    This part of your question is really confusing. In ECS you have Services that will run 1 or more instances of a single ECS Task. An ECS Task is a single instance of the container(s) defined in your ECS Task Definition.


    Ignoring that confusing part of your question, the way to have different subdomains routed differently to ECS requires creating two Target Groups behind a single Application Load Balancer, and configuring domain based routing in the load balancer to send the requests to the appropriate target group depending on the subdomain.

    You can have two separate ECS Services defined, and configure each one with one of those target groups, thus routing the traffic to specific ECS service for that subdomain.

    Alternatively, you can have an ECS Task with two containers defined in it, and you can configure the ECS service to register one of those containers with one of the target groups, and the other container with the other target group.

    Finally, you can have an ECS Task with one container definition that listens on two different ports, and you can configure the ECS service to register one of those ports with one of the target groups, and the other port with the other target group.


    Note that since you are using Cloudflare, you could get rid of the load balancer and have the containers register themselves with Cloudflare Tunnels, and handle the routing entirely in Cloudflare, possibly with a Cloudflare load balancer.