aws-lambda-php-integration by giuseppe-trisciuoglio/developer-kit
npx skills add https://github.com/giuseppe-trisciuoglio/developer-kit --skill aws-lambda-php-integration使用 Bref 框架在 AWS Lambda 上部署 PHP 和 Symfony 应用程序的模式。
此技能为 AWS Lambda PHP 开发提供了完整的模式,包含两种主要方法:
两种方法都支持与 API Gateway 的集成,并提供生产就绪的配置。
在以下情况下使用此技能:
| 方法 | 冷启动 | 最适合 | 复杂度 |
|---|---|---|---|
| Bref | < 2s | Symfony 应用、功能齐全的 API | 中等 |
| 原始 PHP | < 500ms | 简单的处理器、最大控制权 | 低 |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
my-symfony-lambda/
├── composer.json
├── serverless.yml
├── public/
│ └── index.php # Lambda 入口点
├── src/
│ └── Kernel.php # Symfony 内核
├── config/
│ ├── bundles.php
│ ├── routes.yaml
│ └── services.yaml
└── templates/
my-lambda-function/
├── public/
│ └── index.php # 处理器入口点
├── composer.json
├── serverless.yml
└── src/
└── Services/
使用 Bref 的 Symfony:
// public/index.php
use Bref\Symfony\Bref;
use App\Kernel;
use Symfony\Component\HttpFoundation\Request;
require __DIR__.'/../vendor/autoload.php';
$kernel = new Kernel($_SERVER['APP_ENV'] ?? 'dev', $_SERVER['APP_DEBUG'] ?? true);
$kernel->boot();
$bref = new Bref($kernel);
return $bref->run($event, $context);
原始 PHP 处理器:
// public/index.php
use function Bref\Lambda\main;
main(function ($event) {
$path = $event['path'] ?? '/';
$method = $event['httpMethod'] ?? 'GET';
return [
'statusCode' => 200,
'body' => json_encode(['message' => 'Hello from PHP Lambda!'])
];
});
PHP 的冷启动取决于框架的初始化。关键策略:
// 在函数级别缓存 AWS 客户端
use Aws\DynamoDb\DynamoDbClient;
class DatabaseService
{
private static ?DynamoDbClient $client = null;
public static function getClient(): DynamoDbClient
{
if (self::$client === null) {
self::$client = new DynamoDbClient([
'region' => getenv('AWS_REGION'),
'version' => 'latest'
]);
}
return self::$client;
}
}
// config/services.yaml
parameters:
env(DATABASE_URL): null
env(APP_ENV): 'dev'
services:
App\Service\Configuration:
arguments:
$tableName: '%env(DATABASE_URL)%'
保持 composer.json 最小化:
{
"require": {
"php": "^8.2",
"bref/bref": "^2.0",
"symfony/framework-bundle": "^6.0"
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist"
}
}
返回正确的 Lambda 响应:
try {
$result = processRequest($event);
return [
'statusCode' => 200,
'body' => json_encode($result)
];
} catch (ValidationException $e) {
return [
'statusCode' => 400,
'body' => json_encode(['error' => $e->getMessage()])
];
} catch (Exception $e) {
error_log($e->getMessage());
return [
'statusCode' => 500,
'body' => json_encode(['error' => 'Internal error'])
];
}
使用结构化日志:
error_log(json_encode([
'level' => 'info',
'message' => 'Request processed',
'request_id' => $context->getAwsRequestId(),
'path' => $event['path'] ?? '/'
]));
Serverless Framework:
# serverless.yml
service: symfony-lambda-api
provider:
name: aws
runtime: php-82
memorySize: 512
timeout: 20
package:
individually: true
exclude:
- '**/node_modules/**'
- '**/.git/**'
functions:
api:
handler: public/index.php
events:
- http:
path: /{proxy+}
method: ANY
- http:
path: /
method: ANY
使用 Bref 部署:
composer require bref/bref --dev
vendor/bin/bref deploy
# serverless.yml for Symfony
service: symfony-lambda-api
provider:
name: aws
runtime: php-82
stage: ${self:custom.stage}
region: ${self:custom.region}
environment:
APP_ENV: ${self:custom.stage}
APP_DEBUG: ${self:custom.isLocal}
iam:
role:
statements:
- Effect: Allow
Action:
- dynamodb:GetItem
- dynamodb:PutItem
Resource: '*'
functions:
web:
handler: public/index.php
timeout: 30
memorySize: 1024
events:
- http:
path: /{proxy+}
method: ANY
console:
handler: bin/console
timeout: 300
events:
- schedule: rate(1 day)
plugins:
- ./vendor/bref/bref
custom:
stage: dev
region: us-east-1
isLocal: false
有关特定主题的详细指导:
输入:
Create a Symfony Lambda REST API using Bref for a todo application
过程:
composer create-project 初始化 Symfony 项目composer require bref/brefvendor/bin/bref deploy 配置部署输出:
输入:
My Symfony Lambda has 5 second cold start, how do I optimize it?
过程:
输出:
输入:
Configure CI/CD for Symfony Lambda with Serverless Framework
过程:
输出:
版本:1.0.0
每周安装数
187
代码仓库
GitHub 星标数
173
首次出现
2026年2月20日
安全审计
安装于
codex166
gemini-cli164
github-copilot161
opencode159
cursor159
kimi-cli158
Patterns for deploying PHP and Symfony applications on AWS Lambda using the Bref framework.
This skill provides complete patterns for AWS Lambda PHP development with two main approaches:
Both approaches support API Gateway integration with production-ready configurations.
Use this skill when:
| Approach | Cold Start | Best For | Complexity |
|---|---|---|---|
| Bref | < 2s | Symfony apps, full-featured APIs | Medium |
| Raw PHP | < 500ms | Simple handlers, maximum control | Low |
my-symfony-lambda/
├── composer.json
├── serverless.yml
├── public/
│ └── index.php # Lambda entry point
├── src/
│ └── Kernel.php # Symfony Kernel
├── config/
│ ├── bundles.php
│ ├── routes.yaml
│ └── services.yaml
└── templates/
my-lambda-function/
├── public/
│ └── index.php # Handler entry point
├── composer.json
├── serverless.yml
└── src/
└── Services/
Symfony with Bref:
// public/index.php
use Bref\Symfony\Bref;
use App\Kernel;
use Symfony\Component\HttpFoundation\Request;
require __DIR__.'/../vendor/autoload.php';
$kernel = new Kernel($_SERVER['APP_ENV'] ?? 'dev', $_SERVER['APP_DEBUG'] ?? true);
$kernel->boot();
$bref = new Bref($kernel);
return $bref->run($event, $context);
Raw PHP Handler:
// public/index.php
use function Bref\Lambda\main;
main(function ($event) {
$path = $event['path'] ?? '/';
$method = $event['httpMethod'] ?? 'GET';
return [
'statusCode' => 200,
'body' => json_encode(['message' => 'Hello from PHP Lambda!'])
];
});
PHP cold start depends on framework initialization. Key strategies:
// Cache AWS clients at function level
use Aws\DynamoDb\DynamoDbClient;
class DatabaseService
{
private static ?DynamoDbClient $client = null;
public static function getClient(): DynamoDbClient
{
if (self::$client === null) {
self::$client = new DynamoDbClient([
'region' => getenv('AWS_REGION'),
'version' => 'latest'
]);
}
return self::$client;
}
}
// config/services.yaml
parameters:
env(DATABASE_URL): null
env(APP_ENV): 'dev'
services:
App\Service\Configuration:
arguments:
$tableName: '%env(DATABASE_URL)%'
Keep composer.json minimal:
{
"require": {
"php": "^8.2",
"bref/bref": "^2.0",
"symfony/framework-bundle": "^6.0"
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist"
}
}
Return proper Lambda responses:
try {
$result = processRequest($event);
return [
'statusCode' => 200,
'body' => json_encode($result)
];
} catch (ValidationException $e) {
return [
'statusCode' => 400,
'body' => json_encode(['error' => $e->getMessage()])
];
} catch (Exception $e) {
error_log($e->getMessage());
return [
'statusCode' => 500,
'body' => json_encode(['error' => 'Internal error'])
];
}
Use structured logging:
error_log(json_encode([
'level' => 'info',
'message' => 'Request processed',
'request_id' => $context->getAwsRequestId(),
'path' => $event['path'] ?? '/'
]));
Serverless Framework:
# serverless.yml
service: symfony-lambda-api
provider:
name: aws
runtime: php-82
memorySize: 512
timeout: 20
package:
individually: true
exclude:
- '**/node_modules/**'
- '**/.git/**'
functions:
api:
handler: public/index.php
events:
- http:
path: /{proxy+}
method: ANY
- http:
path: /
method: ANY
Deploy with Bref:
composer require bref/bref --dev
vendor/bin/bref deploy
# serverless.yml for Symfony
service: symfony-lambda-api
provider:
name: aws
runtime: php-82
stage: ${self:custom.stage}
region: ${self:custom.region}
environment:
APP_ENV: ${self:custom.stage}
APP_DEBUG: ${self:custom.isLocal}
iam:
role:
statements:
- Effect: Allow
Action:
- dynamodb:GetItem
- dynamodb:PutItem
Resource: '*'
functions:
web:
handler: public/index.php
timeout: 30
memorySize: 1024
events:
- http:
path: /{proxy+}
method: ANY
console:
handler: bin/console
timeout: 300
events:
- schedule: rate(1 day)
plugins:
- ./vendor/bref/bref
custom:
stage: dev
region: us-east-1
isLocal: false
For detailed guidance on specific topics:
Input:
Create a Symfony Lambda REST API using Bref for a todo application
Process:
composer create-projectcomposer require bref/brefvendor/bin/bref deployOutput:
Input:
My Symfony Lambda has 5 second cold start, how do I optimize it?
Process:
Output:
Input:
Configure CI/CD for Symfony Lambda with Serverless Framework
Process:
Output:
Version: 1.0.0
Weekly Installs
187
Repository
GitHub Stars
173
First Seen
Feb 20, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex166
gemini-cli164
github-copilot161
opencode159
cursor159
kimi-cli158
Supabase Postgres 最佳实践指南 - 8大类别性能优化规则与SQL示例
78,800 周安装
dev-session:AI开发会话管理工具,支持进度跟踪、检查点提交和持久化学习记录
353 周安装
Cloudflare Workers KV 使用指南 - 高性能键值存储 API 与最佳实践
355 周安装
Claude Code 开发者工具箱:7个AI智能体提升代码提交、调试、测试与文档效率
355 周安装
AutoAnimate 错误预防指南:解决 SSR、Flexbox、React/Vue 动画问题的完整方案
355 周安装
黄金标准 README 生成工具 - 提升开源项目转化率的专业文档生成器
364 周安装
Cloudflare Workers边缘环境OAuth集成指南:GitHub与Microsoft身份验证
356 周安装