Serverless Laravel Made Easy with Bref

Serverless Laravel Made Easy with Bref

·

4 min read

Run a Serverless Laravel Application using Bref in AWS Lambda.

In this article, we can see how to run a Serverless Laravel Application using the 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 is used to deploy PHP (Laravel) applications 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
  • 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>

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
    
  • After installing the above packages, Run the below command
    php artisan vendor:publish --tag=serverless-config
    

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

Let’s deploy using the below command

serverless deploy

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 files 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.

Conclusion

Now the Serverless Laravel application is successfully hosted in AWS Lambda using Bref. I hope you have found this useful. Thank you for reading.

Connect on Twitter | LinkedIn | GitHub

You should definitely check out my other Blogs:

  1. 15 Custom Hooks to Make your React Component Lightweight
  2. 10 Ways to Host Your React App For Free
  3. How to Secure JWT in React App
  4. How To Use Axios in an Optimized and Scalable Way With React

Thank you for reading.

Did you find this article valuable?

Support Nilanth's Blog by becoming a sponsor. Any amount is appreciated!