There is a service called AWS Cloud Map that partly exists for this purpose. In the context of enabling inter-instance communication between tasks within your VPC, you would create a Cloud Map namespace. When creating the Namespace, for Instance Discovery, you can specify "API calls and DNS queries in VPCs". I would recommend to create a Private DNS Namespace. Cloud Map will automatically create a Hosted Zone with the same name as the namespace. If you view the Hosted Zone at this point, you will notice only an NS Record and SOA Record type.
Now without using ECS Service Discovery, you would have to create a Service for your Namespace. Specify the Service to be Discovered by APIs and DNS. Then you choose the Routing Policy for Route 53 DNS records that Cloud Map creates when you use this service to register instances. In other words, when you register instances to this service, Cloud Map will create DNS records. It is here you can specify the Record Type as A.
Then register one or more instances to your service.
However, with ECS Service Discovery, when you create the Service in ECS, you can specify Service Discovery, associating the Service with the Cloud Map Namespace you created. Here is terraform code I typically use to do it:
resource "aws_service_discovery_service" "discovery_service" {
name = var.service_name
dns_config {
namespace_id = var.namespace_id
dns_records {
ttl = 10
type = "A"
}
}
}
Now when an instance registers to the Service in Cloud Map, a new DNS Record will appear in the Private Hosted Zone with the same name as the Cloud Map namespace. This new DNS Record will have the name of the Service concatenated with the name of the namespace. This hostname is what you use to reach other tasks inside your cluster. It is guaranteed to be there, even if the task drains and recreates with a new private IPv4 IP in the specified subnet.