Kafka on AWS ECS, how to handle advertised.host without known instance?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a popular distributed event streaming platform used extensively for building real-time data pipelines and streaming applications. When deploying Kafka on AWS ECS (Elastic Container Service), managing Kafka's advertised.host settings can be challenging, especially since ECS uses dynamic management of containers, often without static IP addresses.
Understanding advertised.host in Kafka
advertised.host (or advertised.listeners in the latest versions) in Kafka's configuration specifies the host name or IP address and port that the broker will tell other brokers and clients about. This is critical for both internal communication between Kafka brokers and external communication with clients.
Challenges with advertised.host on AWS ECS
During deployment on AWS ECS:
- Dynamic IP Allocation: ECS tasks can have dynamic IP addresses that change every time they restart.
- ECS Service Discovery: Service discovery used in ECS does not directly support Kafka’s need for stable hostnames.
Solutions to Handle advertised.host on AWS ECS
Here are methods and best practices to ensure correct broker advertising in this environment.
1. Using ECS Service Discovery
AWS ECS supports service discovery via Route 53, which allows you to use DNS names rather than IP addresses. When a Kafka broker registers itself, it can advertise a DNS name that remains constant even if the container restarts and changes IP address.
Setting up Service Discovery
- Create a private namespace for your cluster (e.g.,
kafka.local). - Configure a service within this namespace in ECS. For each Kafka task you can specify a readable DNS name.
- Configure Kafka
advertised.listenersto use the DNS name provided by ECS.
2. ECS Task Networking
Utilizing the awsvpc network mode in ECS tasks, you can allocate an Elastic Network Interface (ENI) to each task, which comes with its own static private IP address in the VPC.
Steps:
- Set the network mode of your ECS service to
awsvpc. - Kafka brokers can advertise this stable private IP.
3. Load Balancer (Classic or Application)
Deploy a Load Balancer (LB) in front of your ECS service. The LB receives a stable DNS address that you can set as the advertised.listeners in Kafka.
- Note: This method primarily makes sense if clients are outside the same VPC or if you need an additional layer of abstraction. This approach might introduce latency.
Technical Example
Consider a configuration for Kafka running under awsvpc networking mode:
Here, ip-address-of-the-eni would be the private IP associated with the ECS task through awsvpc.
Summary Table
| Feature | Solution | Pros | Cons |
| Dynamic IP | ECS Service Discovery | Stable DNS, easy client connection | Setup complexity, DNS update latency |
| Direct Connect | ECS Task Networking (awsvpc) | Static IP, high performance | Limited to VPC, resource intensive |
| External Accessibility | Load Balancer | Easy to use, scalable | Additional costs, potential latency |
Additional Considerations
- Security: Ensure that the security groups and network ACLs are configured to allow the necessary traffic between your Kafka brokers and from clients.
- Monitoring and Logging: Use AWS CloudWatch for monitoring the performance and logs of Kafka brokers. This helps in troubleshooting and ensuring optimal performance.
In conclusion, managing advertised.host on AWS ECS requires careful planning and consideration of the deployment architecture. Utilizing ECS service discovery, task networking, or a load balancer allows you to effectively manage the Kafka advertised listeners in a dynamically scaling environment.
Related reading
- kafka on kubernetes cannot produce/consume topics (ClosedChannelException, ErrorLoggingCallback)
- Kafka on kubernetes cluster with Istio
- Kafka on kubernetes cluster with Istio
- Kafka on Kubernetes multi-node
- kafka s3 sink connector crashed when It gets NULL data
- Kafka Static membership in AWS ECS
- Kafka on Masstransit
- Kafka only once consumption guarantee

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.