Back to all articles
Laravel Insights Dec 18, 2025 โˆ™ 1 min read

Serverless Laravel on AWS: A Complete Guide

Learn how to deploy Laravel applications using AWS serverless architecture with practical examples and best practices.

A diagram of a serverless Laravel architecture using AWS Lambda and PlanetScale.

Building Serverless Laravel Applications on AWS: A Comprehensive Guide

Learn how to deploy Laravel applications using AWS serverless architecture with practical examples and best practices.

For decades, deploying a web application meant one thing: provisioning a server. Whether physical or virtual, you were responsible for managing its operating system, security patches, and scaling strategy. This model, while familiar, often leads to paying for idle resources and dealing with the complexities of infrastructure management. Serverless computing flips this paradigm on its head, offering a way to run your code without thinking about servers at all.

For Laravel developers, this shift opens up a world of immense scalability and cost-efficiency. By leveraging services like AWS Lambda, you can deploy applications that automatically scale to handle millions of requests and then scale back down to zero, all while you only pay for the compute time you actually use. Combined with tools like Bref and serverless-friendly databases like PlanetScale, running a high-performance Laravel application in a serverless environment has never been more accessible.

This guide will walk you through everything you need to know about building a serverless Laravel application on AWS. We'll cover the core concepts, provide a step-by-step tutorial for deploying a real application, and explore best practices for performance and maintenance.

What is Serverless Architecture and Why Use It?

Serverless architecture doesn’t mean there are no servers; it just means you don’t manage them. Instead, a cloud provider (like AWS) is responsible for executing your code by dynamically allocating resources. Your application code is run inside ephemeral containers that start up on-demand, handle a single request, and then shut down.

This model, often called "Functions-as-a-Service" (FaaS), offers several compelling advantages:

  • Automatic Scaling: The platform automatically scales the number of functions up or down to match traffic, from zero to thousands of concurrent requests, without any configuration.
  • Cost-Efficiency: You only pay for the execution time your code uses, measured in milliseconds. When your application is idle, you pay nothing.
  • Reduced Operational Overhead: You no longer need to worry about server maintenance, security patches, or capacity planning. You can focus entirely on writing code.
  • High Availability: Serverless platforms are inherently redundant and fault-tolerant, providing high availability out of the box.

However, it's not without its challenges. Developers must contend with "cold starts" (the initial latency when a new function container starts), statelessness, and a different approach to debugging and monitoring.

Tools of the Trade: Bref and PlanetScale

To run a stateful framework like Laravel in a stateless serverless environment, we need some help. This is where specialized tools come in.

  • Bref (Bref is French for "brief"): Bref is an open-source project that makes it incredibly simple to deploy PHP applications, including Laravel, to AWS Lambda. It provides PHP runtimes for Lambda and integrates seamlessly with the Serverless Framework for easy deployment.
  • PlanetScale: A serverless MySQL-compatible database platform. Traditional databases can struggle with the massive number of concurrent connections that a serverless application can generate. PlanetScale is built to handle this, offering effortless scaling and a developer-friendly workflow with features like database branching.

Let's build a simple API that lists a collection of links. We will deploy it to AWS Lambda using Bref and connect it to a PlanetScale database.

Step 1: Set Up Your Prerequisites

Before you start, ensure you have the following:

  1. An AWS account.
  2. Node.js and npm installed.
  3. The Serverless Framework CLI installed globally: npm install -g serverless.
  4. Your AWS credentials configured for the Serverless Framework. Follow this guide to create an AWS user and run serverless config credentials --provider aws --key <key> --secret <secret>.
  5. A free PlanetScale account.

Step 2: Create a New Laravel Project

Start by creating a fresh Laravel application using Composer.

composer create-project laravel/laravel serverless-links
cd serverless-links

Step 3: Integrate Bref

Next, add Bref and its Laravel bridge to your project.

composer require bref/bref bref/laravel-bridge

Now, publish the Bref configuration file (serverless.yml) to your project root.

php artisan vendor:publish --tag=serverless-config

This serverless.yml file is your infrastructure-as-code definition. It tells the Serverless Framework what to deploy to your AWS account.

Step 4: Set Up the PlanetScale Database

  1. In your PlanetScale dashboard, create a new database. Choose the AWS region closest to your users (e.g., us-east-1).
  2. Once created, click the "Connect" button. Select "Laravel" from the "Connect with" dropdown.
  3. PlanetScale will provide you with all the .env variables you need. Copy these and add them to your project's .env file. It will look something like this:
DB_CONNECTION=mysql
DB_HOST=aws.connect.psdb.cloud
DB_PORT=3306
DB_DATABASE=your-db-name
DB_USERNAME=your-username
DB_PASSWORD=your-pscale-password
MYSQL_ATTR_SSL_CA=/etc/ssl/cert.pem

Important: For AWS Lambda, the SSL certificate path is different. You'll need to set up environment variables specifically for your production environment in serverless.yml. Open the file and find the provider.environment section. Add your database credentials there, changing the certificate path.

provider:
    # ...
    environment:
        # ...
        DB_CONNECTION: mysql
        DB_HOST: ${ssm:DB_HOST} # Use AWS SSM for secrets!
        DB_DATABASE: ${ssm:DB_DATABASE}
        DB_USERNAME: ${ssm:DB_USERNAME}
        DB_PASSWORD: ${ssm:DB_PASSWORD}
        MYSQL_ATTR_SSL_CA: /opt/bref/ssl/cert.pem

(For production, it's a best practice to store secrets in AWS Systems Manager Parameter Store (SSM) instead of plain text, as shown above.)

Let's create a Link model and its migration file.

php artisan make:model Link -m

Open the generated migration file and define the schema for our links table.

// database/migrations/xxxx_xx_xx_xxxxxx_create_links_table.php
public function up(): void
{
    Schema::create('links', function (Blueprint $table) {
        $table->id();
        $table->string('title');
        $table->string('url');
        $table->timestamps();
    });
}

Now, let's create a simple API route in routes/api.php to fetch all links.

// routes/api.php
use App\Models\Link;

Route::get('/links', function () {
    return Link::all();
});

Step 6: Deploy to AWS Lambda

With everything configured, deploying is as simple as running one command:

serverless deploy

The Serverless Framework will package your application, create the necessary AWS resources (like Lambda functions and an API Gateway), and deploy your code. When it's finished, it will output the URL for your new serverless API.

Step 7: Run Migrations and Seed Data

To run your database migration, you can't SSH into a server. Instead, you execute the Artisan command directly on Lambda.

# Run migrations
serverless bref:cli --args="migrate --force"

# Use tinker to add a link
serverless bref:cli --args="tinker"
>>> App\Models\Link::create(['title' => 'Laravel', 'url' => 'https://laravel.com']);
>>> App\Models\Link::create(['title' => 'Bref', 'url' => 'https://bref.sh']);

Now, if you visit the /api/links endpoint of your deployed application, you'll see the JSON response with your links, served from a serverless function and a serverless database!

Optimizing Performance with Laravel Octane

A "cold start" happens the first time a Lambda function is invoked. AWS has to download your code and start a new container, which can add hundreds of milliseconds of latency. For subsequent requests, the "warm" container is reused, and responses are much faster.

To minimize latency even further, you can use Laravel Octane with Bref. Octane keeps your Laravel application booted in memory between requests, which dramatically speeds up warm invocations.

To enable Octane, simply modify your serverless.yml to use the Octane handler:

functions:
    web:
        handler: Bref\LaravelBridge\Http\OctaneHandler
        # ... other configs

After another serverless deploy, your application will be even faster on warm requests, often responding in under 20ms.

Conclusion: Is Serverless Right for You?

Serverless Laravel offers an exciting path for building highly scalable, cost-effective applications without the burden of server management. It's an excellent choice for APIs, microservices, and applications with unpredictable or spiky traffic patterns.

By combining the power of the Laravel framework with the flexibility of AWS Lambda, the simplicity of Bref, and the robust scalability of PlanetScale, you can create modern applications that are resilient by design. While the serverless paradigm requires a shift in mindset, the benefits in terms of scalability and reduced operational load are often well worth the effort.


Related articles

Continue exploring Laravel insights and practical delivery strategies.

Laravel consulting

Need senior Laravel help for this topic?

Let's adapt these practices to your product and deliver the next milestone.