Sidecar — Deploy and execute AWS Lambda functions from your Laravel application.

Sidecar for Laravel Overview

Hammerstone Refine has not launched yet. Sign up for the early access list to stay up to date!

Sidecar packages, deploys, and executes AWS Lambda functions from your Laravel application.

It works with any Laravel 7 or Laravel 8 application, hosted anywhere, including your local machine.

You can write functions in any of the following runtimes and execute them straight from PHP:

  • Node.js 14
  • Node.js 12
  • Node.js 10
  • Python 3.8
  • Python 3.7
  • Python 3.6
  • Python 2.7
  • Ruby 2.7
  • Ruby 2.5
  • Java 11
  • Java 8
  • Go 1.x
  • .NET Core 3.1
  • .NET Core 2.1

Any runtime that Lambda supports, you can use!

What A Sidecar Function Looks Like

Every Sidecar Function requires two things:

  • A PHP class
  • Files that you want deployed to Lambda (called a "package")

For example, if you want to use Node on Lambda to generate an og:image for all of your blog posts, you would first set up a simple class in PHP called e.g. OgImage.

App\Sidecar\OgImage.php

1namespace App\Sidecar;
2 
3use Hammerstone\Sidecar\LambdaFunction;
4 
5class OgImage extends LambdaFunction
6{
7 public function handler()
8 {
9 // Define your handler function.
10 // (Javascript file + export name.)
11 return 'resources/lambda/image.handler';
12 }
13 
14 public function package()
15 {
16 // All files and folders needed for the function.
17 return [
18 'resources/lambda',
19 ];
20 }
21}

That's it! There are a lot more options, but that's all that is required.

The second thing you'd need is your function's "handler", in this case a javascript file.

Here's a simple JS file that could serve as our handler:

resources/lambda/image.js

1const {createCanvas} = require('canvas')
2 
3exports.handler = async function (event) {
4 const canvas = createCanvas(1200, 630)
5 const context = canvas.getContext('2d')
6 
7 context.font = 'bold 70pt Helvetica'
8 context.textAlign = 'center'
9 context.fillStyle = '#3574d4'
10 
11 // Read the text out of the event passed in from PHP.
12 context.fillText(event.text, 600, 170);
13 
14 // Return an image.
15 return canvas.toDataURL('image/jpeg');
16}
Code highlighting powered by torchlight.dev, a Hammerstone product.

With those files created, you can deploy this function to Lambda:

1php artisan sidecar:deploy --activate

And then execute it straight from your Laravel app!

web.php

1Route::get('/ogimage', function () {
2 return OgImage::execute([
3 'text' => 'PHP to JS and Back Again!'
4 ]);
5});

Sidecar passes the payload from execute over to your Javascript function. Your Javascript function generates an image and sends it back to PHP.

Why Sidecar Exists

AWS Lambda is a powerful service that allows you to run code without provisioning or thinking about servers.

Laravel Vapor brought that power to Laravel. Using Vapor, you can run your plain ol' Laravel apps on a serverless platform and get incredible speed, security, and reliability.

Using Lambda through Vapor is a wonderful developer experience, but there are times when building your applications that you need to run just one or two Node functions for some reason. Common use cases could be taking screenshots with headless Chrome, generating images, or doing server-side rendering of your Javascript frontend.

Or maybe you want to run a Python script without configuring a server? Or a single Ruby script. Or even Java!

When running on a serverless platform, it's not quite as easy as installing Node and running your functions. You don't have access to the server! So you end up deploying a single Vercel or Netlify function and calling it over HTTP, or just forgetting the thing altogether.

Sidecar brings the ease of Vapor to those non-PHP functions.

What Sidecar Doesn't Do

Sidecar does not handle any API Gateway, Databases, Caches, etc. The only thing Sidecar concerns itself with is packaging, deploying, and executing Lambda functions.

If you need other services, you are encouraged to use the instances that Vapor has set up for you, or set them up yourself.

Sidecar does not provide a way to execute a function via HTTP. You must execute it from your Laravel app through the provided methods.

Finally, Sidecar doesn't replace Vapor in any way. In fact, PHP is not even one of the default AWS supported runtimes!