Initialise application for AWS Lambda


#1

Hi all, I am trying to adapt my commands to make them work with AWS Lambda and custom runtimes. I have a basic function working; as many tutorial said, the Lambda handler should be a function that receives only one param that contains the Lambda parameters; for example:

<?php
require __DIR__ . '/../vendor/autoload.php';
function hello($data) {
    return $data["name"];
}

Now that I have this working, I would like to do some work with CakePHP. So I must initialise the application, to, for example, use TableRegistry. How can I do this? I can’t use Shell or Commands, as the Lambda handler must be a function with that signature.


#2

I think I have it:

<?php
require dirname(__DIR__) . '/config/requirements.php';
require dirname(__DIR__) . '/vendor/autoload.php';

use App\Application;
use Cake\Cache\Cache;
use Cake\Log\Log;


function lambdatest($data) {

    $app = new Application(dirname(__DIR__) . '/config');
    $app->bootstrap();
    $app->pluginBootstrap();

    Cache::disable();
    Log::drop("debug");
    Log::drop("error");
    Log::drop("sqllog");

    return \Cake\ORM\TableRegistry::getTableLocator()->get("Users")->find()->count();

}

I had to disable all loggers, because I get the same errors than with commands (Could not load class debug).