Serverless Laravel made easy with Bref
Tags: laravel, serverless, aws, php
Published: 2021-01-23 14:16:05

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](https://www.serverless.com) 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](https://serverless.com) 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](https://console.aws.amazon.com/iam/home?#/users$new?step=details) and attach the needed policies to the user. Refer to this [gist](https://gist.github.com/ServerlessBot/7618156b8671840a539f405dea2704c8) 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](https://github.com/brefphp/bref) and the [Laravel-Bref](https://github.com/brefphp/laravel-bridge) 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](https://www.serverless.com/framework/docs/providers/aws/guide/serverless.yml/) 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**

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](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-nat-gateway.html). 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](https://aws.amazon.com/vpc/pricing/) for the US-East region (34$/month).

4. 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](https://buymeacoffee.com/nilanth) ☕**