Assigning a domain name to an AWS Fargate task

Solution 1:

For most cases, you most likely want to use an ALB/NLB for service discovery. While there are some costs, you also get a lot of benefits: Some DoS protection, scaling metrics, logging, SSL/TLS

However, you can use ECS service discovery.

Service discovery uses Amazon Route 53 auto naming API actions to manage DNS entries for your service's tasks, making them discoverable within your VPC


Public namespaces are supported but you must have an existing public hosted zone registered with Route 53 before creating your service discovery service.

Service discovery requires that tasks use either the awsvpc, bridge, or host network mode.

Here is a Blog entry detailing how to use Service discovery with fargate:

Solution 2:

I tried to. The problem is the public ip that fargate uses is attached to the TASK so if the task is restarted then a new ip address is given and the dns record needs to be updated. Thats why the alb/nlb is used.

In theory you could use something else to manage the dns host records, perhaps a lambda function or something

Service Discovery Considerations: The DNS records created for a service discovery service always register with the private IP address for the task, rather than the public IP address, even when public namespaces are used.

Solution 3:

I don't believe @m-glatki's answer is correct - you cannot use Service Discovery for public IP addresses, as mentioned by @andreas-pasch.

The only way I've found to implement this was to create a Lambda function triggered by a Cloudwatch Event for when the container reaches the 'RUNNING' state. My first attempt was to fetch the container metadata and update Route 53 on launch via a startup script but this was a dead-end.

There's a good guide here for JavaScript (I re-wrote in Python & Terraform). Something I thought missing was adding the ECS cluster ARN to the Cloudwatch Event pattern under 'detail'; the function may trigger on every container launch otherwise. The ECS event docs might be useful for specifics.

AWS have a guide for EC2 but I didn't bother wading through their code; might be handy to refer to if you also prefer Python.

NB: Service Discovery works well for private IP addresses.