netolabs/php-lambda-runtime
Composer 安装命令:
composer require netolabs/php-lambda-runtime
包简介
A library for running PHP code in AWS Lambda
README 文档
README
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.
-
bootstrapcan be copied directly from this library (seebin/bootstrap) and should not require any modification. -
app.phpis where you instantiate the App object and define the middleware your application requires. Since thebootstrapscript 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.
netolabs/php-lambda-runtime 适用场景与选型建议
netolabs/php-lambda-runtime 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 11 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 netolabs/php-lambda-runtime 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 netolabs/php-lambda-runtime 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2021-11-03