I have two services running on ECS: service_1 and service_2. Service_2 uses service_1. Both services are on the same namespace.
Service Connect service configuration on service_1 sets DNSName and port in the client alias section. Let's say DNSName is set to service1.
Deployment is done using Terraform.
When deploying both services in parallel, /etc/hosts on service_1 container will have entry similar to 127.255.0.1 service1, and so it can discover itself. However on service_2, there is no such entry in /etc/hosts created, so it is not able to find service_1 by hostname.
"Solution" is to force redeploy service_2. Once redeployed, /etc/hosts on service_2 will have new entry, pointing to service1.
My guess is that AWS Service Connect does not autorefresh /etc/hosts on already running services. Which in a way defeats the purpose of this service.
Since I only have one way dependency, I can utilise depends_on in Terraform, so that service_1 is up and running, before service_2 is deployed. But what if I had bi-directional dependency? Do I need to create both, then force-recreate one so that they can find each other?
EDIT
Relevant Terraform code snippets:
resource "aws_service_discovery_private_dns_namespace" "this" {
name = "aws.internal"
<...>
}
module "ecs_cluster" {
source = "terraform-aws-modules/ecs/aws//modules/cluster"
cluster_service_connect_defaults = {
namespace = aws_service_discovery_private_dns_namespace.this.arn
}
<...>
}
module "service1" {
source = "terraform-aws-modules/ecs/aws//modules/service"
cluster_arn = module.ecs_cluster.arn
<...>
container_definitions = {
<...>
service1 = {
<...>
port_mappings = [
{
name = "service1"
containerPort = 8888
hostPort = 8888
protocol = "tcp"
}
]
}
}
service_connect_configuration = {
enabled = true
service = {
client_alias = {
port = 8888
dns_name = "service1"
}
port_name = "service1"
discovery_name = "service1"
}
}
Unfortunately this is an expected behaviour of ECS Service Connect.
This is what AWS recommends as well as described in the Service Connect Concept: Deployment Order
What you can try is to set both services as
Client and serverin Service Connect configuration and access them from each other using their DNS. E.g.:service1.namespaceandservice2.namespace.Edit
Your Namespace's
Instance discoveryshould be set toAPI calls and DNS queries in VPCs