Loading...
Loading...
Provides AWS Lambda integration patterns for PHP with Symfony using the Bref framework. Use when deploying PHP/Symfony applications to AWS Lambda, optimizing cold starts, configuring API Gateway integration, or implementing serverless PHP applications with Bref. Triggers include "create lambda php", "deploy symfony lambda", "bref lambda aws", "php lambda cold start", "aws lambda php performance", "symfony serverless", "php serverless framework".
npx skill4agent add giuseppe-trisciuoglio/developer-kit-claude-code aws-lambda-php-integration| 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/// 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);// 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!'])
];
});// 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)%'composer.json{
"require": {
"php": "^8.2",
"bref/bref": "^2.0",
"symfony/framework-bundle": "^6.0"
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist"
}
}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.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: ANYcomposer 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: falseCreate a Symfony Lambda REST API using Bref for a todo applicationcomposer create-projectcomposer require bref/brefvendor/bin/bref deployMy Symfony Lambda has 5 second cold start, how do I optimize it?Configure CI/CD for Symfony Lambda with Serverless Framework