DEV Community

Taylor D Jones
Taylor D Jones

Posted on

Tackling the Cloud ☁️ Building a Containerized Sports API with AWS ECS and API Gateway

This was one of the most challenging projects I've ever worked on. When I started to build the containerized API management system for sports data, I thought, "How hard could it be?" Spoiler alert: It was like trying to win the Super Bowl without knowing the playbook. But guess what? After fumbling through tasks, battling circuit breakers, and chasing down rogue environment variables, I emerged victorious. Here’s the play-by-play of my adventure with Amazon ECS (Fargate), API Gateway, and a sprinkle of Docker magic.

🏈 Kickoff: The Game Plan

The goal was simple: create a REST API that pulls real-time sports data, containerize it with Docker, deploy it on AWS ECS with Fargate, and expose it through API Gateway. Sounds easy, right? Like a rookie quarterback, I had no idea the defense (aka AWS errors) I was about to face.

⚙️ The Tech Stack Draft

AWS Services: ECS (Fargate), API Gateway, CloudWatch

Programming Language: Python 3.x with Flask

Containerization: Docker

External Data: SerpAPI for sports data

🚀 First Quarter: Building the API

Writing the Flask API was the easy part. I whipped up an endpoint /sports to fetch NFL schedules from SerpAPI. It worked perfectly on my local machine. I felt like an MVP.

@app.route('/sports', methods=['GET'])
def get_nfl_schedule():
    # Fetches the NFL schedule from SerpAPI
    try:
        params = {
            "engine": "google",
            "q": "nfl schedule",
            "api_key": SERP_API_KEY
        }
        response = requests.get(SERP_API_URL, params=params)
        data = response.json()
        return jsonify(data), 200
    except Exception as e:
        return jsonify({"error": str(e)}),
Enter fullscreen mode Exit fullscreen mode

.... but then came the deployment.

💥 Second Quarter: The Fargate Fumble

Deploying to ECS was like facing the 1985 Chicago Bears defense. My containers wouldn't start, tasks kept stopping, and AWS threw errors like confetti:

Exit Code 137: "Out of memory? But I gave you 2GB, why are you still hungry?"

Circuit Breaker Thresholds: ECS decided, "You’re done here, buddy," and kept rolling back my services.

Image Pull Errors: "CannotPullContainerError: manifest does not contain descriptor matching platform 'linux/amd64'."

🏃‍♂️ Halftime Adjustments: Learning from the Fumbles

Memory Issues? Increased the task memory to 4GB. Boom. No more out-of-memory errors.

Circuit Breaker Drama? Disabled it temporarily to figure out the root issues.

Docker Image Errors? Rebuilt the Docker image with the --platform linux/amd64 flag. ECS finally stopped being picky.

🏆 Third Quarter: API Gateway Gets in the Game

Setting up API Gateway was smoother than a perfect touchdown pass:

  • Created a new REST API

  • Linked it to the Application Load Balancer (ALB) from ECS

  • Deployed to the prod stage

BUT... hitting the endpoint returned:

{
  "games": [],
  "message": "No NFL schedule available."
}
Enter fullscreen mode Exit fullscreen mode

Cue dramatic music.

😤 Fourth Quarter: The Debugging Blitz

Turns out, the API key I set as an environment variable was misnamed. I had SPORTS_API_KEY in ECS but SERP_API_KEY in my code. Rookie mistake. I updated the task definition, redeployed the service, and...

TOUCHDOWN! 🏈

{
  "message": "NFL schedule fetched successfully.",
  "games": [
    {
      "away_team": "Kansas City Chiefs",
      "home_team": "Philadelphia Eagles",
      "venue": "Caesars Superdome",
      "date": "Sun, Feb 9, 6:30 PM",
      "time": "6:30 PM ET"
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

📚 Overtime: Lessons Learned

  • Memory Matters: Don’t underestimate resource allocation.

  • Naming Conventions Save Lives: Consistency with environment variables is key.

  • Logs Are Your Best Friend: CloudWatch helped me track down every sneaky error.

  • Docker Platforms: Specify the platform when building for ECS to avoid compatibility headaches.

💡 Final Thoughts: From Rookie to Pro

What started as a simple API project turned into a cloud computing masterclass. I battled ECS tasks, tamed API Gateway, and came out with a fully containerized, scalable sports API. So, if you’re diving into AWS, just remember:

It’s okay to fumble, as long as you recover the ball. 🙌

Ready to deploy your own sports API? Check out the project on GitHub!

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay