I’m trying to resolve simple issue. How can I specify connection string to AWS ECS fargate-hosted task, which will preserve task restarts?
I created a task definition, then started a task. Ok, I see the private IP address. But I have no guarantee that it will be same when I change task definition or restart a task.
I’m starting JanusGraph database as a service that way, so my other EC2 instances need to know how to access it internally, in VPC (I don’t need external Elastic IP). How can I do this?
2
Answers
You need to enable ECS Service Discovery, so that your container will be mapped to a DNS name inside your VPC. ECS will manage updates to that DNS name if your task is restarted.
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:
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.