Skip to content

Commit

Permalink
Merge pull request #1668 from brefphp/lambda-context-function
Browse files Browse the repository at this point in the history
Expose the Lambda context in `LAMBDA_INVOCATION_CONTEXT` in the function runtime
  • Loading branch information
mnapoli authored Nov 5, 2023
2 parents 5a5db92 + 731f8ba commit 378a63a
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/FunctionRuntime/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Bref\FunctionRuntime;

use Bref\Bref;
use Bref\Context\Context;
use Bref\LazySecretsLoader;
use Bref\Runtime\LambdaRuntime;
use Throwable;
Expand Down Expand Up @@ -34,7 +35,17 @@ public static function run(): void
if (++$loops > $loopMax) {
exit(0);
}
$success = $lambdaRuntime->processNextEvent($handler);

$success = $lambdaRuntime->processNextEvent(function ($event, Context $context) use ($handler) {
// Expose the context in an environment variable
// Used for example to retrieve the context in Laravel Queues jobs
$jsonContext = json_encode($context, JSON_THROW_ON_ERROR);
$_SERVER['LAMBDA_INVOCATION_CONTEXT'] = $_ENV['LAMBDA_INVOCATION_CONTEXT'] = $jsonContext;
putenv("LAMBDA_INVOCATION_CONTEXT=$jsonContext");

return $handler($event, $context);
});

// In case the execution failed, we force starting a new process regardless of BREF_LOOP_MAX
// Why: an exception could have left the application in a non-clean state, this is preventive
if (! $success) {
Expand Down

0 comments on commit 378a63a

Please sign in to comment.