commit f60d9f1fb667c1241ad01ce7530056d0c789e1f3 Author: Sergio Álvarez Date: Mon Nov 11 17:37:47 2019 +0100 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c002039 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +composer* +deploy.md +runtime* +vendor* diff --git a/README.md b/README.md new file mode 100644 index 0000000..42908ee --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +`./compile.sh` will compile PHP, and `.build.sh` will generate the `.zip`s. diff --git a/bootstrap b/bootstrap new file mode 100755 index 0000000..8e52580 --- /dev/null +++ b/bootstrap @@ -0,0 +1,60 @@ +#! /opt/bin/php -c /opt/etc/php.ini +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); + diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..ae5f345 --- /dev/null +++ b/build.sh @@ -0,0 +1,31 @@ +#! /bin/bash + +rm -rf runtime +mkdir -p runtime/{bin,etc,lib,ext} + +cp bootstrap runtime/ +cp ~/php-7.4.0RC6-build/bin/php runtime/bin/ +cp ~/php-7.4.0RC6/php.ini-production runtime/etc/php.ini +cp ~/php-7.4.0RC6-build/lib/php/extensions/no-debug-non-zts-20190902/* runtime/ext/ + +echo "extension_dir=/opt/ext" >> runtime/etc/php.ini +echo "extension=redis.so" >> runtime/etc/php.ini +echo "extension=opcache.so" >> runtime/etc/php.ini + +for lib in libncurses.so.5 libtinfo.so.5 libpcre.so.0; do + cp "/lib64/${lib}" runtime/lib/ +done + +for lib in libonig.so.2; do + cp "/usr/lib64/${lib}" runtime/lib/ +done + +curl -sS https://getcomposer.org/installer | runtime/bin/php +runtime/bin/php composer.phar require aws/aws-sdk-php + +cd runtime +zip -r runtime.zip * +mv runtime.zip ../ +cd .. + +zip -r vendor.zip vendor diff --git a/compile.sh b/compile.sh new file mode 100755 index 0000000..d3bffeb --- /dev/null +++ b/compile.sh @@ -0,0 +1,20 @@ +#! /bin/bash + +START=$(pwd) + +cd ~ +wget https://downloads.php.net/~derick/php-7.4.0RC6.tar.gz +tar zxvf php-7.4.0RC6.tar.gz +cd php-7.4.0RC6 + +./configure --prefix=/home/ec2-user/php-7.4.0RC6-build/ --with-curl --with-zlib --with-openssl --enable-bcmath --enable-mbstring --with-mysqli --enable-sockets --without-sqlite3 --disable-fileinfo --disable-cgi +make install + +cd ~ +wget -O phpredis-5.1.1.tar.gz https://github.com/phpredis/phpredis/archive/5.1.1.tar.gz +tar zxvf phpredis-5.1.1.tar.gz +cd phpredis-5.1.1 + +~/php-7.4.0RC6-build/bin/phpize +./configure --with-php-config=/home/ec2-user/php-7.4.0RC6-build/bin/php-config +make install