hammerstone/sidecar
Sidecar lets Laravel package, deploy, and invoke AWS Lambda functions directly from your app. Define a simple PHP class plus the files to ship, choose any supported runtime (Node, Python, Java, .NET, Ruby, or OS-only), and execute from PHP.
Installation
composer require hammerstone/sidecar
php artisan vendor:publish --provider="Hammerstone\Sidecar\SidecarServiceProvider" --tag="sidecar-config"
Publish the config and migration files.
Configuration
Edit config/sidecar.php to define your Lambda functions:
'functions' => [
'process-payment' => [
'handler' => 'App\Services\PaymentProcessor',
'memory' => 512,
'timeout' => 30,
'environment' => [
'APP_ENV' => env('APP_ENV'),
],
],
],
Run migrations:
php artisan migrate
First Use Case Define a Lambda handler class:
namespace App\Services;
use Hammerstone\Sidecar\Contracts\LambdaHandler;
class PaymentProcessor implements LambdaHandler
{
public function handle($event)
{
// Process payment logic
return ['status' => 'success'];
}
}
Trigger it via HTTP or CLI:
php artisan sidecar:invoke process-payment '{"data": "test"}'
Local Development
Use the sidecar:serve command to test Lambdas locally:
php artisan sidecar:serve
This spins up a local AWS-like environment for debugging.
Deployment Integration
Hook into Laravel’s deploy event in AppServiceProvider:
public function boot()
{
Sidecar::afterDeploy(function () {
// Post-deploy Lambda updates (e.g., warm-up calls)
});
}
Event-Driven Lambdas Subscribe to Laravel events and trigger Lambdas:
use Hammerstone\Sidecar\Facades\Sidecar;
event(new PaymentProcessed($payment));
Sidecar::invoke('process-payment', ['payment_id' => $payment->id]);
Sidecar::sharedState() to pass data between Lambdas and your main app..env:
'environment' => [
'STRIPE_KEY' => env('STRIPE_KEY'),
],
Sidecar::invokeAsync() for fire-and-forget tasks:
Sidecar::invokeAsync('cleanup-temp-files');
Cold Starts Lambdas may have latency on first invocation. Mitigate with:
sidecar.php:
'provisioned_concurrency' => [
'process-payment' => 2,
],
Environment Mismatches
Ensure APP_ENV and other env vars match between your app and Lambda. Use:
'environment' => array_merge(
['APP_ENV' => env('APP_ENV')],
config('services.aws.default', [])
),
Dependency Hell
Lambdas share the same composer.json as your app. Use sidecar:prune to clean up unused dependencies:
php artisan sidecar:prune
php artisan sidecar:logs process-payment
sidecar.php:
'aws' => [
'xray_enabled' => true,
],
sidecar:serve --debug to attach Xdebug.Custom Handlers
Extend LambdaHandler to add middleware:
class AuthenticatedPaymentProcessor extends PaymentProcessor
{
public function handle($event)
{
if (!auth()->check()) {
throw new \RuntimeException('Unauthorized');
}
return parent::handle($event);
}
}
Pre/Post Hooks
Override SidecarServiceProvider to add logic before/after invocations:
public function boot()
{
Sidecar::beforeInvoke(function ($name, $payload) {
\Log::info("Invoking Lambda: {$name}");
});
}
Custom Storage
Replace the default DynamoDB storage with RDS or another backend by binding a custom Storage contract:
$this->app->bind(
\Hammerstone\Sidecar\Contracts\Storage::class,
\App\Services\CustomLambdaStorage::class
);
How can I help you explore Laravel packages today?