Browse Source

first commit

Sergio Álvarez 1 year ago
commit
f60d9f1fb6
5 changed files with 116 additions and 0 deletions
  1. 4 0
      .gitignore
  2. 1 0
      README.md
  3. 60 0
      bootstrap
  4. 31 0
      build.sh
  5. 20 0
      compile.sh

+ 4 - 0
.gitignore

@@ -0,0 +1,4 @@
+composer*
+deploy.md
+runtime*
+vendor*

+ 1 - 0
README.md

@@ -0,0 +1 @@
+`./compile.sh` will compile PHP, and `.build.sh` will generate the `.zip`s.

+ 60 - 0
bootstrap

@@ -0,0 +1,60 @@
+#! /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);
+

+ 31 - 0
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

+ 20 - 0
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