Running Laravel on AWS Lambda

Running Laravel on AWS Lambda

Learn how to create serverless Laravel applications by deploying them to AWS Lambda.


Before deploying to AWS Lambda, you will need:

  • An AWS account

Install Node JS and serverless CLI

Download and import the Nodesource GPG key

sudo apt-get update
sudo apt-get install -y ca-certificates curl gnupg
sudo mkdir -p /etc/apt/keyrings
curl -fsSL | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg

Create deb repository

echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg]$NODE_MAJOR.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list

Run Update and Install

sudo apt-get update
sudo apt-get install nodejs -y

Install the serverless CLI using NPM

npm install -g serverless

Update Package Lists

sudo apt-get update

Install PHP and Required Extensions

sudo apt install php libapache2-mod-php php-mbstring php-xmlrpc php-soap php-gd php-xml php-cli php-zip php-bcmath php-curl php-tokenizer php-json

Install Composer

Downloads and installs Composer globally. Create Laravel Project:

curl -sS | php 
sudo mv composer.phar /usr/local/bin/composer 
sudo chmod +x /usr/local/bin/composer

Create A sample Laraval Project

Uses Composer to create a new Laravel project named "serverless-laravel"

composer create-project laravel/laravel serverless-laravel

cd to serverless-laravel

install Bref and its Laravel integration

composer require bref/bref bref/laravel-bridge --update-with-dependencies

create a serverless.yml configuration file

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

connect the serverless to your AWS account

connect the serverless CLI to your AWS account via AWS access and secret key keys

Create AWS access and secret keys by following documentation,

Adding keys

serverless config credentials --provider aws --key <key> --secret <secret>


serverless deploy

Using RDS as the database

Edit the .env configuration file to set up the database connection.Also, Make sure Lambda has access to this Database;

DB_HOST=<host url>

Deploying for production

composer install --prefer-dist --optimize-autoloader --no-dev


Serverless Framework has a concept of stages (environments). We can deploy the same application multiple times in completely separated environments

serverless deploy --stage=prod

Automating deployments

If you are using CodePipeline you can use this in AWS CodeBuild as buildspec.yml

version: 0.2
      nodejs: 18
      php: 8.2

      - npm install -g serverless
      - curl -sS | php -- --install-dir=/usr/local/bin --filename=composer
      - composer install --prefer-dist --optimize-autoloader --no-dev

      - serverless deploy --stage=prod

Note: Make sure the AWS code build has permission (Role) attached to it so the code build is allowed to deploy.

Modify the IAM Role of CodeBuild and add this Policy, this is a Minimal AWS policy that allows to deployment of a serverless application using the Serverless Framework.

    "Statement": [
            "Action": [
            "Effect": "Allow",
            "Resource": "*"
    "Version": "2012-10-17"

Did you find this article valuable?

Support Muhammad Usama by becoming a sponsor. Any amount is appreciated!