123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- #! /opt/bin/php -c /opt/etc/php.ini
- <?php
- // This invokes Composer's autoloader so that we'll be able to use Guzzle and any other 3rd party libraries we need.
- require __DIR__ . '/vendor/autoload.php';
- function getNextRequest()
- {
- $client = new \GuzzleHttp\Client();
- $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($invocationId, $response)
- {
- $client = new \GuzzleHttp\Client();
- $client->post(
- 'http://' . $_ENV['AWS_LAMBDA_RUNTIME_API'] . '/2018-06-01/runtime/invocation/' . $invocationId . '/response',
- ['body' => $response]
- );
- }
- function sendError($invocationId, $error)
- {
- $client = new \GuzzleHttp\Client();
- $client->post(
- 'http://' . $_ENV['AWS_LAMBDA_RUNTIME_API'] . '/2018-06-01/runtime/invocation/' . $invocationId . '/error',
- ['body' => $error]
- );
- }
- // This is the request processing loop. Barring unrecoverable failure, this loop runs until the environment shuts down.
- do {
- // Ask the runtime API for a request to handle.
- $request = getNextRequest();
- try {
- // Obtain the function name from the _HANDLER environment variable and ensure the function's code is available.
- $handlerFunction = array_slice(explode('.', $_ENV['_HANDLER']), -1)[0];
- require_once $_ENV['LAMBDA_TASK_ROOT'] . '/src/' . $handlerFunction . '.php';
- // Execute the desired function and obtain the response.
- $response = $handlerFunction($request['payload']);
- // Submit the response back to the runtime API.
- sendResponse($request['invocationId'], $response);
- } catch (Throwable $e) {
- sendError($request['invocationId'], (string)$e);
- }
- } while (true);
|