Skip to content

raulprdev/aws-serverless-laravel

Repository files navigation

Serverless Laravel on AWS

Deploy Laravel applications on AWS Lambda using Terraform. Zero server management, pay-per-request pricing, automatic scaling.

Architecture

                                    ┌─────────────────┐
                                    │   CloudFront    │
                                    │   (CDN + SSL)   │
                                    └────────┬────────┘
                                             │
                    ┌────────────────────────┼────────────────────────┐
                    │                        │                        │
                    ▼                        ▼                        ▼
            ┌───────────────┐       ┌───────────────┐       ┌───────────────┐
            │  /build/*     │       │  /css/*       │       │  Everything   │
            │  /js/*        │       │  /images/*    │       │  else         │
            └───────┬───────┘       └───────┬───────┘       └───────┬───────┘
                    │                       │                       │
                    ▼                       ▼                       ▼
            ┌───────────────────────────────┐               ┌───────────────┐
            │          S3 Bucket            │               │  API Gateway  │
            │      (Static Assets)          │               │   HTTP API    │
            └───────────────────────────────┘               └───────┬───────┘
                                                                    │
                                                                    ▼
                                                            ┌───────────────┐
                                                            │    Lambda     │
                                                            │  (PHP + Bref) │
                                                            └───────┬───────┘
                                                                    │
                                    ┌───────────────────────────────┼───────────────┐
                                    │                               │               │
                                    ▼                               ▼               ▼
                            ┌───────────────┐               ┌───────────────┐   ┌───────┐
                            │      SQS      │               │      RDS      │   │  ...  │
                            │  (Job Queue)  │               │    (MySQL)    │   │       │
                            └───────────────┘               └───────────────┘   └───────┘

Features

  • Multi-site support - Deploy multiple Laravel apps with one configuration
  • Automatic scaling - Lambda scales to handle any traffic
  • Zero servers - No EC2, no patching, no maintenance
  • Queue processing - SQS integration with worker Lambda
  • Static assets - S3 + CloudFront for CSS/JS/images
  • Database - Connects to existing RDS instance
  • Artisan commands - Dedicated Lambda for running artisan

Prerequisites

  • AWS account with appropriate permissions
  • Terraform >= 1.0
  • Existing VPC with private subnets (for RDS access)
  • Existing RDS MySQL instance
  • Domain managed in Route 53 or external DNS

Quick Start

  1. Copy example configuration

    cp terraform.tfvars.example terraform.tfvars
  2. Edit terraform.tfvars with your values:

    • VPC and subnet IDs
    • RDS endpoint and credentials
    • Site configuration (domain, database name, APP_KEY)
  3. Initialize and apply

    terraform init
    terraform plan
    terraform apply
  4. Add DNS validation records (output after apply)

  5. Deploy your Laravel code to the Lambda function

Configuration

Site Configuration

sites = {
  my_app = {
    name           = "my-app"
    domain         = "app.example.com"
    domain_aliases = ["www.app.example.com"]
    db_name        = "my_app_db"
    app_key        = "base64:..."
    lambda_memory  = 1024          # Optional, default 1024
    lambda_timeout = 28            # Optional, max 28 for API Gateway
    worker_memory  = 512           # Optional
    worker_timeout = 120           # Optional, max 900
    enabled        = true
  }
}

Custom Environment Variables

Add any environment variables your app needs:

custom_env_vars = {
  MAIL_MAILER     = "ses"
  STRIPE_KEY      = "sk_live_xxx"
  PUSHER_APP_ID   = "123456"
}

Deploying Code

Lambda functions are created with placeholder code. Deploy your actual Laravel app using:

Option 1: AWS CLI

cd your-laravel-app
zip -r deployment.zip . -x "*.git*" "node_modules/*" "tests/*"
aws lambda update-function-code \
  --function-name my-app-web \
  --zip-file fileb://deployment.zip

Option 2: CI/CD Pipeline (recommended)

Set up GitHub Actions, GitLab CI, or AWS CodePipeline to deploy on push.

Laravel Configuration

Your Laravel app needs these settings for Lambda:

LOG_CHANNEL=stderr
CACHE_DRIVER=array
SESSION_DRIVER=database
QUEUE_CONNECTION=sqs

Add to AppServiceProvider.php:

public function boot(): void
{
    if ($this->app->environment('production') && env('APP_URL')) {
        URL::forceRootUrl(env('APP_URL'));
        URL::forceScheme('https');
    }
}

Cost Estimate

Typical costs for a low-traffic Laravel app:

Resource Monthly Cost
Lambda (1M requests) ~$0.20
API Gateway (1M requests) ~$1.00
CloudFront (10GB transfer) ~$0.85
S3 (1GB storage) ~$0.02
Total ~$2-5/month

Does not include RDS costs (shared with other apps)

Outputs

After terraform apply:

  • cloudfront_domains - CloudFront URLs and distribution IDs
  • api_gateway_urls - Direct API Gateway endpoints
  • lambda_functions - Lambda function names (web, worker, artisan)
  • sqs_queues - SQS queue URLs
  • certificate_validation_records - DNS records for SSL validation

Running Artisan Commands

aws lambda invoke \
  --function-name my-app-artisan \
  --payload '{"command": "migrate --force"}' \
  response.json

About

Deploy Laravel applications on AWS Lambda with Terraform. Zero server management, automatic scaling, pay-per-request pricing. Includes CloudFront CDN, API Gateway, SQS queues, and S3 static assets. Multi-site support with a single configuration.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages