-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap
More file actions
executable file
·63 lines (52 loc) · 1.89 KB
/
bootstrap
File metadata and controls
executable file
·63 lines (52 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/opt/bin/php
<?php
// require vendors from layer
require __DIR__.'/vendor/autoload.php';
// require file with function basing on ENV configuration
$handlerFunction = array_slice(explode('.', $_ENV['_HANDLER']), -1)[0];
require_once $_ENV['LAMBDA_TASK_ROOT'] . '/src/' . $handlerFunction . '.php';
// Generate random number to test Lambda Execution Context
$sharedRandom = rand();
$client = new \GuzzleHttp\Client();
while (true) {
$request = getNextRequest($client);
try {
$response = $handlerFunction($request['payload'], $sharedRandom);
} catch (\Exception $e) {
// Handle invocation error and notify Runtime API
handleInvocationError($client, $request['invocationId'], $e);
continue;
}
sendResponse($client, $request['invocationId'], $response);
}
function getNextRequest($client): array
{
$response = $client->get('http://'.$_ENV['AWS_LAMBDA_RUNTIME_API'].'/2018-06-01/runtime/invocation/next');
return [
'invocationId' => $response->getHeader('Lambda-Runtime-Aws-Request-Id')[0],
'payload' => json_decode((string) $response->getBody(), true)
];
}
function sendResponse($client, $invocationId, $response)
{
$client->post(
'http://' . $_ENV['AWS_LAMBDA_RUNTIME_API'] . '/2018-06-01/runtime/invocation/' . $invocationId . '/response',
['body' => $response]
);
}
function handleInvocationError($client, $invocationId, \Exception $e)
{
$client->post(
'http://' . $_ENV['AWS_LAMBDA_RUNTIME_API'] . '/2018-06-01/runtime/invocation/' . $invocationId . '/error',
[
'body' => json_encode([
'errorType' => 'CustomInvocationError',
'errorMessage' => $e->getMessage(),
'stackTrace' => $e->getTraceAsString()
]),
'headers' => [
"Lambda-Runtime-Function-Error-Type" => "Unhandled"
]
]
);
}