Developing Serverless Apps with Laravel 11 In 2024 For Best Web Apps With Code Examples

Earlier, we have shown you the latest wep apps development trends 2024-2025 using Laravel 11 and one of the hottest trends these days in the same domain is using Serverless architecture for achieving that. It has become a cornerstone for modern web-apps, offering a simplified deployment process and scalability without the overhead of server management. Laravel, a PHP framework renowned for its elegance and robustness, can be effectively used to build serverless 2025’s apps. This guide will explore how to deploy its applications using Laravel Vapor, AWS Lambda with Bref, and Vercel, complete with detailed code examples. You can also see here, how to use Serverless function in Laravel 11 for in your new 2024 projects which we have shown you previously.

Serverless computing allows developers to build and run applications without worrying about server management. The cloud provider takes care of provisioning, scaling, and managing the infrastructure, allowing you to focus on your application’s functionality.

Laravel Vapor

Laravel Vapor is an official serverless deployment platform for new such apps in 2024 and 2025 new projects, built on AWS. It abstracts the complexities of AWS and provides a seamless experience for deploying these apps to AWS Lambda.

Setting Up Laravel Vapor

(I) Install Laravel Vapor CLI

To get started, install the Laravel Vapor CLI globally using Composer:

composer global require laravel/vapor-cli

(II) Initialize a Vapor Project

Navigate to your Laravel project and initialize Vapor:

vapor init

Follow the prompts to configure your AWS credentials and project settings.

(III) Configure ‘vapor.yml’

Edit the vapor.yml file to configure your environment:

id: 1
name: my-laravel-app
environments:
    production:
        memory: 1024
        cli-memory: 512
        timeout: 60
        runtime: php-8.0
        build:
            - 'composer install --no-dev'
            - 'php artisan config:cache'
            - 'php artisan route:cache'

(IV) Deploy to Vapor

Deploy your application to Vapor:

vapor deploy production

(V) Using Vapor’s CLI Commands

Vapor CLI offers several useful commands, such as:

vapor shell production
vapor logs production
vapor database:connect production

Example Application

Here’s a simple route setup in web.php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

Deploy this setup using Vapor to make it available via AWS Lambda.

AWS Lambda with Bref

Bref is a popular open-source project that simplifies running PHP applications on AWS Lambda. It provides custom runtimes for PHP.

Setting Up Laravel with AWS Lambda using Bref.

#1 Install Bref

Add Bref to your Laravel project:

 

composer require bref/bref

#2 Create serverless.yml

Configure the Serverless Framework by creating a serverless.yml file:

service: laravel

provider:
  name: aws
  runtime: provided.al2
  region: us-east-1

functions:
  web:
    handler: public/index.php
    timeout: 28
    layers:
      - 'arn:aws:lambda:us-east-1:209497400698:layer:php-80-fpm:1'
    events:
      - http: 'ANY /'
      - http: 'ANY /{proxy+}'

plugins:
  - ./vendor/bref/bref

#3 Deploy with Serverless Framework

Deploy your application using the Serverless Framework:

vendor/bin/bref deploy

Example Application

Create a new controller:

php artisan make:controller HelloWorldController

In HelloWorldController.php:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HelloWorldController extends Controller
{
    public function index()
    {
        return response()->json(['message' => 'Hello, world!']);
    }
}

Update web.php:

use App\Http\Controllers\HelloWorldController;

Route::get('/hello', [HelloWorldController::class, 'index']);

Deploy this setup to AWS Lambda using Bref.

Vercel

Vercel is a cloud platform for hosting static sites and serverless functions, supporting PHP through its serverless functions feature.

Deploying Laravel to Vercel

(I) Install Vercel CLI

Install the Vercel CLI globally

npm install -g vercel

(II) Create ‘vercel.json’

Configure your Vercel project:

{
  "version": 2,
  "builds": [
    {
      "src": "api/index.php",
      "use": "@vercel/php"
    }
  ],
  "routes": [
    {
      "src": "/(.*)",
      "dest": "/api/index.php"
    }
  ]
}

Deploy to Vercel:

Deploy your Laravel application:

vercel

Example Application
Move your index.php to the api directory and ensure it points to the correct paths:

require __DIR__.'/../vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);

Deploy with Vercel and your Laravel application will be served through their serverless functions.

Building serverless apps in 2024 for your current or new projects for 2025 with Laravel 11 opens up new possibilities for scalability and efficiency. Laravel Vapor, AWS Lambda with Bref, and Vercel provide powerful options for deploying Laravel applications in a serverless architecture. By following the steps and examples provided, you can seamlessly transition your Laravel projects to a serverless environment, enjoying the benefits of automatic scaling and reduced infrastructure management.