DEV Community

Cover image for Deploy Serverless Laravel application using bref
Chandresh Singh
Chandresh Singh

Posted on

Deploy Serverless Laravel application using bref

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:

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

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

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

Select JSON tab.
JSON

Copy and paste the json below and Click on Review Policy

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

Click next and finally click on Create User.

Create User

Copy the access key id and secret access key and Setup AWS Keys using this command.

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.

composer require bref/bref

Create a new file serverless.yml inside your project directory using this command.

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:

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.

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

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

php artisan config:cache

And to remove config cache file run

php artisan config:clear

And That’s it

To deploy it on AWS Lambda we just need to run

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!

Top comments (6)

Collapse
 
agungdarmanto profile image
Agung Darmanto

Great article! Just want to clarify that command php artisan vendor:publish --tag=serverless-config only works when install composer require bref/laravel-bridge which is missing from your article.

Collapse
 
chandreshhere profile image
Chandresh Singh

This command worked for me without installing this package. However you only need that package if you want to use SQS. I have written about queue setup on SQS using this package here: dev.to/chandreshhere/setup-queue-w...

Collapse
 
bmitch profile image
Bill Mitchell

Thanks, I just ran into the same issue and that fixed it.

Collapse
 
amitavroy7 profile image
Amitav Roy

So we really need so many permissions on AWS for this? I am not sure if we require all of this.

Collapse
 
willvincent profile image
Will Vincent

Yeah, that permissions list is a bit... cavalier.

Collapse
 
hamm profile image
Hamm

Thanks is worked for me.
I'm using bref (PHP 8.1)
👍