A simple asynchronous weather API service built with FastAPI. It fetches weather data from an external API, caches results, stores weather data in AWS S3, and logs events in AWS DynamoDB.
- Fetch current weather data for a given city.
- Cache weather data to avoid redundant external API calls.
- Store weather data in AWS S3 as JSON files.
- Log events (city, timestamp, S3 URL) in AWS DynamoDB.
- Fully asynchronous implementation with FastAPI and
boto3
.
- Python 3.13
- Docker and Docker Compose installed
- AWS account with access to S3 and DynamoDB
- API key for OpenWeatherMap (or a similar external API)
-
Clone the Repository:
git clone <repository-url> cd weather-api-service
-
Create a Virtual Environment:
python -m venv venv source venv/bin/activate # Linux/Mac venv\Scripts\activate # Windows
-
Install Dependencies:
pip install -r requirements.txt
-
Set Up Environment Variables:
Create a.env
file in the root directory with the following content:API_KEY=your_openweather_api_key AWS_ACCESS_KEY_ID=your_aws_access_key AWS_SECRET_ACCESS_KEY=your_aws_secret_key AWS_REGION=your_aws_region S3_BUCKET_NAME=your_s3_bucket_name LOCAL_STORAGE_PATH=./data
-
Run the Application:
uvicorn app.main:app --reload
-
Access the API:
Open your browser or use an HTTP client like Postman to test the endpoint:http://localhost:8000/weather?city=London
-
Build the Docker Image:
docker build -t weather-api-service .
-
Run the Container:
docker run -d -p 8000:8000 --env-file .env weather-api-service
-
Test the API:
Access the endpoint:http://localhost:8000/weather?city=London
-
Create a
docker-compose.yml
File:
Ensure thedocker-compose.yml
file is in the root directory:version: '3.8' services: weather-api: build: . ports: - "8000:8000" env_file: .env volumes: - .:/app
-
Run the Service:
docker-compose up -d
-
Test the API:
Access the endpoint:http://localhost:8000/weather?city=London
-
Stop the Service:
docker-compose down
- GET /weather
Query Parameters:city
(required): Name of the city to fetch weather data for.
http://localhost:8000/weather?city=New York
-
Missing AWS Credentials: Ensure your
.env
file is correctly populated withAWS_ACCESS_KEY_ID
,AWS_SECRET_ACCESS_KEY
, andAWS_REGION
. -
S3/DynamoDB Errors: Verify the S3 bucket and DynamoDB table exist. You can create the DynamoDB table using the provided
scripts/create_table.py
. -
Docker Issues: Ensure Docker and Docker Compose are installed and running:
docker --version docker-compose --version
-
Dependency Issues: Reinstall dependencies:
pip install --force-reinstall -r requirements.txt
weather-api-service/
├── app/
│ ├── __init__.py
│ ├── main.py
│ ├── routes/
│ │ ├── __init__.py
│ │ ├── weather.py
│ ├── services/
│ │ ├── __init__.py
│ │ ├── weather_service.py
│ │ ├── storage_service.py
│ │ ├── db_service.py
│ ├── utils/
│ │ ├── __init__.py
│ │ ├── cache.py
│ │ ├── config.py
├── scripts/
│ ├── create_table.py
├── .env
├── requirements.txt
├── Dockerfile
├── docker-compose.yml
├── README.md
This project is licensed under the MIT License.