Laravel Vapor Serverless
Introduction
Laravel Vapor is a serverless deployment platform for Laravel applications, powered by AWS Lambda. It automatically scales your application based on traffic, handles infrastructure management, and provides a seamless deployment experience.
With Vapor, you don't need to worry about server provisioning, scaling, or infrastructure maintenance. Your Laravel application runs on AWS Lambda with automatic scaling from zero to millions of requests.
Vapor Setup
Install the Vapor CLI globally:
composer global require laravel/vapor-cli
vapor login
Initialize Vapor in your Laravel project:
vapor init my-project
This creates a vapor.yml configuration file in your project root.
Deployment
Deploy your application with a single command:
vapor deploy production
The vapor.yml configuration controls your deployment:
id: my-project
name: my-project
runtime: php-8.3
build:
- 'composer install --no-dev'
- 'npm install && npm run production'
memory: 1024
timeout: 30
domains:
- myapp.com
routes:
- /
- '/api/{any}'
database: production-db
Database Management
Create and manage databases through Vapor:
# Create a database
vapor database create production-db
# Get database credentials
vapor database detail production-db
Configure your database in .env:
DB_CONNECTION=pgsql
DB_HOST=production-db.xxxxx.us-east-1.rds.amazonaws.com
DB_DATABASE=vapor
DB_USERNAME=xxx
DB_PASSWORD=xxx
Queues & Workers
Configure queue workers in vapor.yml:
jobs:
- name: default-queue
runtime: php-8.3
memory: 1024
handler: App\Jobs\ProcessPodcast
timeout: 300
queue:
default: 10
priority: 5
Deploy the worker:
vapor job deploy production default-queue
Caching & Sessions
Vapor uses DynamoDB for caching and sessions by default:
CACHE_DRIVER=dynamodb
SESSION_DRIVER=dynamodb
DYNAMODB_CACHE_TABLE=vapor-cache
DYNAMODB_SESSION_TABLE=vapor-sessions
Create the DynamoDB tables:
vapor cache-table create
Environment Variables
Manage environment variables securely:
# Add environment variable
vapor variable set STRIPE_KEY=sk_live_xxx
# List variables
vapor variable list
# Remove variable
vapor variable delete STRIPE_KEY
Variables are encrypted and securely injected into your Lambda functions.
Summary
Laravel Vapor provides an excellent serverless deployment solution for Laravel applications. With automatic scaling, managed databases, and seamless deployments, Vapor lets you focus on writing code while AWS handles the infrastructure.
For more information, check out our other tutorials on Laravel Forge and Laravel Octane.