Search code examples
amazon-web-servicesamazon-ecsamazon-route53amazon-vpcaws-cloudmap

How can you launch ECS Fargate containers having a public DNS?


I've built an AWS CodePipeline to build and deploy containers into Fargate managed EC2 instances. Ref AWS CodePipeline

One of the services is a web server and I'm attempting to access it from the public which is possible via a public assigned IP address; however, that's not very useful as each deployed container receives a fresh IP address.

I understand it's possible to setup Elastic IP addresses or point a domain to the container service but I'd think there is an easier way.

EC2 instances can be launched with the option of providing a Public DNS...

Is it possible to launch container services with a static public DNS record? If so, how?


Solution

  • Most Common Choice: ALB

    Although it's not free, normally if you want a public DNS name to an ECS service (fargate or EC2) you'd front it with a load balancer (which can also do SSL termination, if you so desire).

    Because of that, AWS makes it easy to create a load balancer or add your service to an existing target group when you're setting up a service. I don't think you can change that after the fact, so you may need to recreate the service.

    Finally, when you have a load balancer in front of the ECS service, you just need to set up a CNAME or an A ALIAS in Route53 (if you're using Route53) to direct a DNS name to that load balancer.

    AWS has a walkthrough from 2016 on the AWS Compute Blog quickly describing how to set up an ECS service and expose it using an Application Load Balancer.

    ECS Service Connect

    ECS Service Connect was announced at ReInvent 2022, and seems to let you serve up an ECS Service that is load balanced without having to provision or manage an ALB or an API Gateway:

    With ECS Service Connect, you can refer and connect to your services by logical names using a namespace provided by AWS Cloud Map and automatically distribute traffic between ECS tasks without deploying and configuring load balancers.

    CloudMap / Service Discovery / API Gateway

    With ECS Service Discovery and AWS CloudMap, you can use an API Gateway. Your load balancing options are more limited, but API Gateways are billed based on usage rather than hours, so it can potentially save costs on lower-volume services. You can also use a single API Gateway in front of multiple ECS services, which some people are going to want to do anyway. This approach is less commonly employed, but might be the right path for some uses.