Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Runtime/LambdaRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ public function processNextEvent(Handler | RequestHandlerInterface | callable $h

// Expose the context in an environment variable
$this->setEnv('LAMBDA_INVOCATION_CONTEXT', json_encode($context, JSON_THROW_ON_ERROR));
// These are used for logging/tracing purposes
$this->setEnv('LAMBDA_REQUEST_ID', $context->getAwsRequestId());
$this->setEnv('_X_AMZN_TRACE_ID', $context->getTraceId());

try {
ColdStartTracker::invocationStarted();
Expand Down
14 changes: 14 additions & 0 deletions tests/Runtime/LambdaRuntimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,4 +431,18 @@ public function afterInvoke(mixed ...$params): void
$this->assertStringContainsString('Invoke Error {"errorType":"Exception","errorMessage":"Invocation error","stack":', $this->getActualOutput());
$this->assertStringContainsString('Invoke Error {"errorType":"Exception","errorMessage":"This is an exception in afterInvoke","stack":', $this->getActualOutput());
}

public function test request id env variables()
{
$this->givenAnEvent([]);

$this->runtime->processNextEvent(function () use (&$requestId, &$traceId) {
$requestId = getenv('LAMBDA_REQUEST_ID');
$traceId = getenv('_X_AMZN_TRACE_ID');
return ['hello' => 'world'];
});

$this->assertSame('1', $requestId);
$this->assertSame('Root=1-67891233-abcdef012345678912345678', $traceId);
}
}
1 change: 1 addition & 0 deletions tests/RuntimeTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ protected function givenAnEvent(mixed $event): void
[
'lambda-runtime-aws-request-id' => '1',
'lambda-runtime-invoked-function-arn' => 'test-function-name',
'lambda-runtime-trace-id' => 'Root=1-67891233-abcdef012345678912345678',
],
json_encode($event, JSON_THROW_ON_ERROR)
),
Expand Down
Loading