Infrastructure Overview
Overview of the infrastructure architecture of the AI SaaS Boilerplate
Infrastructure Overview
The AI SaaS Boilerplate uses AWS Cloud Development Kit (CDK) to define and provision all infrastructure resources. This infrastructure-as-code approach ensures consistent, repeatable deployments and simplifies management.
Infrastructure as Code with CDK
AWS CDK allows you to define cloud infrastructure using familiar programming languages. The boilerplate uses TypeScript for CDK code, providing:
- Type safety and auto-completion
- Reusable components through constructs
- Logic and conditionals for complex deployments
- Integration with your existing development workflow
CDK Project Structure
The CDK code is organized in the infrastructure
directory:
Stack Categories
The infrastructure is organized into several logical stack categories:
Authentication & User Management
CognitoUserPoolStack
Manages the AWS Cognito user pool for authentication
AdminUserStack
Handles admin user management
CognitoPostConfirmationTrigger
Lambda function triggered after user signup
Storage & Secrets
StorageStack
Deploys S3 buckets for storing user data
SecretManagerStack
Manages AWS Secrets Manager for secure storage
UpdateSecretLambdaStack
Updates secrets dynamically in AWS Secrets Manager
AI Services
ImageGenLambdaStack
Deploys an AI-based image generation Lambda
OcrPdfLambdaStack
Deploys an OCR Lambda to extract text from PDFs
YtVideoSummarizeStack
Deploys AI-powered YouTube video summarization
MarkdownConvertStack
Converts Markdown files into different formats
ServerlessPdfChatStack
Enables AI-driven conversations with PDFs
YtVideoChatStack
Enables AI-based chat with YouTube videos
PhotoToAnimeStack
Transforms user photos into anime-style images
Payments & Subscriptions
StripePaymentHandlingStack
Handles Stripe payment processing
StripeWebhookStack
Listens for Stripe webhook events
StripePricingStack
Fetches pricing plans from Stripe
ServiceCreditsStack
Manages service credits for AI API usage
Frontend Deployment
AmplifyNextJsAppStack
Deploys the main frontend via AWS Amplify
AmplifyConfigStack
Handles Amplify configurations and API endpoints
Utility Stacks
EventBridgeScheduleRoleStack
Defines scheduled tasks via AWS EventBridge
EmailSystemStack
Handles automated email sending
ContactFormStack
Manages contact form submissions
UserSettingsStack
Manages user settings and preferences
UserCreditsHandlingStack
Handles credit updates and transactions
Stack Dependencies
The CDK stacks have dependencies on each other. Here's a simplified dependency diagram:
CDK automatically determines the correct deployment order based on these dependencies.
Resource Naming
Resources are named using a consistent pattern:
For example:
ai-saas-dev-user-pool
ai-saas-prod-image-gen-lambda
This naming convention makes it easy to identify resources in the AWS console and helps with resource management.
Environment-Specific Configurations
The boilerplate supports different deployment environments (dev, staging, prod) through context-based configuration:
Environment-specific configurations are defined in cdk.context.json
.
IAM Roles and Permissions
Each Lambda function and service is assigned an IAM role with the minimum necessary permissions following the principle of least privilege. These roles are defined within each stack.
Example IAM role for a Lambda function:
API Gateway Configuration
The API Gateway is configured with:
- REST API endpoints organized by service
- Lambda integrations for each endpoint
- Cognito authorizers for protected endpoints
- CORS configuration for frontend access
- API deployment stages (dev, prod)
Resource Tagging
All resources are tagged with metadata for better organization and cost tracking:
CloudWatch Logs and Monitoring
Each Lambda function has CloudWatch Logs enabled for monitoring and troubleshooting. Additionally, CloudWatch Alarms are configured for critical components to alert on issues.
Cost Optimization
The infrastructure is designed with cost optimization in mind:
- Serverless components that scale to zero when not in use
- Automatic scaling based on demand
- Appropriate provisioned concurrency settings
- Storage lifecycle policies for cost-effective data management
Next Steps
To learn more about specific aspects of the infrastructure: