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

Function Hooks

There are a few ways to interact with your functions during the deployment and activation process.

Sidecar dispatches global events as it's deploying and activating functions, and it also calls instance methods on each function.

The following four methods are available to you on every Sidecar function:

  • beforeDeployment
  • afterDeployment
  • beforeActivation
  • afterActivation

Example: Before Deployment

The beforeDeployment hook is a great place to run a build step if your function requires it. We'll be using the ncc build command mentioned in the Handlers & Packages section for this example.

In this example, we'll use Symfony's Process component to run the ncc command, so that we never forget to build our bundle before we deploy.

1use Symfony\Component\Process\Process;
2 
3class ExampleFunction extends LambdaFunction
4{
5 public function beforeDeployment()
6 {
7 Sidecar::log('Compiling bundle with NCC.');
8 
9 $command = ['ncc', 'build', 'resources/lambda/image.js', '-o', 'resources/lambda/dist'];
10 
11 Sidecar::log('Running `' . implode(' ', $command). '`');
12 
13 $process = new Process($command, $cwd = base_path(), $env = []);
14 
15 $process->setTimeout(60)->disableOutput()->mustRun();
16 
17 Sidecar::log('Bundle compiled!');
18 }
19}

With this in place, you'll see something like this in your logs:

1[Sidecar] Deploying App\Sidecar\Example to Lambda as `SC-App-local-Sidecar-Example`.
2 ↳ Environment: local
3 ↳ Runtime: nodejs14.x
4 ↳ Compiling bundle with NCC.
5 ↳ Running `ncc build resources/lambda/image.js -o resources/lambda/dist`
6 ↳ Bundle compiled!
7 ↳ Function already exists, potentially updating code and configuration.
8 ↳ Packaging files for deployment.
9 ↳ Creating a new zip file.
10 ↳ Zip file created at s3://sidecar-us-east-2-XXXX/sidecar/001-7a2e86d9853b10c97b970af51d101f8d.zip
11 ↳ Function code and configuration updated.
Code highlighting powered by torchlight.dev, a Hammerstone product.