Serverless Laravel Applications
In this article, how to make Serverless applications with Laravel Vapor? we will learn
Laravel Vapor is an auto-scaling, serverless deployment platform for Laravel, powered by AWS Lambda. Manage your Laravel infrastructure on Vapor and fall in love with the scalability and simplicity of serverless.
Vapor abstracts the complexity of managing Laravel applications on AWS Lambda, as well as interfacing those applications with SQS queues, databases, Redis clusters, networks, CloudFront CDN, and more. Some highlights of Vapor’s features include:
- Auto-scaling web / queue infrastructure fine tuned for Laravel
- Zero-downtime deployments and rollbacks
- Environment variable / secret management
- Database management, including point-in-time restores and scaling
- Redis Cache management, including cluster scaling
- Database and cache tunnels, allowing for easy local inspection
- Automatic uploading of assets to Cloudfront CDN during deployment
- Unique, Vapor assigned vanity URLs for each environment, allowing immediate inspection
- Custom application domains
- DNS management
- Certificate management and renewal
- Application, database, and cache metrics
- CI friendly
How to Deploy Project with Laravel Vapor?
First you should have account on AWS Server
Create IAM User in AWS
To create an IAM User in the AWS console, navigate to the IAM service on your AWS dashboard. Select “Users” from the left-side navigation panel.
Now you can see there is a Button call “Add User” and click on it to create user.
On the permissions management screen, you may grant full administrator access to the IAM user by selecting the “Attach existing policies directly” option and “AdministratorAccess” policy. Once the policy has been attached, you may click “Next”.
Next select “attach existing policies directly” option and select “AdminitratorAccess” option.
In the next section, click create user button.
After create user you will see “User”, “Access Key ID” and “Secret Access Key” as bellow schreen shot. next you have to use this keys on vapor.
Create Account with Laravel Vapor
in this step, you have to create account in laravel vapor. so go to following links:
Link AWS Account with Laravel Vapor
Now you need to link your AWS account with Laravel Vapor. so let’s go on following link and add “User”, “Access Key ID” and “Secret Access Key” details from AWS that we created on first step:
https://vapor.laravel.com/app/team/settings/cloud-providers.
Installing The Vapor CLI
You will deploy your Laravel Vapor applications using the Vapor CLI (opens new window). This CLI may be installed globally or on a per-project basis using Composer:
composer require laravel/vapor-cli --update-with-dependenciescomposer global require laravel/vapor-cli --update-with-dependencies
Or, if you need to deploy an existing project to Laravel Vapor, you may use the vapor init
CLI command. This command should be executed within the root directory of the Laravel project you wish to deploy. The init
command will prompt you to select the AWS account that the project should be associated with, as well as the AWS region that it should be deployed to.
vapor init
Now you can confirm if vapor cli installed currently or not.
php vendor/bin/vapor list
#Logging In
After you have installed the Vapor CLI, you should authenticate with your Vapor account using the login
command:
vapor login
Initiating Deployments
To initiate a deployment, execute the deploy
CLI command from the root of your application:
vapor deploy production
The init command will generate vapor.yml file in the root directory which contains all the information to deploy the project. Now a new project will be created in the Vapor dashboard.
The vapor.yml file will be the following when created.
id: 3958name: vapor-exampleenvironments:production:memory: 1024cli-memory: 512build:- 'composer install --no-dev'- 'php artisan event:cache'- 'npm ci && npm run prod && rm -rf node_modules'staging:memory: 1024cli-memory: 512build:- 'composer install'- 'php artisan event:cache'- 'npm ci && npm run dev && rm -rf node_modules'
There will be two environments, “production”, and “staging”.
Each environment is assigned a separate vanity URL which you can use to access the application immediately after it is deployed. To view your application you need to deploy it.
Deployments
To initiate a deployment, execute the deploy command through
vapor deploy production
This will generate the deployment for the production environment. You can view the deployment details from the Vapor UI like below.
You may define build hooks and deployment hooks for an environment using the build key and deploy key respectively within your vapor.yml file. You can tell the Vapor to run artisan migrate function after it is deployed or install composer in your local file system before deployment in your vapor.yml file using deploy and build hooks respectively.
Reviewing Output / Logs
When a deployment hook fails, you may review the output / logs via the Vapor UI’s deployment detail screen.
Also, if you are deploying your application using the vapor deploy
command, the CLI output will contain the failing hook output. Of course, you may review the output at any time using the hook:output
command:
vapor hook:output {DEPLOYMENT_HOOK_ID}
You can review the logs associated with the failing hook using the hook:log
command:
vapor hook:log {DEPLOYMENT_HOOK_ID}
You can also view functions on your AWS Lambda Account:
https://console.aws.amazon.com/lambda/home?region=us-east-1#/functions.
The Bottom Line
Vapor makes you super easy to deploy a Laravel application in a serverless environment. The more you use Vapor and get familiar with running serverless, the more you’ll appreciate Vapor. It takes care of a whole lot of wiring behind the scenes that you don’t need to worry about.
Thank you for reading this article.
If you find this article useful please kindly share it with your network and feel free to use the comment section for questions, answers, and contributions.
You might also like :
- Read Also : Laravel Tips and Tricks #2
- Read Also : Laravel 9 Traits Example
- Read Also : Laravel Eloquent Tips and Tricks
- Read Also : Upgrade from Laravel 8 to 9