Assigning a domain name to an AWS Fargate task
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: https://aws.amazon.com/blogs/aws/amazon-ecs-service-discovery/
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.
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.
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.