Managing APIs efficiently is essential, especially when dealing with real-time data like sports scores. Last week we were given a project by #DevopsAllStarChallenge to create a Containerized Sports API Management System.

In this post, I’ll explain how I built the system to fetch real-time sports data using AWS services, creating a container Docker, and creating our own customized API using API Gateway.

Why Containerization?

Containerization is the process of packaging your app and its dependencies into a single unit, called a container, that can run consistently across different environments. For this project, containerization ensures that our API runs smoothly and can be easily managed and scaled.

I used Docker to containerize the dependencies and library using Python, and AWS Fargate to run the containers without managing the underlying servers. This makes the whole system easier to handle.

The Goal

The goal was to:

  • Fetch real-time sports data (in this case, NFL data).
  • Containerize the query from the API and create a customised API through AWS Gateway.
  • Ensure the system is scalable and can handle increasing traffic.
  • Use AWS services like ECR, ECS, and Fargate to manage the application.

Explanation of Technical Architecture

The diagram above shows how a user accesses sports data using AWS services. Here’s how it works:

  1. User Login & Request:
    • The user logs into their AWS account and requests sports data.
  2. API Gateway:
    • The request first goes through Amazon API Gateway, which manages and secures API traffic.
  3. Load Balancer:
    • The request is sent to an Application Load Balancer (ALB), which distributes it to available servers.
  4. Containers:
    • The ALB forwards the request to one of the containers (small independent services running the application).
    • The container processes the request and fetches data from an external API.
  5. Response Back to User:
    • The external API sends the data back to the container.
    • The container sends it to the Load Balancer, then to the API Gateway, and finally back to the user.

This architecture ensures scalability, security, and efficient load balancing while integrating with external APIs.

Tech Stack

Here’s a quick overview of the tools and services used in this project:

  • Docker: For containerizing the application.
  • AWS ECR (Elastic Container Registry): To store Docker images.
  • AWS ECS (Elastic Container Service): To run and manage the containers.
  • AWS Fargate: To run the containers in a serverless environment.
  • Amazon API Gateway: To expose the API securely.
  • AWS ALB (Application Load Balancer): To distribute incoming traffic across containers.
  • SerpAPI: The service used to fetch real-time NFL data.

Step-by-Step Guide

1. Setting Up Docker

The first step was to set up Docker and create a Dockerfile for the application. The Dockerfile defines the environment needed to run the app and install dependencies.

Here’s the Dockerfile:

FROM python:3.9-slim

Set the working directory inside the container
- WORKDIR /app

Copy the requirements file into the container
-COPY requirements.txt requirements.txt

Install dependencies
-RUN pip install -r requirements.txt

Copy all files from the current directory into the container
- COPY . .

Expose the port your app runs on
- EXPOSE 8080

Command to run the application
- CMD ["python", "app.py"]

2. Building and Pushing the Docker Image

After setting up the Dockerfile, I built the Docker image using:

docker build -t sports-api .

Next, I pushed the image to Amazon ECR:

aws ecr create-repository --repository-name sports-api
docker tag sports-api:latest <aws_account_id>.dkr.ecr.<region>.amazonaws.com/sports-api:latest
docker push <aws_account_id>.dkr.ecr.<region>.amazonaws.com/sports-api:latest

3. Deploying with AWS ECS and Fargate

Once the image was pushed to ECR, I used Amazon ECS to manage the containers. I also used AWS Fargate to run the containers without needing to manage the underlying servers.

I created a task definition in ECS to specify how the containers should be run, including the image and port.

4. Load Balancing with ALB

To handle traffic efficiently, I used AWS ALB (Application Load Balancer). ALB helps distribute incoming requests evenly across containers, ensuring the system can handle more traffic without issues.

5. Setting Up the API Gateway

Next, I set up Amazon API Gateway to expose the API securely. It acts as a front door to the application, allowing us to control access and manage traffic.

I created a customized REST API.

6. Querying Sports Data

For the sports data, I used SerpAPI to fetch NFL information.

7. Running the System

Once everything was set up, I deployed the containers using ECS Fargate, set up ALB to handle traffic and created an AWS Gateway API. The system was now ready to serve real-time sports data to users.

Conclusion

The Containerized Sports API Management System was a great project that helped me learn how to use Docker, AWS Fargate, and other AWS services to build a scalable system.

The key takeaway is the power of containerization and serverless computing. With these tools, we can focus on the code while AWS handles the infrastructure.

If you found this blog post helpful, share it with others who may find it interesting and the link to the specific project repo can be found here: GitHub

Stay updated with my projects by following me on Twitter, LinkedIn and GitHub.

Thank you for reading.

Author Of article : Ijay Read full article