๐ŸŒฆ How I Deployed a Flask Weather App on AWS Using Docker, ECS, and ALB

Flowchart illustrating the deployment of a weather application to AWS using Docker, ECS, and ALB. It shows steps from pulling application code from GitHub, building and pushing Docker images to AWS ECR, creating and configuring ECS clusters and services, deploying applications from ECR, configuring AWS ALB, and routing traffic to make the application accessible.


Building is fun. Deploying it live? Even more satisfying โ€” especially when your app predicts the weather! โ˜๏ธ

In this post, Iโ€™ll walk you through how I deployed a simple Flask weather application to AWS using Docker, ECS (Elastic Container Service), and ALB (Application Load Balancer). I'll also share a common bug I hit and how I fixed it.


๐Ÿ› ๏ธ What I Built

A Flask web app that:

  • Takes a city name as input.

  • Calls the OpenWeatherMap API to get current weather + 5-day forecast.

  • Shows details like temperature, wind speed, and conditions.


๐Ÿงฑ Architecture Breakdown

Hereโ€™s the flow:

  1. Local Dev

    • Pulled code from GitHub.

    • Built a Docker image using a Dockerfile.

  2. Container Registry

    • Pushed the image to AWS Elastic Container Registry (ECR).
  3. ECS Deployment

    • Created an ECS Cluster.

    • Configured ECS service to pull the image from ECR.

    • Deployed the container.

  4. Traffic Management

    • Set up an Application Load Balancer.

    • Routed traffic from ALB to the ECS service.

    • Accessed the app via a public DNS URL.


๐Ÿš€ Step-by-Step Deployment Guide

1. Clone & Build the App

git clone https://github.com/your-repo/weather-app
cd weather-app
docker build -t weather-app .

2. Push to AWS ECR

aws ecr create-repository --repository-name weather-app
aws ecr get-login-password | docker login --username AWS --password-stdin <your-ecr-url>

docker tag weather-app:latest <your-ecr-url>/weather-app:latest
docker push <your-ecr-url>/weather-app:latest

3. ECS & ALB Setup

  • Use the AWS Console to:

    • Create a Cluster โ†’ EC2 or Fargate.

    • Create a Task Definition referencing your ECR image.

    • Configure Service to launch tasks.

    • Set up an ALB, add target group & listener.

Done right, AWS will assign a DNS URL like:

http://weather-app-alb-xxxxxxxxx.ap-south-1.elb.amazonaws.com/

๐Ÿง‘โ€๐Ÿ”ง The Bug I Faced (and Fixed)

โŒ Error:

KeyError: 0
lat = location_data[0]['lat']

๐Ÿง  Root Cause:

The OpenWeather Geocoding API didnโ€™t return data for the city entered, so location_data[0] crashed.

โœ… Fix:

Wrap the response parsing in a try-except block:

location_data = location_response.json()
if not location_data or 'lat' not in location_data[0]:
    return redirect(url_for("error"))
lat = location_data[0]['lat']

Also added logs to debug:

print("Geo API Response:", location_response.status_code, location_response.text)

๐Ÿ’ก Final Thoughts

This was a great hands-on way to learn:

  • Docker image creation.

  • ECR, ECS, and container orchestration.

  • Load balancing using ALB.

  • Debugging real-world deployment bugs.

Next up: Auto-scaling and HTTPS using ACM!


๐Ÿ”— Useful Resources

0
Subscribe to my newsletter

Read articles from Anirban Banerjee directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anirban Banerjee
Anirban Banerjee