Upgrading to Bref 4.0
Updating dependencies
Update all Bref packages in your composer.json file from ^3 to ^4, for example:
"require": {
"bref/bref": "^4",
"bref/laravel-bridge": "^4", // if you use Laravel
"bref/symfony-bridge": "^4" // if you use Symfony
}Only update the versions for the packages you actually have in your project.
Then run:
composer update --with-all-dependenciesRuntimes instead of layers
Bref v4 no longer uses AWS Lambda layers: runtime: php-84 (and php-84-fpm, php-84-console) now deploys the function on Bref’s PHP runtime, a customer-managed Lambda runtime . The same runtime works on x86 and ARM.
The PHP runtime no longer counts toward Lambda’s 250MB size limit for code and layers: applications get about 90MB more room.
Deploying them requires osls 4.8 or greater. The Serverless Framework (v3, or v4 by Serverless Inc.) is no longer supported:
npm install -g osls@4PHP 8.2 is no longer supported: the runtimes and the bref/bref package require PHP 8.3 or greater. Use php-83, php-84, php-85 or php-86.
If the credentials used to deploy have restricted permissions, allow the lambda:AssociateRuntimeAlias action on arn:aws:lambda:*:873528684822:runtime/*/alias/4.
The ${bref:layer.php-84} variables and the serverless bref:layers command no longer exist. If you referenced Bref layers directly, use runtime: php-84 (or php-84-fpm, php-84-console) instead: the Bref plugin sets BREF_RUNTIME.
functions:
api:
- runtime: provided.al2023
- layers:
- - ${bref:layer.php-84}
- environment:
- BREF_RUNTIME: fpm
+ runtime: php-84-fpmPHP extensions directory
Bref’s PHP extensions are now installed in /usr/local/lib/php/extensions instead of /opt/bref/extensions.
If you enable a Bref extension with its full path, use its name instead:
-extension=/opt/bref/extensions/intl.so
+extension=intlExtensions that you add yourself in /opt/bref/extensions (via an AWS Lambda layer or in your container image) are no longer found by their name: load them with their full path.
-extension=xhprof.so
+extension=/opt/bref/extensions/xhprof.soGD and GMP are included
The gd and gmp extensions are now installed in Bref runtimes, disabled by default. If you used the extra extensions layers or Docker images for these extensions, remove them and enable the extensions in php/conf.d/php.ini:
runtime: php-84-fpm
- layers:
- - ${bref-extra:gd-php-84}
- - ${bref-extra:gmp-php-84}-COPY --from=bref/extra-gd-php-84:3 /opt /opt
-COPY --from=bref/extra-gmp-php-84:3 /opt /optextension=gd
extension=gmpLocation of the SSL certificates
The SSL certificates (CA bundle) are now in /etc/ssl/certs/ca-certificates.crt, their usual location on Ubuntu. The Bref v3 locations no longer exist:
/opt/bref/ssl/cert.pem/etc/pki/tls/cert.pem
If your application references one of them, for example in MYSQL_ATTR_SSL_CA to connect to a database over SSL, replace it with /etc/ssl/certs/ca-certificates.crt. In PHP code, openssl_get_cert_locations()['default_cert_file'] returns a path that works in every Bref version.
Multi-architecture container images
If you don’t deploy with container images, you can skip this section.
Bref container images are now multi-architecture: the same image works on x86 and ARM. The ARM images (with the arm- prefix) no longer exist:
- FROM bref/arm-php-84:3
+ FROM bref/php-84:4The same applies to bref/arm-php-84-dev (now bref/php-84-dev) and bref/arm-build-php-84 (now bref/build-php-84).
Docker builds the architecture of the machine it runs on, unless a platform is set. Lambda functions only accept single-architecture images, so set the platform that matches the functions’ architecture, especially when building on an ARM machine (e.g. an Apple Silicon Mac) for x86 functions:
provider:
ecr:
images:
my-app:
path: ./
file: Dockerfile
platform: linux/amd64 # or linux/arm64 for functions with `architecture: arm64`If you build with docker build, pass --platform linux/amd64 (or linux/arm64).
Failed console commands are no longer retried
When a command invoked asynchronously failed (e.g. a cron), Lambda retried it twice by default. Bref now sets maximumRetryAttempts: 0 on console functions: Lambda no longer retries failed commands. Failed commands are still counted in the Errors metric of the function.
To keep the retries, set maximumRetryAttempts on the function:
functions:
artisan:
handler: artisan
runtime: php-84-console
maximumRetryAttempts: 2If you deploy without serverless.yml (e.g. with AWS CDK or Terraform), set the maximum retry attempts of the function’s asynchronous invocations to 0 to get the same behavior.
HTTP headers with an underscore are dropped
Headers with an underscore in their name (e.g. api_key) are no longer passed to PHP, on every runtime. Like api-key, they would become the HTTP_API_KEY variable: a client could replace a header set by a proxy. Nginx and Apache drop them by default.
If your application reads such a header, rename it with a dash instead (e.g. api-key), in the application and in its clients.
Binary responses without BREF_BINARY_RESPONSES
Bref now encodes responses that are not text (images, PDF…) in base64 by itself: BREF_BINARY_RESPONSES is no longer needed. Remove it: it also encodes text responses (HTML, JSON…), which limits them to ~4.5 MB instead of 6 MB.
API Gateway v1 REST APIs (http events) still need binaryMediaTypes.
Work after the response with fastcgi_finish_request()
Bref layers no longer disable fastcgi_finish_request(): the client gets the response when it is called, and the rest of the script runs afterwards. Laravel and Symfony call it, then run their “terminate” work (e.g. Laravel’s defer(), Symfony’s kernel.terminate listeners) after the response.
There is nothing to change, but that work still counts toward the function’s timeout and is billed, and the Lambda instance does not handle other requests until it finishes. Read Work after the response.
Output buffering with PHP-FPM
PHP-FPM now buffers up to 4 KB of output (output_buffering=4096), like the php.ini-production of a usual PHP-FPM server.
To stream a response from plain PHP code, call ob_flush() before flush(), else chunks under 4 KB are only sent at the end. To keep the previous behavior, set output_buffering=0 in php/conf.d/.
Custom HTTP handlers and listeners
These changes only affect code that uses Bref’s PHP classes directly:
handleRequest()in the classes that extendBref\Event\Http\HttpHandlerno longer returns aBref\Event\Http\HttpResponse. It receives aBref\Event\Http\HttpResponseWriterand writes the response. PSR-15 handlers are not affected.afterInvoke()inBref\Listener\BrefEventSubscriberreceivesnullas$resultfor HTTP events: HTTP handlers write their response, they do not return it.
- public function handleRequest(HttpRequestEvent $event, Context $context): HttpResponse
+ public function handleRequest(HttpRequestEvent $event, Context $context, HttpResponseWriter $responseWriter): void
{
- return new HttpResponse('Hello world!', ['Content-Type' => 'text/plain']);
+ $responseWriter->respond('Hello world!', ['Content-Type' => 'text/plain']);
}