Skip to content
This repository was archived by the owner on Dec 2, 2020. It is now read-only.

Commit f746086

Browse files
committed
Create runtime image
1 parent 9bc0592 commit f746086

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ RUN mkdir bin && \
3939
RUN curl -sS https://getcomposer.org/installer | ./bin/php -- --install-dir=/runtime/bin --filename=composer && \
4040
./bin/php ./bin/composer require guzzlehttp/guzzle
4141

42+
# Copy in runtime bootstrap
43+
COPY runtime/bootstrap /runtime/
44+
4245
###### Create runtime image ######
4346

4447
FROM lambci/lambda:provided as runtime
4548

46-
COPY --from=builder /runtime /opt
49+
COPY --from=builder /runtime/ /opt/

runtime/bootstrap

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/opt/bin/php
2+
<?php
3+
4+
// This invokes Composer's autoloader so that we'll be able to use Guzzle and any other 3rd party libraries we need.
5+
require __DIR__ . '/vendor/autoload.php';
6+
7+
function getNextRequest()
8+
{
9+
$client = new \GuzzleHttp\Client();
10+
$response = $client->get('http://' . $_ENV['AWS_LAMBDA_RUNTIME_API'] . '/2018-06-01/runtime/invocation/next');
11+
12+
return [
13+
'invocationId' => $response->getHeader('Lambda-Runtime-Aws-Request-Id')[0],
14+
'payload' => json_decode((string) $response->getBody(), true)
15+
];
16+
}
17+
18+
function sendResponse($invocationId, $response)
19+
{
20+
$client = new \GuzzleHttp\Client();
21+
$client->post(
22+
'http://' . $_ENV['AWS_LAMBDA_RUNTIME_API'] . '/2018-06-01/runtime/invocation/' . $invocationId . '/response',
23+
['body' => $response]
24+
);
25+
}
26+
27+
// This is the request processing loop. Barring unrecoverable failure, this loop runs until the environment shuts down.
28+
do {
29+
// Ask the runtime API for a request to handle.
30+
$request = getNextRequest();
31+
32+
// Obtain the function name from the _HANDLER environment variable and ensure the function's code is available.
33+
$handlerFunction = array_slice(explode('.', $_ENV['_HANDLER']), -1)[0];
34+
require_once $_ENV['LAMBDA_TASK_ROOT'] . '/src/' . $handlerFunction . '.php';
35+
36+
// Execute the desired function and obtain the response.
37+
$response = $handlerFunction($request['payload']);
38+
39+
// Submit the response back to the runtime API.
40+
sendResponse($request['invocationId'], $response);
41+
} while (true);

0 commit comments

Comments
 (0)