DeployFrame Docs

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:

infrastructure/
├── bin/                   # CDK app entry point
│   └── app.ts             # Main CDK application
├── lib/                   # Stack definitions
│   ├── stacks/            # Individual stack implementations
│   │   ├── auth/          # Authentication stacks
│   │   ├── services/      # AI service stacks
│   │   ├── storage/       # Storage stacks
│   │   └── ...            # Other stack categories
│   └── constructs/        # Reusable CDK constructs
├── cdk.json               # CDK configuration
├── cdk.context.json       # Context values and parameters
├── package.json           # Dependencies and scripts
└── tsconfig.json          # TypeScript configuration

Stack Categories

The infrastructure is organized into several logical stack categories:

Authentication & User Management

Storage & Secrets

AI Services

Payments & Subscriptions

Frontend Deployment

Utility Stacks

Stack Dependencies

The CDK stacks have dependencies on each other. Here's a simplified dependency diagram:

StorageStack <-- CognitoUserPoolStack <-- ServiceCreditsStack
                                      <-- AI Service Stacks
SecretManagerStack <-- UpdateSecretLambdaStack <-- AdminUserStack
                                              <-- UserCreditsHandlingStack
                                             
CognitoUserPoolStack <-- EventBridgeScheduleRoleStack
                     <-- EmailSystemStack
                     <-- ContactFormStack
                     <-- StripePaymentHandlingStack
                     <-- StripeWebhookStack
                     <-- StripePricingStack
                     <-- UserSettingsStack

(All API Stacks) <-- AmplifyConfigStack <-- AmplifyNextJsAppStack

CDK automatically determines the correct deployment order based on these dependencies.

Resource Naming

Resources are named using a consistent pattern:

{AppName}-{Stage}-{ResourceType}-{OptionalIdentifier}

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:

const stage = this.node.tryGetContext('stage') || 'dev';
const config = this.node.tryGetContext(stage);

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:

const role = new iam.Role(this, 'LambdaRole', {
  assumedBy: new iam.ServicePrincipal('lambda.amazonaws.com'),
  managedPolicies: [
    iam.ManagedPolicy.fromAwsManagedPolicyName('service-role/AWSLambdaBasicExecutionRole')
  ]
});
 
// Add specific permissions
role.addToPolicy(new iam.PolicyStatement({
  effect: iam.Effect.ALLOW,
  actions: ['s3:GetObject', 's3:PutObject'],
  resources: [bucket.arnForObjects('*')]
}));

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:

cdk.Tags.of(this).add('Project', 'AI-SaaS-Platform');
cdk.Tags.of(this).add('Environment', stage);
cdk.Tags.of(this).add('ManagedBy', 'CDK');

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: