In this guide, I'll walk you through how to optimize a Laravel 11 application for serverless deployment on AWS Lambda. Running Laravel on AWS Lambda can bring scalability and cost savings, but it also comes with challenges like cold starts, database connections, and file storage.
I'll explain these issues and provide practical solutions with step-by-step examples and code snippets. By the end of this article, you'll be ready to deploy your Laravel app efficiently on AWS Lambda.
To deploy Laravel on AWS Lambda, you'll need a solution that allows PHP applications to run in this environment. We'll use Bref, a serverless PHP framework that simplifies Laravel deployments on AWS Lambda.
Install Bref in your Laravel project:
composer require bref/bref
Configure serverless.yml
for deployment: Create a serverless.yml
file at the root of your Laravel project. This file defines how your app will be deployed to AWS Lambda.
service: laravel-on-lambda
provider:
name: aws
region: us-east-1
runtime: provided.al2
functions:
website:
handler: public/index.php
events:
- http: 'ANY /'
- http: 'ANY /{proxy+}'
plugins:
- ./vendor/bref/bref
Set Laravel's storage path to tmp
: AWS Lambda has a read-only file system, but it provides /tmp
for temporary file storage.
Update config/filesystems.php to handle Lambda's file storage:
'lambda' => [
'driver' => 'local',
'root' => env('LAMBDA_STORAGE_PATH', '/tmp'),
],
Cold starts occur when an AWS Lambda function is invoked after being idle, leading to longer initial response times.
To mitigate cold starts:
Optimize Autoloader: Enable optimized autoloading to reduce the time it takes to load classes.
composer install --optimize-autoloader --no-dev
Keep Lambda Warm: Use a warm-up plugin like serverless-plugin-warmup
to keep the function active and reduce cold starts.
npm install serverless-plugin-warmup --save-dev
Add it to the serverless.yml
:
plugins:
- serverless-plugin-warmup
Reduce Bootstrapping Time: Remove unnecessary service providers in app.php to streamline the bootstrapping process.
Database connections can be tricky on Lambda since it's a stateless environment. Traditional database connections can cause timeouts or connection exhaustion.
Use AWS RDS Proxy for handling MySQL or PostgreSQL connections: RDS Proxy pools and manages database connections, allowing your Laravel app to scale efficiently. Increase PDO connection retries in your database.php to prevent timeouts.
Update your DB_HOST in the env file.
DB_HOST=my-rds-proxy.endpoint.aws
Increase PDO connection retries in your database.php file
'mysql' => [
'retry_after' => 5,
'retries' => 3,
],
Since Lambda is stateless, you cannot rely on local file storage. Instead, you can use AWS S3 for file uploads and storage.
Set up AWS S3 in config/filesystems.php:
'disks' => [
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
],
],
Update the env file with AWS S3 credentials like AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_DEFAULT_REGION, AWS_BUCKET
Use S3 for file uploads:
$filePath = $request->file('upload')->store('uploads', 's3');
With all the optimizations in place, you're ready to deploy your Laravel application to AWS Lambda.
Package your Laravel app for deployment:
serverless deploy
Test your API endpoint to ensure it's working efficiently:
curl https://your-api-endpoint.amazonaws.com/dev
You might also like:
Hi there! In this tutorial, I’ll show you how to build smarter chatbots in Laravel by leveraging Python’s po...
Dec-03-2024
In this article, we will see an example of laravel 9 REST API with passport authentication. Also, perform CRUD...
Mar-13-2022
Using Select2 with Laravel Livewire can enhance your forms by providing a user-friendly dropdown experience. However, wh...
Jan-21-2025
In this article, we will see how to get multiple checkbox values in react js. In react, js click on the submit butt...
Aug-29-2022