DeployFrame Docs

Backend Overview

Understanding the AI SaaS Boilerplate backend architecture and code structure

Backend Overview

The AI SaaS Boilerplate backend is built with Python Lambda functions organized into a clean, modular architecture. This section helps you understand the backend code structure and organization to enable future customization and extension.

Backend Architecture Principles

The backend follows separation of concerns with three main categories:

  • Business Logic: Core platform functionality (credits, payments, user management)
  • AI Services: Individual AI service implementations (document, image, video processing)
  • Shared Components: Common utilities and patterns used across all services

Each Lambda function is self-contained with its own dependencies, while shared utilities provide consistent patterns for authentication, credit management, and API parameter validation across all services.

Backend Code Structure

The backend is organized to mirror the CDK infrastructure, making it easy to understand the relationship between deployment code and business logic:

backend/
├── business/                      # Core platform business logic
│   ├── credits/                   # Credit system management
│   │   ├── credit_handling/       # Credit processing and distribution
│   │   │   └── lambda/           
│   │   └── service_pricing/       # AI service pricing logic
│   │       └── lambda/           
│   ├── payments/                  # Stripe payment integration
│   │   ├── payment_handling/      # Subscription and billing operations
│   │   │   └── lambda/           
│   │   ├── pricing/              # Public pricing API
│   │   │   └── lambda/           
│   │   └── webhook/              # Stripe webhook processing
│   │       └── lambda/           
│   └── user/                     # User lifecycle management
│       └── onboarding/           # New user setup and credit allocation
│           └── lambda/           
├── frontend/                     # Frontend-specific backend services
│   └── api/                     
│       └── user_settings/        # User preferences and profile management
│           └── lambda/           
└── services/                     # AI service implementations
    ├── document/                 # Document processing services  
    │   ├── markdown_conversion/  # Document format transformation
    │   │   └── lambda/          
    │   ├── ocr/                 # Text extraction from images
    │   │   └── lambda/          
    │   └── pdf_chat/            # Interactive document conversations
    │       └── functions/       
    ├── image/                   # Image processing services
    │   ├── generation/          # AI image generation using Bedrock
    │   │   └── lambda/         
    │   └── style_transfer/      # Photo artistic transformation
    │       └── lambda/         
    ├── shared/                  # Common utilities and patterns
    │   └── python/             
    │       ├── credit_management.py      # Credit validation and consumption
    │       └── route_params_loader.py    # API parameter validation
    └── video/                   # Video processing services
        ├── video_chat/          # YouTube video interaction
        │   └── functions/      
        └── video_summarizer/    # Video content summarization
            └── lambda/         

The structure uses lambda/ folders for single-function services and functions/ folders for services that may contain multiple related functions. This organization makes it easy to locate and modify specific functionality.

Technology Stack

Runtime Environment:

  • Python 3.11 for all Lambda functions
  • AWS Lambda for serverless compute
  • Shared Lambda layers for common dependencies

Key Libraries:

  • AWS SDK (boto3): AWS service integration
  • Stripe SDK: Payment processing
  • OpenAI SDK: AI model integration
  • Requests: HTTP client for external APIs
  • Pillow: Image processing capabilities

Integration Points:

  • DynamoDB: User data and credit management
  • S3: File storage and processing
  • Parameter Store: Secure configuration management
  • AppSync: Real-time GraphQL updates

Backend Organization Categories

The backend is organized into three main categories that correspond to different aspects of the platform:

Business Logic Layer

Core platform functionality that handles user accounts, billing, and credit management. These services power the fundamental business operations of the SaaS platform.

AI Services Layer

Individual AI service implementations that provide the core value proposition. Each service is independently deployable and follows consistent patterns for authentication and credit consumption.

Shared Components Layer

Common utilities and patterns used across all services to ensure consistency, reduce code duplication, and maintain standardized behavior for authentication and credit management.

Backend Components Guide

Integration with Infrastructure

The backend code structure directly corresponds to the CDK infrastructure stacks:

  • Each lambda/ folder corresponds to a Lambda function defined in CDK
  • Business logic maps to business stacks (credits, payments)
  • AI services map to service stacks deployed through the service factory
  • Shared utilities are deployed as Lambda layers across multiple functions

This one-to-one mapping makes it easy to understand how code changes affect infrastructure and vice versa.

Next Steps

Now that you understand the backend organization:

On this page