Building a Containerized Sports API Management System with AWS

Building a Containerized Sports API Management System with AWS

Project Overview

The Containerized Sports API Management System is a cloud-based solution designed to expose real-time sports data through a REST API. It leverages Amazon ECS (Fargate) for containerized application hosting, Amazon API Gateway for managing API endpoints, and an external Sports API for retrieving sports data. This project highlights best practices in container orchestration, API management, and secure AWS integrations.


Project Features

✅ Exposes a REST API for real-time sports data retrieval
✅ Runs a containerized backend using Amazon ECS with Fargate
✅ Implements a scalable and serverless architecture
✅ Manages API routing via Amazon API Gateway


Prerequisites

Before starting, ensure you have the following:

  • Sports API Key: Sign up for a free account at SerpAPI to obtain an API key.

  • AWS Account: Set up an AWS account and understand basic concepts of ECS, API Gateway, Docker, and Python.

  • AWS CLI Installed & Configured: Install and configure the AWS CLI to interact with AWS.

  • SerpAPI Library: Install the SerpAPI library using:

      pip install google-search-results
    
  • Docker CLI & Desktop: Install Docker to build and push container images.


Technologies Used

  • Cloud Provider: AWS

  • Core Services: Amazon ECS (Fargate), API Gateway, CloudWatch

  • Programming Language: Python 3.x

  • Containerization: Docker

  • Security: IAM roles with least privilege access


Project Structure

sports-api-management/
├── app.py                # Flask application for querying sports data
├── Dockerfile            # Dockerfile to containerize the Flask app
├── requirements.txt      # Python dependencies
├── .gitignore
└── README.md             # Project documentation

Setup Instructions

Step 1: Clone the Repository

git clone https://github.com/Faoziyah/30_days_devops_challenge.git
cd DevOps_Challenge_Day4/containerized-sports-api

Step 2: Create an ECR Repository

aws ecr create-repository --repository-name sports-api --region us-east-1

Step 3: Build and Push the Docker Image

Authenticate Docker with AWS:

aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <AWS_ACCOUNT_ID>.dkr.ecr.us-east-1.amazonaws.com

Build and tag the image:

docker build --platform linux/amd64 -t sports-api .
docker tag sports-api:latest <AWS_ACCOUNT_ID>.dkr.ecr.us-east-1.amazonaws.com/sports-api:sports-api-latest

Push the image to ECR:

docker push <AWS_ACCOUNT_ID>.dkr.ecr.us-east-1.amazonaws.com/sports-api:sports-api-latest

Step 4: Deploy to Amazon ECS (Fargate)

Create an ECS Cluster

  1. Open AWS Management Console → Search for ECS

  2. Click Clusters → Create New Cluster

  3. Name the cluster (e.g., sports-api-cluster)

  4. Select Fargate and click Create Cluster

Create a Task Definition

  1. Navigate to Task Definitions → Create New Task Definition

  2. Name it (e.g., sports-api-task)

  3. Select AWS Fargate

  4. Add a container:

  5. Add Environment Variables:

    • SPORTS_API_KEY: <YOUR_SPORTS_API_KEY>

Deploy the Service with an ALB

  1. Go to Clusters → Select sports-api-clusterCreate Service

  2. Select Fargate as capacity provider

  3. Choose sports-api-task

  4. Set desired tasks to 2

  5. Create a new Security Group with Inbound Rule: All TCP, Source: Anywhere

  6. Use Application Load Balancer (ALB)

    • Listener: HTTP (Port 80)

    • Health Check Path: /sports

  7. Click Create Service

Verify the ALB by copying the DNS name and accessing:

http://sports-api-alb-<AWS_ACCOUNT_ID>.us-east-1.elb.amazonaws.com/sports

Step 5: Set Up API Gateway

  1. Open AWS API Gateway ConsoleCreate APIREST API

  2. Name the API (e.g., Sports API)

  3. Create a Resource: /sports

  4. Create a GET Method:

    • Integration Type: HTTP Proxy

    • HTTP Method: GET

    • Endpoint: ALB DNS + ``

  5. Deploy API to prod stage

  6. Copy the Invoke URL and test:

curl https://<api-gateway-id>.execute-api.us-east-1.amazonaws.com/prod/sports

Cleaning Up AWS Resources

To avoid unnecessary costs, delete the resources:

aws ecr delete-repository --repository-name sports-api --force
aws ecs delete-cluster --cluster sports-api-cluster
aws elbv2 delete-load-balancer --load-balancer-arn <ALB_ARN>
aws apigateway delete-rest-api --rest-api-id <API_ID>

Lessons Learned

Setting up a scalable, containerized API with ECS and API Gateway
Deploying applications using AWS Fargate without managing EC2 instances
Managing API requests efficiently using AWS services


Future Enhancements

🔹 Implement caching with Amazon ElastiCache for frequent API requests
🔹 Use DynamoDB to store user-specific queries and preferences
🔹 Secure API Gateway with IAM-based authentication or API keys
🔹 Automate CI/CD for container deployments


Conclusion

This project provided hands-on experience in container orchestration, API management, and cloud-native deployments. With AWS services like ECS, API Gateway, and ALB, we successfully built a scalable and secure API management system for sports data. 🚀


Let's Connect!

Follow me for more DevOps and cloud-native insights! Feel free to share your thoughts and improvements.

#DevOps #AWS #Containerization #APIManagement #ECS #Fargate #Docker