Service Discovery

Let services find each other by DNS name with Cloud Map or Service Connect.

Instead of hard-coding IPs, use service discovery so services call each other by a stable name.

Two approaches

  • ECS Service Connect — the modern option; adds a proxy, DNS names, and built-in metrics.
  • AWS Cloud Map — DNS-based discovery that registers task IPs under a namespace.

With Service Connect, one service can simply call http://orders and traffic is routed to healthy tasks.

Example

Example · json
{
  "serviceConnectConfiguration": {
    "enabled": true,
    "namespace": "prod",
    "services": [
      {
        "portName": "http",
        "clientAliases": [
          {
            "port": 80,
            "dnsName": "orders"
          }
        ]
      }
    ]
  }
}

When to use it

  • A microservices team uses ECS Service Connect so the payments service can reach the inventory service by calling http://inventory:8080 without managing DNS records.
  • A team registers ECS services with Cloud Map so a sidecar running outside ECS can discover service endpoints by querying the service registry.
  • An SRE switches from hardcoded IP-based service communication to Service Connect so traffic automatically shifts to healthy tasks when the service scales.

More examples

Enable Service Connect on a Service

Creates a service with Service Connect enabled so other services in the prod.local namespace can reach it at http://inventory:80.

Example · bash
aws ecs create-service \
  --cluster prod \
  --service-name inventory \
  --task-definition inventory:3 \
  --desired-count 2 \
  --launch-type FARGATE \
  --network-configuration 'awsvpcConfiguration={subnets=[subnet-a],securityGroups=[sg-svc],assignPublicIp=DISABLED}' \
  --service-connect-configuration \
    '{"enabled":true,"namespace":"prod.local","services":[{"portName":"http","discoveryName":"inventory","clientAliases":[{"port":80,"dnsName":"inventory"}]}]}'

Register Service in Cloud Map

Creates a Cloud Map private DNS namespace and service record so ECS tasks register A records that other services can resolve.

Example · bash
NS_ID=$(aws servicediscovery create-private-dns-namespace \
  --name prod.local \
  --vpc vpc-0abc12345 \
  --query 'OperationId' --output text)

aws servicediscovery create-service \
  --name payments \
  --dns-config 'NamespaceId='"$NS_ID"',DnsRecords=[{Type=A,TTL=10}]' \
  --health-check-custom-config FailureThreshold=1

List Cloud Map Service Instances

Lists all registered instances (task IPs and ports) for a Cloud Map service to verify which tasks are currently healthy and discoverable.

Example · bash
aws servicediscovery list-instances \
  --service-id srv-0abc12345 \
  --query 'Instances[*].{id:Id,ip:Attributes.AWS_INSTANCE_IPV4,port:Attributes.AWS_INSTANCE_PORT}'

Discussion

  • Be the first to comment on this lesson.