Skip to content

maccevedor/Python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Interview Project - FastAPI Backend with AWS Services

A comprehensive interview project demonstrating a production-ready microservices architecture using Python, FastAPI, AWS SQS, Lambda, PostgreSQL, and Docker.

πŸ—οΈ Architecture Overview

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   FastAPI   │─────▢│   AWS SQS    │─────▢│AWS Lambda   β”‚
β”‚   Backend   β”‚      β”‚    Queue     β”‚      β”‚  Function   β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
       β”‚                                            β”‚
       β”‚            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”               β”‚
       └───────────▢│  PostgreSQL  β”‚β—€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                    β”‚   Database   β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Components

  • FastAPI Backend: RESTful API for task management
  • PostgreSQL: Relational database for persistent storage
  • AWS SQS: Message queue for asynchronous task processing
  • AWS Lambda: Serverless function for processing tasks
  • Docker: Containerization for easy deployment
  • LocalStack: Local AWS services emulation for development

πŸš€ Features

  • βœ… RESTful API with FastAPI
  • βœ… PostgreSQL database with SQLAlchemy ORM
  • βœ… Asynchronous task processing with SQS
  • βœ… AWS Lambda function for message processing
  • βœ… Docker Compose for local development
  • βœ… Pydantic models for data validation
  • βœ… Comprehensive test suite
  • βœ… Health check endpoints
  • βœ… Environment-based configuration
  • βœ… Database migrations with Alembic

πŸ“‹ Prerequisites

  • Docker and Docker Compose
  • Python 3.11+ (for local development)
  • AWS Account (for production deployment)
  • Git

πŸ› οΈ Installation & Setup

1. Clone the Repository

git clone <repository-url>
cd python

2. Environment Configuration

Create a .env file from the example:

cp .env.example .env

Edit .env with your configuration:

DATABASE_URL=postgresql://postgres:postgres@db:5432/interview_db
AWS_REGION=us-east-1
AWS_ACCESS_KEY_ID=your_access_key
AWS_SECRET_ACCESS_KEY=your_secret_key
SQS_QUEUE_URL=https://sqs.us-east-1.amazonaws.com/123456789/interview-queue
ENVIRONMENT=development

3. Start the Application

# Start all services
docker-compose up -d

# View logs
docker-compose logs -f

# Check service status
docker-compose ps

4. Initialize LocalStack (Development)

# Make the init script executable
chmod +x localstack/init-aws.sh

# The script runs automatically when LocalStack starts
# Or run manually:
docker-compose exec localstack /etc/localstack/init/ready.d/init-aws.sh

5. Access the Application

πŸ“š API Endpoints

Tasks

Method Endpoint Description
GET / Root endpoint with API info
GET /health Health check
POST /tasks Create a new task
GET /tasks List all tasks (with pagination)
GET /tasks/{task_id} Get a specific task
PUT /tasks/{task_id} Update a task
DELETE /tasks/{task_id} Delete a task
POST /tasks/{task_id}/process Manually trigger task processing

Example Requests

Create a Task

curl -X POST "http://localhost:8000/tasks" \
  -H "Content-Type: application/json" \
  -d '{
    "title": "Process user data",
    "description": "Extract and transform user information"
  }'

List Tasks

curl "http://localhost:8000/tasks?skip=0&limit=10"

Get Task by ID

curl "http://localhost:8000/tasks/1"

Update Task

curl -X PUT "http://localhost:8000/tasks/1" \
  -H "Content-Type: application/json" \
  -d '{
    "title": "Updated title",
    "status": "completed"
  }'

Delete Task

curl -X DELETE "http://localhost:8000/tasks/1"

πŸ§ͺ Testing

Run Tests

# Install dependencies
pip install -r requirements.txt

# Run all tests
pytest

# Run with coverage
pytest --cov=app --cov-report=html

# Run specific test file
pytest tests/test_api.py

# Run with verbose output
pytest -v

Test Coverage

The test suite includes:

  • Unit tests for API endpoints
  • Integration tests with database
  • Validation tests for Pydantic models
  • Error handling tests

πŸ—„οΈ Database Management

Migrations with Alembic

# Initialize Alembic (already done)
alembic init alembic

# Create a new migration
alembic revision --autogenerate -m "description"

# Apply migrations
alembic upgrade head

# Rollback migration
alembic downgrade -1

# View migration history
alembic history

Database Access

# Connect to PostgreSQL
docker-compose exec db psql -U postgres -d interview_db

# Common SQL commands
\dt              # List tables
\d tasks         # Describe tasks table
SELECT * FROM tasks;

πŸ”§ Development

Project Structure

.
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ main.py           # FastAPI application
β”‚   β”œβ”€β”€ config.py         # Configuration management
β”‚   β”œβ”€β”€ database.py       # Database connection
β”‚   β”œβ”€β”€ models.py         # SQLAlchemy models
β”‚   β”œβ”€β”€ schemas.py        # Pydantic schemas
β”‚   └── sqs_client.py     # AWS SQS client
β”œβ”€β”€ lambda/
β”‚   β”œβ”€β”€ lambda_function.py # Lambda handler
β”‚   β”œβ”€β”€ requirements.txt   # Lambda dependencies
β”‚   └── Dockerfile         # Lambda container
β”œβ”€β”€ localstack/
β”‚   └── init-aws.sh       # LocalStack initialization
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ conftest.py       # Test configuration
β”‚   └── test_api.py       # API tests
β”œβ”€β”€ docker-compose.yml    # Docker services
β”œβ”€β”€ Dockerfile            # API container
β”œβ”€β”€ requirements.txt      # Python dependencies
β”œβ”€β”€ .env.example          # Environment template
β”œβ”€β”€ .gitignore
β”œβ”€β”€ INTERVIEW_QUESTIONS.md # Interview questions
└── README.md

Local Development Without Docker

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Set environment variables
export DATABASE_URL="postgresql://postgres:postgres@localhost:5432/interview_db"
export AWS_REGION="us-east-1"
# ... other variables

# Run the application
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000

🚒 Production Deployment

AWS Deployment

1. Deploy Lambda Function

# Build Lambda Docker image
cd lambda
docker build -t interview-lambda .

# Tag and push to ECR
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <account-id>.dkr.ecr.us-east-1.amazonaws.com
docker tag interview-lambda:latest <account-id>.dkr.ecr.us-east-1.amazonaws.com/interview-lambda:latest
docker push <account-id>.dkr.ecr.us-east-1.amazonaws.com/interview-lambda:latest

# Create Lambda function
aws lambda create-function \
  --function-name interview-processor \
  --package-type Image \
  --code ImageUri=<account-id>.dkr.ecr.us-east-1.amazonaws.com/interview-lambda:latest \
  --role arn:aws:iam::<account-id>:role/lambda-execution-role

2. Create SQS Queue

# Create queue
aws sqs create-queue --queue-name interview-queue

# Configure Lambda trigger
aws lambda create-event-source-mapping \
  --function-name interview-processor \
  --event-source-arn arn:aws:sqs:us-east-1:<account-id>:interview-queue \
  --batch-size 10

3. Deploy API (ECS/Fargate or EC2)

# Build and push API image
docker build -t interview-api .
docker tag interview-api:latest <account-id>.dkr.ecr.us-east-1.amazonaws.com/interview-api:latest
docker push <account-id>.dkr.ecr.us-east-1.amazonaws.com/interview-api:latest

# Deploy to ECS (example)
# Create task definition, service, and configure load balancer

4. RDS PostgreSQL Setup

# Create RDS instance
aws rds create-db-instance \
  --db-instance-identifier interview-db \
  --db-instance-class db.t3.micro \
  --engine postgres \
  --master-username postgres \
  --master-user-password <password> \
  --allocated-storage 20

πŸ”’ Security Considerations

  • βœ… Use AWS Secrets Manager for sensitive credentials
  • βœ… Implement JWT authentication for API endpoints
  • βœ… Enable HTTPS/TLS in production
  • βœ… Use IAM roles instead of access keys
  • βœ… Enable VPC for database and Lambda
  • βœ… Implement rate limiting
  • βœ… Regular security updates
  • βœ… Input validation with Pydantic
  • βœ… SQL injection prevention with ORM

πŸ“Š Monitoring & Logging

CloudWatch Integration

# Add to Lambda function
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)

# Add to FastAPI
import logging
logging.basicConfig(level=logging.INFO)

Metrics to Monitor

  • API response times
  • Database connection pool
  • SQS queue depth
  • Lambda execution duration
  • Error rates
  • Task processing success/failure rates

πŸ› Troubleshooting

Common Issues

1. Database Connection Failed

# Check if database is running
docker-compose ps db

# Check database logs
docker-compose logs db

# Restart database
docker-compose restart db

2. SQS Connection Issues

# Check LocalStack logs
docker-compose logs localstack

# Verify queue exists
docker-compose exec localstack awslocal sqs list-queues

# Recreate queue
docker-compose exec localstack awslocal sqs create-queue --queue-name interview-queue

3. Lambda Not Processing Messages

  • Check Lambda logs in CloudWatch
  • Verify SQS trigger is configured
  • Check IAM permissions
  • Verify database connectivity from Lambda

4. API Container Won't Start

# Check logs
docker-compose logs api

# Rebuild container
docker-compose build api
docker-compose up -d api

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“ Interview Questions

See INTERVIEW_QUESTIONS.md for comprehensive interview questions covering:

  • Python & FastAPI
  • PostgreSQL & SQLAlchemy
  • AWS Services (SQS, Lambda)
  • Docker & DevOps
  • System Design & Architecture
  • Practical Coding Tasks
  • Debugging Scenarios

πŸ“„ License

This project is created for interview and educational purposes.

πŸ”— Resources

πŸ“§ Contact

For questions or feedback about this project, please open an issue in the repository.


Happy Coding! πŸš€

About

SQS LAMBDA POSTGRESQL

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published