Serverless Laravel made easy with Bref
Nilanth
Posted on January 23, 2021
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
- AWS Lambda
- Azure Functions
- Google Cloud Functions
- Cloudflare
Why Serverless?
- The server setup is managed by the cloud service provider.
- Pay for what is used.
- 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
- Install the Serverless framework as a global dependency using below command.
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 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
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.
If you are using sessions, You need to move the session storage to Elasticache or DynamoDB.
Update FILESYSTEM_DRIVER to S3 and store all your public file to S3.
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).
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.
....
You can support me by buying me a coffee ☕
Posted on January 23, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.