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

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:
Local Dev
Pulled code from GitHub.
Built a Docker image using a
Dockerfile
.
Container Registry
- Pushed the image to AWS Elastic Container Registry (ECR).
ECS Deployment
Created an ECS Cluster.
Configured ECS service to pull the image from ECR.
Deployed the container.
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
๐ AWS ECS Docs
๐ณ Docker Basics
๐ Flask Official Docs
Subscribe to my newsletter
Read articles from Anirban Banerjee directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
