Skip to main content
Welcome back. In the previous lesson we provisioned a VM on Amazon Elastic Compute Cloud (EC2), installed Kafka (using the Raft protocol), created an IAM role, launched an EC2 instance, and created the cartevent topic. In this article we’ll build a simple front-end that produces order events to the Kafka cluster and verify those events on the broker. Below you’ll find the project layout, installation steps, the complete Flask app that produces events, how to run the UI, check logs, and validate messages on the Kafka broker.

Project layout

Open a terminal, switch into the project folder, and install dependencies:
Requirements (contents of requirements.txt):
Install them:

Flask app — producing order events to Kafka

Open app.py in your editor. The example below shows the essential, corrected parts of the Flask application: imports, logging, Kafka producer configuration, a delivery callback, sample product data, routes for cart operations, and the handler that produces an order event to the cartevent topic.
Be sure to replace 54.226.13.161:9092 in the bootstrap.servers configuration with the public IP of your Kafka Amazon Elastic Compute Cloud (EC2) instance and the correct port. If broker is not reachable, the producer will fail to connect.

Updating the bootstrap server

If your frontend cannot connect to Kafka, confirm the bootstrap.servers setting in app.py uses the EC2 instance’s public IPv4 address and the Kafka listener port (default 9092). You can copy the public IP from the EC2 console and update the configuration accordingly.
The image shows the AWS EC2 management console with an instance named "kafka_demo" that is currently running. The instance details include its instance ID, type, public IPv4 address, and other network information.

Running the frontend and placing an order

Start the Flask app:
Open the Toy Shop in a browser (http://localhost:5000 if running locally). The UI shows a list of products. Add a couple of toys to the cart, view the cart, fill in name and address, and click Place Order. The app prepares an order object, serializes it to JSON, and produces it to the cartevent topic.
The image shows a webpage for "KodeKloud Toy Shop" displaying six toys with their prices and an option to add them to the cart.
After placing the order you should see a confirmation page:
The image shows an order confirmation page thanking a user named Raghu, with a link to return to the shop.

Checking the application logs

The Flask app logs HTTP requests as well as producer activity. If message delivery succeeds, the delivery callback logs the metadata (topic, partition, offset, timestamp). Example local logs:

Consuming messages on the Kafka broker

On the Kafka EC2 instance you can create the cartevent topic (if not already created) and consume messages to verify they were produced by the frontend.
Example consumer output showing two orders placed from the frontend:

Next steps

With the Toy Shop producing order events to the cartevent topic, a logical next step is to implement the Warehouse UI (an internal consumer dashboard). That dashboard can consume cartevent messages and present orders to warehouse staff for picking, packing, and shipping.

Watch Video