laravel/vapor-core
Core runtime and service providers for running Laravel on Vapor (AWS Lambda). Handles serverless bootstrapping and integrations like queues, databases, Redis, networking, and CDN, helping Laravel apps scale smoothly in a serverless environment.
## Getting Started
### Minimal Setup
1. **Installation**:
Add `laravel/vapor-core` to your `composer.json`:
```bash
composer require laravel/vapor-core
Ensure your config/app.php includes the VaporServiceProvider under providers:
Laravel\Vapor\Core\VaporServiceProvider::class,
First Use Case: Check if your app is running on Vapor in any controller/middleware:
use Laravel\Vapor\Core\Facades\Vapor;
if (Vapor::running()) {
// Vapor-specific logic (e.g., optimize for cold starts)
\Log::info('Running on Vapor!');
}
Key Entry Points:
Vapor::running() to conditionally execute Vapor-specific logic.config('vapor.*') or env('VAPOR_*').Vapor::executionContext() to log Lambda metadata (e.g., awsRequestId).Where to Look First:
vendor/laravel/vapor-core/src/Facades/Vapor.php for API reference.config/vapor.php (auto-generated) for Vapor-specific configurations.if (!Vapor::running()) {
$this->loadHeavyDependencies();
}
if (Vapor::running()) {
DB::disconnect();
DB::reconnect();
}
Vapor::isSqsEvent() to handle SQS-triggered Lambda invocations:
if (Vapor::isSqsEvent()) {
$event = Vapor::event();
// Process SQS message
}
Vapor::event() for custom routing:
$request = Vapor::event()->get('body');
$config = Vapor::running()
? config('vapor.optimized_settings')
: config('local.settings');
env('VAPOR_REGION') or config('vapor.region') to fetch Vapor-specific AWS regions.\Log::info('Processing request', [
'awsRequestId' => Vapor::executionContext()['awsRequestId'],
'functionName' => Vapor::executionContext()['functionName'],
]);
Vapor::log() for structured JSON logs compatible with AWS CloudWatch:
Vapor::log('user.created', ['user_id' => 123]);
VaporOctaneHandler:
use Laravel\Vapor\Core\Octane\VaporOctaneHandler;
return new class extends VaporOctaneHandler {
protected function configure(): void {
$this->withFileStorage();
}
};
Vapor::queue() to interact with SQS queues directly:
Vapor::queue('orders')->push(new ProcessOrder($orderId));
config/vapor.php:
'queues' => [
'orders' => [
'maxRetries' => 3,
'visibilityTimeout' => 30,
],
],
config/filesystems.php:
'disks' => [
's3' => [
'driver' => 's3',
'url' => env('S3_ENDPOINT', 'https://s3.amazonaws.com'),
// ... other config
],
],
Route::vapor() to define Vapor-specific routes (e.g., API Gateway integrations):
Route::vapor('GET', '/webhook', [WebhookController::class, 'handle']);
public function handle($request, Closure $next) {
if (Vapor::running() && !$request->hasHeader('x-custom-header')) {
abort(403);
}
return $next($request);
}
AppServiceProvider:
if (Vapor::running()) {
$this->commands([
\App\Console\Commands\Vapor\OptimizeCommand::class,
]);
}
config('database.connections.mysql.host') to dynamically resolve Vapor-provided endpoints.Cache::tags() for invalidation:
Cache::tags(['vapor-cdn'])->put('key', 'value', now()->addHours(1));
vapor:test Artisan command to simulate Lambda invocations:
php artisan vapor:test --event=api-gateway
Vapor::running() in tests:
Vapor::shouldReceive('running')->andReturn(true);
Cold Start Latency:
php-redis, pdo_mysql) increase cold start times.Vapor::logColdStart():
if (Vapor::running()) {
Vapor::logColdStart();
}
Connection Leaks:
if (Vapor::running()) {
DB::disconnect();
Redis::connection()->disconnect();
}
DB::retryUsing() for transient failures:
DB::retryUsing(function () {
return DB::connection()->getPdo();
});
Timeout Handling:
\Log::info('Remaining time:', [
'seconds' => Vapor::executionContext()['remainingTime'] ?? 0,
]);
Environment Variable Conflicts:
.env vars may override Vapor’s VAPOR_* vars.config('vapor.*') instead of env() where possible.VAPOR_ENV over APP_ENV for Vapor-specific logic.Multipart Form Data:
Vapor::parseMultipart():
$data = Vapor::parseMultipart($request->input());
Octane + Vapor:
VaporOctaneHandler and avoid blocking calls in Octane workers.if (Vapor::running() && $this->isCli()) {
$this->disableOctane();
}
SQS Visibility Timeouts:
How can I help you explore Laravel packages today?