Skip to content

moayad-khader/fastapi-azure-functions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

15 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Azure Functions FastAPI Microservices Boilerplate

A comprehensive boilerplate for building microservices using Azure Functions and FastAPI with Docker Compose support.

πŸ—οΈ Architecture

This project consists of two main microservices with shared components:

  • Auth Service (Port 8001) - Authentication and authorization with API versioning
  • Organization Service (Port 8002) - Organization management with Service Bus integration
  • API Gateway (Port 8003) - Centralized documentation and service aggregation
  • Shared Components - Common utilities, authentication, and core functionality
  • Infrastructure - Azure Bicep templates for cloud deployment
  • Nginx Proxy - Reverse proxy for service routing and load balancing

πŸš€ Quick Start with Docker Compose

Prerequisites

  • Docker and Docker Compose installed
  • Git (for cloning the repository)

Running the Services

  1. Clone the repository (if not already done):

    git clone <repository-url>
    cd azure_functions_fastapi_microservices_boilerplate
  2. Build and start all services:

    docker-compose up --build
  3. Start services in detached mode:

    docker-compose up -d --build
  4. View logs:

    # All services
    docker-compose logs -f
    
    # Specific service
    docker-compose logs -f auth-service
  5. Stop services:

    docker-compose down

Service Endpoints

Direct Service Access:

Via Nginx Reverse Proxy:

πŸ› οΈ Development

Local Development with Docker Compose

The Docker Compose setup includes volume mounts for hot reloading during development:

# Start in development mode
docker-compose up --build

# Make changes to your code - the services will automatically reload

Individual Service Development

You can also run individual services:

# Build and run only auth service
docker-compose up --build auth-service

# Run specific services
docker-compose up auth-service organization-service

Environment Variables

Create .env files for each service to customize configuration:

# services/auth-service/.env
DEBUG=true
LOG_LEVEL=debug
JWT_SECRET_KEY=your-secret-key-change-in-production
ACCESS_TOKEN_EXPIRE_MINUTES=30
REFRESH_TOKEN_EXPIRE_DAYS=7

# services/organization-service/.env
DEBUG=true
COSMOS_DB_ENDPOINT=your-cosmos-endpoint
AZURE_SERVICE_BUS_CONNECTION_STRING=your-service-bus-connection-string

πŸ“¦ Docker Commands

Useful Docker Compose Commands

# Build without cache
docker-compose build --no-cache

# View running containers
docker-compose ps

# Execute commands in running containers
docker-compose exec auth-service bash

# View resource usage
docker-compose top

# Remove all containers and networks
docker-compose down --remove-orphans

# Remove containers, networks, and volumes
docker-compose down -v

Individual Docker Commands

# Build individual service
docker build -t auth-service ./services/auth-service

# Run individual container
docker run -p 8001:8000 auth-service

πŸ› οΈ Makefile Commands

This project includes a Makefile for common operations:

# View all available commands
make help

# Build all Docker images
make build

# Start all services in detached mode
make up

# Stop all services
make down

# Restart all services
make restart

# View logs from all services
make logs

# Clean up everything (containers, networks, volumes)
make clean

# Run health checks on all services
make test

# Check health status
make health

# Development mode (with logs)
make dev

πŸ”§ Troubleshooting

Common Issues

  1. Port conflicts: Make sure ports 8001, 8002, 8003, and 80 are available
  2. Build failures: Try docker-compose build --no-cache
  3. Permission issues: Ensure Docker has proper permissions

Health Checks

All services include health checks. Monitor service health:

# Check health status
docker-compose ps

# View health check logs
docker-compose logs nginx

Debugging

# Access service logs
docker-compose logs -f [service-name]

# Access service shell
docker-compose exec [service-name] bash

# Inspect service configuration
docker-compose config

☁️ Azure Deployment

This project includes Azure Bicep templates for cloud deployment:

Infrastructure Components

  • Azure Functions - Serverless compute for microservices
  • Azure Cosmos DB - NoSQL database for data storage
  • Azure Service Bus - Message queuing for service communication
  • Azure API Management - API gateway and management

Deployment Steps

  1. Prerequisites:

    # Install Azure CLI
    az login
    az account set --subscription "your-subscription-id"
  2. Deploy Infrastructure:

    # Deploy using Bicep templates
    az deployment group create \
      --resource-group your-rg \
      --template-file infrastructure/main.bicep \
      --parameters @infrastructure/parameters.json
  3. Deploy Functions:

    # Deploy auth service
    cd services/auth-service
    func azure functionapp publish your-auth-function-app
    
    # Deploy organization service
    cd ../organization-service
    func azure functionapp publish your-org-function-app

Production Considerations

  • Environment-specific configurations
  • Azure Key Vault for secrets management
  • Application Insights for monitoring
  • Azure Front Door for load balancing
  • Security hardening and compliance

πŸ“ API Documentation

πŸš€ Unified Documentation (NEW!)

Access all microservices APIs in one place: http://localhost/docs

The new API Gateway provides:

  • Centralized Swagger UI with all microservices APIs
  • Service switching between individual and unified views
  • Real-time API aggregation from all running services
  • Health monitoring and service discovery
  • Professional documentation interface with service tagging

Individual Service Documentation

Each service also provides its own interactive API documentation:

πŸ’‘ Tip: Use the unified documentation at http://localhost/docs for the best experience!

πŸ—οΈ Project Structure

azure_functions_fastapi_microservices_boilerplate/
β”œβ”€β”€ services/                    # Microservices
β”‚   β”œβ”€β”€ auth-service/           # Authentication service
β”‚   β”‚   β”œβ”€β”€ src/               # Source code
β”‚   β”‚   β”œβ”€β”€ HttpTrigger/       # Azure Functions trigger
β”‚   β”‚   β”œβ”€β”€ Dockerfile         # Container configuration
β”‚   β”‚   └── requirements.txt   # Python dependencies
β”‚   β”œβ”€β”€ organization-service/  # Organization management service
β”‚   β”‚   β”œβ”€β”€ src/               # Source code
β”‚   β”‚   β”œβ”€β”€ HttpTrigger/       # Azure Functions HTTP trigger
β”‚   β”‚   β”œβ”€β”€ ServiceBusListener/ # Service Bus trigger
β”‚   β”‚   β”œβ”€β”€ Dockerfile         # Container configuration
β”‚   β”‚   └── requirements.txt   # Python dependencies
β”‚   └── shared/                # Shared components
β”‚       β”œβ”€β”€ authentication/    # Auth utilities
β”‚       β”œβ”€β”€ core/             # Core utilities
β”‚       └── utils/            # Common utilities
β”œβ”€β”€ infrastructure/                     # Infrastructure as Code
β”‚   β”œβ”€β”€ main.bicep            # Main Bicep template
β”‚   └── modules/              # Bicep modules
β”‚       β”œβ”€β”€ apim.bicep        # API Management
β”‚       β”œβ”€β”€ cosmos.bicep      # Cosmos DB
β”‚       └── servicebus.bicep  # Service Bus
β”œβ”€β”€ docs/                     # Documentation
β”‚   β”œβ”€β”€ architecture.md      # Architecture overview
β”‚   └── adr/                 # Architecture Decision Records
β”œβ”€β”€ docker-compose.yml        # Docker Compose configuration
β”œβ”€β”€ nginx.conf               # Nginx reverse proxy config
└── Makefile                 # Development commands

πŸ”§ Features

Authentication Service

  • API Versioning - Support for v1 and v2 APIs
  • JWT Authentication - Secure token-based auth
  • User Management - Registration, login, profile management
  • Password Security - Hashing, reset, change functionality
  • Role-based Access Control - Admin and user roles

Organization Service

  • CRUD Operations - Full organization management
  • Service Bus Integration - Asynchronous message processing
  • Event-driven Architecture - Reactive service design

Shared Components

  • Common Authentication - Reusable auth utilities
  • Core Utilities - Shared business logic
  • Standardized Responses - Consistent API responses

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes following the project structure
  4. Add tests for new functionality
  5. Test with Docker Compose (make test)
  6. Commit your changes (git commit -m 'Add amazing feature')
  7. Push to the branch (git push origin feature/amazing-feature)
  8. Submit a pull request

Development Guidelines

  • Follow the established project structure
  • Use type hints in Python code
  • Write comprehensive tests
  • Update documentation for new features
  • Follow PEP 8 coding standards
  • Use meaningful commit messages

πŸ“š Additional Resources


Built with ❀️ using FastAPI, Azure Functions, and Docker

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published