xp-forge/lambda
Composer 安装命令:
composer require xp-forge/lambda
包简介
AWS Lambda for the XP Framework
README 文档
README
Serverless infrastructure.
Example
Put this code in a file called Greet.class.php:
use com\amazon\aws\lambda\Handler; class Greet extends Handler { /** @return callable|com.amazon.aws.lambda.Lambda|com.amazon.aws.lambda.Streaming */ public function target() { return fn($event, $context) => sprintf( 'Hello %s from PHP %s via %s @ %s', $event['name'], PHP_VERSION, $context->functionName, $context->region ); } }
The two parameters passed are $event (a value depending on where the lambda was invoked from) and $context (a Context instance, see below).
Initialization
If you need to run any initialization code, you can do so before returning the lambda from target(). This code is only run once during the init phase:
use com\amazon\aws\lambda\Handler; class Greet extends Handler { /** @return callable|com.amazon.aws.lambda.Lambda|com.amazon.aws.lambda.Streaming */ public function target() { $default= $this->environment->properties('task')->readString('greet', 'default'); return fn($event, $context) => sprintf( 'Hello %s from PHP %s via %s @ %s', $event['name'] ?? $default, PHP_VERSION, $context->functionName, $context->region ); } }
The lambda's environment accessible via $this->environment is an Environment instance, see below.
Logging
To write output to the lambda's log stream, use trace():
use com\amazon\aws\lambda\Handler; class Greet extends Handler { /** @return callable|com.amazon.aws.lambda.Lambda|com.amazon.aws.lambda.Streaming */ public function target() { return function($event, $context) { $this->environment->trace('Invoked with ', $event); return sprintf(/* Shortened for brevity */); }; } }
Any non-string arguments passed will be converted to string using util.Objects::stringOf(). To integrate with XP logging, pass the environment's writer to the console appender, e.g. by using $cat= Logging::all()->toConsole($this->environment->writer).
Response streaming
This library supports AWS Lambda response streaming as announced by AWS in April 2023. To use the stream, return a function(var, Stream, Context) from the handler's target() method instead of a function(var, Context):
use com\amazon\aws\lambda\{Context, Handler, Stream}; class Streamed extends Handler { public function target(): callable { return function($event, Stream $stream, Context $context) { $stream->use('text/plain'); $stream->write("[".date('r')."] Hello world...\n"); sleep(1); $stream->write("[".date('r')."] ...from Lambda\n"); $stream->end(); }; } }
Invoking this lambda will yield the following:
The Stream interface is defined as follows:
public interface com.amazon.aws.lambda.Stream extends io.streams.OutputStream, lang.Closeable { public function transmit(io.Channel|io.streams.InputStream $source, string $mimeType): void public function use(string $mimeType): void public function write(string $bytes): void public function end(): void public function flush(): void public function close(): var }
Development
To run your lambda locally, use the following:
$ xp lambda run Greet '{"name":"Timm"}'
Hello Timm from PHP 8.2.11 via Greet @ test-local-1
This does not provide a complete lambda environment, and does not have any execution limits imposed on it! To detect this programmatically, use $this->environment->local(), which will return true.
Integration testing
To test your lambda inside a local containerized lambda environment, use the test command.
$ xp lambda test Greet '{"name":"Timm"}' START RequestId: 9ff45cda-df9b-1b8c-c21b-5fe27c8f2d24 Version: $LATEST END RequestId: 9ff45cda-df9b-1b8c-c21b-5fe27c8f2d24 REPORT RequestId: 9ff45cda-df9b-1b8c-c21b-5fe27c8f2d24 Init Duration: 922.19 ms... "Hello Timm from PHP 8.2.11 via test @ us-east-1"
This functionality is provided by the AWS Lambda base images for custom runtimes. Although this also runs on your machine, $this->environment->local() will return false.
Setup
The first step is to create and publish the runtime layer:
$ xp lambda runtime $ aws lambda publish-layer-version \ --layer-name lambda-xp-runtime \ --zip-file fileb://./runtime-X.X.X.zip \ --region us-east-1
...and create a role:
$ cat > /tmp/trust-policy.json { "Version": "2012-10-17", "Statement": [{ "Effect": "Allow", "Principal": {"Service": "lambda.amazonaws.com"}, "Action": "sts:AssumeRole" }] } $ aws iam create-role \ --role-name InvokeLambda \ --path "/service-role/" \ --assume-role-policy-document file:///tmp/trust-policy.json
After ensuring your dependencies are up-to-date using composer, create the function:
$ xp lambda package Greet.class.php $ aws lambda create-function \ --function-name greet \ --handler Greet \ --zip-file fileb://./function.zip \ --runtime provided.al2 \ --role "arn:aws:iam::XXXXXXXXXXXX:role/service-role/InvokeLambda" \ --region us-east-1 \ --layers "arn:aws:lambda:us-east-1:XXXXXXXXXXXX:layer:lambda-xp-runtime:1"
Invocation
To invoke the function:
$ aws lambda invoke \ --cli-binary-format raw-in-base64-out \ --function-name greet \ --payload '{"name":"Timm"}' response.json $ cat response.json "Hello Timm from PHP 8.0.10 via greet @ us-east-1"
Deploying changes
After having initially created your lambda, you can update its code as follows:
$ xp lambda package Greet.class.php $ aws lambda update-function-code \ --function-name greet \ --zip-file fileb://./function.zip \ --publish
Upgrading the runtime
To upgrade an existing runtime layer, build the new runtime and publish a new version by calling the following to create a new version:
$ xp lambda runtime $ aws lambda publish-layer-version \ --layer-name lambda-xp-runtime \ --zip-file fileb://./runtime-X.X.X.zip \ --region us-east-1
Now, switch the function over to use this new layer:
$ aws lambda update-function-configuration \
--function-name greet \
--layers "arn:aws:lambda:us-east-1:XXXXXXXXXXXX:layer:lambda-xp-runtime:2"
Using other AWS services
In order to programmatically use other AWS services use the ServiceEndpoint class:
use com\amazon\aws\{Credentials, ServiceEndpoint}; use com\amazon\aws\lambda\Handler; class WebSockets extends Handler { /** @return callable|com.amazon.aws.lambda.Lambda|com.amazon.aws.lambda.Streaming */ public function target() { return function($event, $context) { // Send message to WebSocket connection $this->environment->endpoint('execute-api') ->in($context->region) ->using($event['requestContext']['apiId']) ->resource('/{stage}/@connections/{connectionId}', $event['requestContext']) ->transmit(['message' => 'Reply']) ; return ['statusCode' => 200]; }; } }
To test this locally, pass the necessary environment variables via -e on the command line:
$ xp lambda test -e AWS_ACCESS_KEY_ID=... -e AWS_SECRET_ACCESS_KEY=... WebSockets '{"requestContext":...}' # ...
Context
The context object passed to the target lambda is defined as follows:
public class com.amazon.aws.lambda.Context implements lang.Value { public string $awsRequestId public string $invokedFunctionArn public string $traceId public string $clientContext public string $cognitoIdentity public string $deadline public string $functionName public string $functionVersion public string $memoryLimitInMB public string $logGroupName public string $logStreamName public string $region public int $payloadLength public function __construct(array $headers, array $environment) public function remainingTime(?float $now): ?float public function toString(): string public function hashCode(): string public function compareTo(var $value): int }
Environment
The runtime environment is defined as follows:
public class com.amazon.aws.lambda.Environment { public string $root public [:string] $variables public io.streams.StringWriter $writer public util.PropertySource $properties public function __construct(string $root, ?io.streams.StringWriter $writer) public function taskroot(): io.Path public function path(string $path): io.Path public function tempDir(): io.Path public function local(): bool public function variable(string $name): ?string public function credentials(): com.amazon.aws.Credentials public function trace(var... $args): void public function properties(string $name): util.PropertyAccess }
Interfaces
Instead of functions, a handler's target() method may also return instances implementing the Lambda or Streaming interfaces:
public interface com.amazon.aws.lambda.Lambda { public function process(var $event, com.amazon.aws.lambda.Context $context): var } public interface com.amazon.aws.lambda.Streaming { public function handle( var $event, com.amazon.aws.lambda.Stream $stream, com.amazon.aws.lambda.Context $context ): void }
See also
- What is AWS Lambda?
- AWS Lambda Webservices for the XP Framework
- AWS Core for the XP Framework
- Lambda runtimes
- AWS Lambda Custom Runtime for PHP: A Practical Example
- AWS SDK for PHP
- The Serverless LAMP stack - Community Resources
- Configuring a Lambda function to stream responses
- Implementing response streaming in a custom runtime
xp-forge/lambda 适用场景与选型建议
xp-forge/lambda 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.99k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 08 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「module」 「xp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 xp-forge/lambda 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 xp-forge/lambda 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 xp-forge/lambda 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
bootstrap select field module implementing http://silviomoreto.github.io/bootstrap-select/
Yii2 module to manage website content. Kind of a CMS...
Codeurx Modular is simply a package for Laravel to help you create and manage modules.
User Module for the Attogram Framework
Info Module for the Attogram Framework
Contact Form Module for the Attogram Framework
统计信息
- 总下载量: 9.99k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 11
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2021-08-20

