Deploy Serverless Laravel application using bref 
Tags: serverless, laravel, bref, lambda
Published: 2020-07-27 09:04:03

---
title: Deploy Serverless Laravel application using bref 
published: true
description: Deploy Laravel application on AWS Lambda using bref, which is a package used to deploy and run serverless php application
tags: serverless, laravel, bref, lambda
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/dns2jjtuu5f106dncu5g.png
---
Let me show you a way to deploy your Laravel application on AWS Lambda for free! using bref.

Why not Laravel Vapor?
Laravel Vapor is great and if you can spend a little money then I would highly recommend that. It provides you with a nice dashboard for managing all you serverless applications and because it's built by Taylor Otwell (Creator of Laravel), So the integration and the support provided is great.

But if you are just starting out with serverless and want to test few Laravel applications then this tutorial is for you.

If you would like to watch me doing this then you can also watch this tutorial:

{% youtube rWExnXzUBqc %}

If you are not familiar with concept of **serverless** then it’s just an approach to **running application without managing, configuring and provisioning servers**.

You won’t have to worry about scaling it even at times of peak load.
You won’t have to pay a fixed price. 
You will only pay for the actual use which is calculated in terms of execution time and no. of executions.

Sounds nice! 
Let's see how this can be done?

**First requirement is to have an AWS account.** If you don’t have one, you can easily create one and with free tier plan you can get started for free.

Once you have have it, **Install serverless framework as global dependency using npm**

```bash
npm install -g serverless
```

Next we need AWS access keys. 

To create them go to **IAM management console** of your AWS account and create a new user with programatic access. 

![IAM management console](https://dev-to-uploads.s3.amazonaws.com/i/zz5okludoavzzm5lakva.png)

We need to attach a new policy for this user to provide selective access to AWS services.

Click on **"Select existing policies directly"** and then click on "Create policy" button.
![Select existing policies directly](https://dev-to-uploads.s3.amazonaws.com/i/smwugzqolqhk9bmmf1e8.png)

Select JSON tab.
![JSON](https://dev-to-uploads.s3.amazonaws.com/i/nf87af8cohhfrw32x41f.png)

Copy and paste the json below and Click on **Review Policy**
{% gist https://gist.github.com/ServerlessBot/7618156b8671840a539f405dea2704c8 %}

Give it a name and Click on **create policy**.

Go back to your create user page, Refresh the list, Search for your policy. Select policy and click on Next

![create user page](https://dev-to-uploads.s3.amazonaws.com/i/1uyuuzlvyynsrcytybkf.png)

Click next and finally click on **Create User**.

![Create User](https://dev-to-uploads.s3.amazonaws.com/i/dq1r3vhpdk49cyv8gqtr.png)

Copy the **access key id** and **secret access key** and Setup AWS Keys using this command.
```bash
serverless config credentials --provider aws --key <key> --secret <secret>
```

**Your serverless framework setup is done here.**

To **integrate bref** in your existing laravel project.

First pull in bref package using composer.
```bash
composer require bref/bref
```

Create a new file **serverless.yml** inside your project directory using this command.
```bash
php artisan vendor:publish --tag=serverless-config
```

Now you can edit **serverless.yml** as per your requirement. 

Next we need to change the location of compiled view and a few other things like changing session driver to cookie and log channel to standard error in environment (.env) file.

Open **.env** file and add following:
```bash
CACHE_DRIVER=array
VIEW_COMPILED_PATH=/tmp/storage/framework/views
SESSION_DRIVER=array
LOG_CHANNEL=stderr
```

Another issues that I faced was with caching and changing cache driver to array solved that for me. So if you get errors related to caching then you can try changing this.

```bash
CACHE_DRIVER=array
```

Please note that you can not use localhost for your mysql database here, So you have deploy it on another server and add the credentials here, Or you can even use AWS RDS service.

Next we need to make changes in app service provider so that if compiled view directory is not present then it should recreate it automatically.

So copy and paste below section in boot method of **app/Providers/AppServiceProvider.php**

```php
public function boot()
    {
        //
        // Make sure the directory for compiled views exist
        if (! is_dir(config('view.compiled'))) {
            mkdir(config('view.compiled'), 0755, true);
        }
    }
```

Now since laravel caching breaks in AWS lambda because of different file paths, However it also breaks without package’s and service cache, So to build all cache files run
```bash
php artisan config:cache
```
And to remove config cache file run
```bash
php artisan config:clear
```

And That’s it

To deploy it on AWS Lambda we just need to run 
```bash
serverless deploy 
```
and it will take care of everything else.

Finally you will see your application url, copy it and try it in your browser.

And it should works.

That's it!

Hope you find this helpful!