Download the PHP package netolabs/php-lambda-runtime without Composer
On this page you can find all versions of the php package netolabs/php-lambda-runtime. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download netolabs/php-lambda-runtime
More information about netolabs/php-lambda-runtime
Files in netolabs/php-lambda-runtime
Package php-lambda-runtime
Short Description A library for running PHP code in AWS Lambda
License
Informations about the package php-lambda-runtime
PHP Lambda Runtime library
Requirements
- PHP 7.3+
- Composer
Installation
Run composer require netolabs/php-lambda-runtime
from the root of your project.
You'll need two files in root of your project for your application to work in Lambda, bootstrap
and app.php
.
-
bootstrap
can be copied directly from this library (seebin/bootstrap
) and should not require any modification. app.php
is where you instantiate the App object and define the middleware your application requires. Since thebootstrap
script sets up the autoloader, you do not need to do this in yourapp.php
.
Also required is a PHP binary compiled to run in the AWS Lambda environment. Documentation on how to build your own can be found here.
Deploying to AWS
When you're ready to ship your new function you'll need to create a Zip file containing
app.php
, bootstrap
, src
, vendor
and your PHP binary in bin/php
(or use a Lambda layer).
The Zip file can then be uploaded via the AWS console or your favourite IaC tool.
Using Middleware
This library uses PSR-15 compliant Middleware. If you want to learn more about how to write and use middleware, have a look at these resources:
- https://mwop.net/blog/2018-01-23-psr-15.html
- http://www.slimframework.com/docs/v4/concepts/middleware.html
- https://github.com/middlewares/awesome-psr15-middlewares
While reusable middleware is great for application logic such as authentication, logging, exception handling and such, it's generally not advisable to put business logic in your middleware.
Examples
These are some basic examples of an app.php
file.
Anonymous function
In this example we use an anonymous function to output "hello world" in the response body.
<?php
$app = \Neto\Lambda\Application\AppFactory::create();
$app->addCallable(function() {
return 'hello world';
});
$app->run();
Anonymous function using Response object
You can also return a Response object if you'd like to manipulate the headers, status code, etc.
This example has a header of foo: bar
and a response body of baz
.
<?php
$app = \Neto\Lambda\Application\AppFactory::create();
$app->addCallable(function() {
return new \GuzzleHttp\Psr7\Response(200, [ 'foo' => 'bar' ], 'baz');
});
$app->run();
Adding middleware
This example uses the provided HelloWorld middleware to return a response with modified headers and body.
<?php
$app = \Neto\Lambda\Application\AppFactory::create();
$app->addMiddleware(new \Neto\Lambda\Middleware\HelloWorld())
->run();
If invoked from the command-line, you would expect to see the following
Status code 200
Headers
hello: world
Response body
{"success":true,"message":"Hello world!"}
Duration: 0.006911039352417ms
Testing locally
Invoking from the command line
To run your lambda from the command line, you can simply run vendor/bin/invoke
.
There are two optional parameters, handler name (-h
) and request body data (-d
).
Running a local server
You can also use PHPs built-in web server to test and send requests to your lambda.
Simply start the server by running vendor/bin/start_server handler.name [hostname] [port]
.
You can then send requests to your function via curl, eg: curl --data '{"foo":"bar"}' localhost
License
The MIT License (MIT). Please see the License File for more information.
All versions of php-lambda-runtime with dependencies
ext-json Version *
psr/http-message Version 1.0.*
psr/http-server-handler Version 1.0.*
psr/http-server-middleware Version 1.0.*
psr/container Version ^1.0
guzzlehttp/guzzle Version 6.3.*
ulrichsg/getopt-php Version ^3.2
netolabs/simple-container Version ^0.2.0
php-di/invoker Version ^2.0