Service Discovery - Automating Microservice Connectivity with AWS Cloud Map
Learn how to build service discovery with AWS Cloud Map. Covers DNS-based and API-based service detection, ECS/EKS integration, and App Mesh connectivity.
The Challenge of Service Discovery
In microservices architectures, resolving communication endpoints between services is a critical challenge. In monolithic applications, communication targets are relatively fixed, but in microservices, IP addresses change dynamically due to container starts/stops, Auto Scaling, and deployments. While placing a load balancer (ALB) in front of each service is one approach, cost and management complexity increase as the number of services grows. AWS Cloud Map is a service discovery service that centrally manages microservice endpoints in namespaces. It registers each service's instances (IP addresses, ports, custom attributes) in a registry, allowing other services to look up connection targets by name. It provides two discovery methods - DNS-based and API-based - that you can choose based on your use case.
Registering Namespaces and Services
Cloud Map manages resources in a three-tier structure: Namespace, Service, and Instance. Namespaces come in two types: DNS-based (public DNS or private DNS) and API-based (HTTP). With DNS namespaces, service names are registered as DNS records, and connection targets can be resolved through standard DNS queries. For example, you can retrieve the IP address of a payment service using the DNS name payment.myapp.local. With API namespaces, you search for services using the DiscoverInstances API, with filtering by custom attributes (version, environment, region, etc.). ```bash # Create a private DNS namespace aws servicediscovery create-private-dns-namespace \ --name myapp.local \ --vpc vpc-12345 \ --region ap-northeast-1 # Register a service aws servicediscovery create-service \ --name payment \ --namespace-id ns-xxxx \ --dns-config '{"DnsRecords":[{"Type":"A","TTL":10}]}' ``` Instance registration and deregistration can be done manually (via API calls) or automatically through ECS/EKS integration.
ECS Integration and Health Checks
Native integration between Cloud Map and ECS automatically registers instances in Cloud Map when ECS service tasks start and automatically deregisters them when tasks stop. Integration is completed simply by specifying serviceRegistries in the ECS service definition. This enables inter-service communication by resolving task IP addresses directly through Cloud Map DNS names, bypassing ALBs. This reduces ALB costs (approximately $22/month per ALB) and lowers latency. Two types of health checks are supported. Route 53 health checks perform HTTP/HTTPS/TCP health checks on instances with public IPs. Custom health checks report health status by calling the UpdateInstanceCustomHealthStatus API from the application side. With ECS integration, health checks are linked to ECS task health checks, so additional health check configuration is typically unnecessary. To broaden your network design knowledge, specialized books on Amazon can also be helpful.
App Mesh Integration and Pricing
AWS App Mesh (service mesh) uses Cloud Map as its service discovery backend. By associating Cloud Map services with App Mesh virtual services, Envoy proxies automatically retrieve endpoints from Cloud Map and apply mesh features such as traffic routing, retries, and circuit breakers. In EKS environments, the AWS Cloud Map Controller for Kubernetes automatically synchronizes Kubernetes Service resources with Cloud Map. Pricing is $0.10/month per namespace, $0.10 per 1,000 instance registrations, and $1.00 per million DiscoverInstances API queries. DNS queries are charged at Route 53 rates ($0.40 per million queries). For small to medium microservice environments, the monthly cost is typically just a few dollars.
Summary - Cloud Map Usage Guidelines
AWS Cloud Map is a service discovery service that centrally manages microservice endpoints. Its key strengths are two discovery methods (DNS-based and API-based), native ECS integration, and automatic removal of unhealthy instances through health checks. When running microservices on ECS, adopting Cloud Map instead of ALBs for inter-service communication can achieve cost reduction and latency improvement.