Skip to content

hebertcisco/laravel-serverless-boilerplate

Repository files navigation

Laravel Logo

It is important to note that this example can be improved, but it is an introductory example and I intend to bring more content of this type with more examples, consuming, for example, resources from cloud providers, such as SQS for queues, S3 for Storange, etc.

🏗 Laravel CI

Serverless applications are becoming increasingly popular due to their ability to automatically scale and reduce operational costs.

PHP is a programming language widely used for web development and can be used to create efficient and scalable Serverless applications.

Using this kind of approach when providing a PHP with Serverless application, we have a very big gain in time, money and mainly, scalability.

However, with this, we will no longer, for example, pay a fixed price for hosting, often with a fixed and small size of resources, such as memory and processing. In addition to the configuration being much more laborious and costing much more effort in a traditional hosting.

My example will use PHP version 8.1 and the Bref package. We will also use Laravel.

To get started, it's important to have the PHP environment set up with Composer, and also Node.js installed, to ensure that our tutorial flows.

First, we must install the Serverless Framework CLI, using the following command NPM:

npm install -g serverless

It is important to note that Bref is only compatible with versions above 3 of the Serverless Framework (which is the current version).

Have your AWS credentials in hand so that we can proceed with the tutorial, where you will have to configure Serverless with your credentials with the following command:

serverless config credentials --provider aws --key <key> --secret <secret>

With that, we will create a common project with Laravel, using the create-project command by composer:

composer create-project laravel/laravel laravel-serverless-boilerplate

Bref

With your new project open, install Laravel Bref using composer:

composer require bref/bref bref/laravel-bridge --update-with-dependencies

Now, with Bref installed, you will have Bref executables available within your vendor, knowing that, we will run the Bref initialization command:

php artisan vendor:publish --tag=serverless-config

Deploy

Finally, we reached the final stretch, the implementation of this idea. To launch it, just run the commands below:

We don't want to deploy "dev" caches that were generated on our machine (because the paths will be different in AWS Lambda). Let's clean up before going upstairs:

php artisan config:clear

When running on AWS Lambda, the Laravel application will automatically cache its configuration during startup. You don't need to run php crafter config:cache before deploying.

serverless deploy

When finished, the deploy command will show the URL of the application.

Know that this is the basics, this Serverless configuration is minimal and can be extended and adapted to your needs. So if you want to deploy to production or learn more about Serverless, follow me on YouTube for more content, (I want to create more related content)

Links

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!

Or buy me a coffee 🙌🏾

📝 License

Copyright © 2023 Hebert F Barros.
This project is MIT licensed.