The Problem

RabbitMQ runs as an ECS Fargate task. Every time it restarts, it gets a new private IP.

restart #1 → 172.31.8.45
restart #2 → 172.31.22.111
restart #3 → 172.31.3.200

Cart cannot hardcode an IP that keeps changing.


What Cloud Map Does

Cloud Map is AWS’s internal DNS service. It gives RabbitMQ a stable hostname inside the VPC, and automatically keeps the IP behind that hostname up to date.

Cart connects to:  rabbitmq.cs6650.local:5672
                          │
                          ▼
                     Cloud Map
                  (internal phonebook)
                  rabbitmq.cs6650.local → 172.31.22.111
                          │
                          ▼
                   RabbitMQ container

Cart always uses the same hostname. Cloud Map handles the IP lookup.


Who Updates the IP?

ECS does it automatically:

ECS task starts → registers new container IP in Cloud Map
ECS task stops  → deregisters the old IP from Cloud Map

No manual intervention needed.


Analogy

Like a contact saved as “Mom” in your phone. You never dial the number directly — you just tap “Mom”. If she gets a new number, you update the contact once and everything still works.

Cloud Map is that contact book, but for services inside AWS.


Terraform Configuration

# 1. Create the private DNS namespace:  *.cs6650.local
resource "aws_service_discovery_private_dns_namespace" "internal" {
  name = "cs6650.local"
  vpc  = data.aws_vpc.default.id
}
 
# 2. Register a service entry:  rabbitmq.cs6650.local
resource "aws_service_discovery_service" "rabbitmq" {
  name = "rabbitmq"
  dns_config {
    namespace_id = aws_service_discovery_private_dns_namespace.internal.id
    dns_records {
      ttl  = 10   # DNS cache expires after 10 seconds
      type = "A"  # returns an IPv4 address
    }
  }
}
 
# 3. Link the ECS service to Cloud Map
resource "aws_ecs_service" "rabbitmq" {
  service_registries {
    registry_arn = aws_service_discovery_service.rabbitmq.arn
  }
}

Cart reads the hostname from an env var:

{ name = "RABBITMQ_HOST", value = "rabbitmq.cs6650.local" }

Why Not Use the ALB Instead?

The ALB only handles HTTP traffic (port 80). RabbitMQ uses AMQP protocol on port 5672 — the ALB cannot route it. Cloud Map (DNS-based) works for any protocol.

ALBCloud Map
ProtocolHTTP / HTTPSAny (TCP, AMQP, etc.)
RoutingPath-basedDNS resolution
Use caseExternal + internal HTTPInternal service discovery