DEV Community

Cover image for Serverless Laravel made easy with Bref
Nilanth
Nilanth

Posted on • Originally published at nilanth.Medium

Serverless Laravel made easy with Bref

Run a Serverless Laravel application using Bref in AWS Lambda.

In this article, we can see how to run a serverless Laravel application using Bref package and AWS Lambda.

What is Serverless?

Let’s keep it simple: Serverless is a cloud model where the cloud provider takes care of resource allocation dynamically, so charged for the amount of resource used. Serverless is also referred to as “Functions as a Service” or “FaaS”.

Serverless Providers

  1. AWS Lambda
  2. Azure Functions
  3. Google Cloud Functions
  4. Cloudflare

Why Serverless?

  1. The server setup is managed by the cloud service provider.
  2. Pay for what is used.
  3. Automatic scaling.

Due to serverless architecture, we only pay when the code executes not for idle time.

Bref for Serverless:

Bref is an open-source composer package, that used to deploy PHP (Laravel) application to AWS Lambda. Bref has good documentation and supports major PHP frameworks. Bref uses the serverless framework to deploy and configure the serverless application.

Now let’s deploy our Laravel app to AWS Lamba.

Serverless Framework Config

Prerequisite Node.js 6.x or later version

npm install -g serverless
Enter fullscreen mode Exit fullscreen mode
  • After step 1 is completed, create IAM programmatic user in AWS IAM console and attach the needed policies to the user. Refer to this gist for required policies. After creating the user, Note the access keys that are generated.

Creating a separate IAM programmatic user for serverless is recommended.

  • Setup Serverless config using the below command
serverless config credentials --provider aws --key <key> --secret <secret>
Enter fullscreen mode Exit fullscreen mode

Now the serverless framework is configured to our local machine, let’s add Bref to our Laravel project

Bref setup

  • In the existing Laravel project install Bref and the Laravel-Bref package using composer.
composer require bref/bref bref/laravel-bridge
Enter fullscreen mode Exit fullscreen mode
  • After installing the above packages, Run the below command
php artisan vendor:publish --tag=serverless-config
Enter fullscreen mode Exit fullscreen mode

This command will create the serverless.yml config file in the project root directly. By default, the serverless app will be hosted in us-east-1 region, if you want to change the region use can change the region in serverless.yml file and there are a lot of properties you can configure in it. Check here to view all the properties.

Deploy Your App

Clear all caches before deployment and run deploy command

php artisan config:clear
Enter fullscreen mode Exit fullscreen mode

Let’s deploy using below command

serverless deploy
Enter fullscreen mode Exit fullscreen mode

The above command will zip the Laravel project, Upload it to S3 and deploy it in Lambda using the CloudFormation template. When the deployment is completed, It will show a URL of the hosted app in AWS Lambda, Hit that URL in the browser to see your Serverless Laravel application.

Points to be Noted

  1. AWS Lambda filesystem is read-only expect /temp directory, by default Bref move the Laravel caching to /temp directory, But the /temp directory is not shared with other Lambda instances. So we need to use centralized storage services like Elasticache or DynamoDB to resolve this.

  2. If you are using sessions, You need to move the session storage to Elasticache or DynamoDB.

  3. Update FILESYSTEM_DRIVER to S3 and store all your public file to S3.

  4. To provide internet access to your Lambda function, Which is in VPC. You need to create a NAT Gateway. As to access most of the AWS services, Lambda needs to run in the same VPC. NAT Gateway will increase the cost, As its hourly cost is $0.045 for the US-East region (34$/month).

  5. Lambda Cold Start duration might increase if the application codebase size increases. So exclude test cases, images, files, UI assets while deploying.

Now the Serverless Laravel application is successfully hosted in AWS Lambda using Bref.

Top comments (0)