Skip to Content
DocumentationUse casesHTTP applicationsAdvanced HTTP use cases

Advanced HTTP use-cases

If you are getting started with Bref, read the guides for Laravel (Get started), Symfony (Get started), or other PHP frameworks (Get started).

This documentation is for advanced use-cases.

Alternative AWS architectures

By default, Bref uses API Gateway v2 HTTP APIs to run HTTP applications.

However, there are other ways to run HTTP applications on AWS Lambda: using API Gateway , Lambda Function URLs , or AWS Application Load Balancer (ALB) :

  • API Gateway v1 “REST” + Lambda
  • API Gateway v2 “HTTP” + Lambda
  • Lambda Function URL
  • AWS ALB (Application Load Balancer) + Lambda

Bref supports all 4, but API Gateway v2 “HTTP” is the default because it is the simplest option that supports both HTTP APIs and websites with custom domains.

If you are getting started, stay with the defaults. The documentation and integrations will be much simpler to follow.

You can change the architecture later if you need to.

Here is a summary of the differences between the 4 options:

API Gateway v1 RESTAPI Gateway v2 HTTPFunction URLALB
Pricing (Lambda cost excluded)$3.5/million requests$1/million requestsFree (no extra costs)Starts at $22/mo
Custom domain✅✅No, but possible with CloudFront✅
HTTP/HTTPS Support✅HTTPS only (use CloudFront for HTTP)HTTPS only (use CloudFront for HTTP)✅
AuthorizersIAM, Lambda, CognitoIAM, Lambda, Cognito, OAuth 2IAMOIDC, Cognito
CORS✅✅✅❌
CloudWatch metrics✅✅✅✅
CloudWatch access logs✅✅❌✅
Caching✅❌❌❌
API key management✅❌❌❌
Request transformation✅❌❌❌
Request/response validation✅❌❌❌
Maximum request/response size6MB6MB6MB1MB
Maximum HTTP response timeout29s30s30 minutes15 minutes
Added latency to HTTP responses25ms15ms10ms

You can read more details in the “Choosing between REST APIs and HTTP APIs” AWS documentation .

API Gateway v2 HTTP API

The simplest way to set up API Gateway is to have all incoming requests sent to our application in one Lambda function:

serverless.yml
functions: hello: handler: index.php # ... events: - httpApi: '*'

That works well with frameworks like Symfony or Laravel that have a single entrypoint (e.g. public/index.php) combined with the PHP-FPM runtime.

You can look at more advanced API Gateway routing options in the serverless documentation .

Lambda Function URLs

AWS Lambda can respond to HTTP requests via Lambda Function URLs , without using API Gateway.

You can deploy a Lambda Function URL via the following configuration :

serverless.yml
functions: hello: handler: index.php # ... url: invokeMode: RESPONSE_STREAM

With this configuration, responses are streamed to the client as they are produced, and they are not limited to 6 MB.

If you create the Function URL with other tools (AWS CDK, Terraform, the AWS console…), set the BREF_RESPONSE_STREAMING environment variable to 1 on the function along with the RESPONSE_STREAM invoke mode (or the STREAM transfer mode of a REST API). With only one of them, pages are empty or show raw JSON.

url: true (without streaming) works too, with the 6 MB limit.

Server-sent events

Streamed responses can send server-sent events  (for example with Laravel’s response()->eventStream()), to stream the answer of an LLM, the progress of a long task, etc. Keep in mind that:

  • A response lasts at most until the Lambda timeout (30 minutes maximum): set the function’s timeout accordingly. With PHP-FPM, Bref ends the response shortly before the timeout. With Laravel Octane or the Symfony kernel, stop the stream in your code before the timeout: else the response is cut at the timeout and the Lambda instance restarts. Browsers reconnect automatically and send the Last-Event-ID header, so that the application can resume where it stopped.
  • Each open connection keeps one Lambda instance busy, and billed, for its whole duration. For connections that stay open for hours (notifications, chats…), use WebSockets instead.
  • Behind CloudFront, send an event or a comment (e.g. : ping) at least every 30 seconds: CloudFront closes responses that stay silent longer than its origin response timeout (30 seconds by default).

API Gateway v1 REST API

The syntax is slightly different from API Gateway v2 HTTP APIs as we must use a different events configuration. Here is an example that sends all requests to a single Lambda function:

serverless.yml
functions: hello: handler: index.php # ... events: - http: 'ANY /' - http: 'ANY /{proxy+}'

Learn more in the Serverless Framework documentation .

Application Load Balancer

Application Load Balancer (ALB) is a managed load balancer that can be used to route HTTP requests to Lambda functions. It is a more advanced option that is interesting at high scale as ALB can be much cheaper than API Gateway.

serverless.yml
functions: hello: handler: index.php # ... events: - alb: listenerArn: arn:aws:elasticloadbalancing:us-east-1:12345:listener/app/my-load-balancer/50dc6c495c0c9188/ priority: 1 conditions: path: '/*'

Learn more in the Serverless Framework documentation .

PHP handlers

This section applies to all 4 approaches: API Gateway v1, v2, ALB, and Function URLs.

Bref abstracts the differences so that the same code can be used with all 4 solutions.

There are two ways to handle HTTP events with PHP:

Here is a full comparison between both approaches:

PHP-FPM runtimeEvent-Driven Function handler
What are the use cases?To build websites, APIs, etc. This should be the default approach as it’s compatible with mature PHP frameworks and tools.Build event-driven microservices, or run Laravel Octane, or Symfony with a keep-alive process (like Roadrunner/Swoole).
Why does that solution exist?For out-of-the-box compatibility with existing applications and frameworks.To match how other languages run in AWS Lambda, i.e. to build very specialized HTTP endpoints.
How it runs under the hoodUsing PHP-FPM.Using the PHP CLI.
What does the routing (i.e. separate pages)?Your PHP framework (one Lambda receives all the URLs).API Gateway: we define one Lambda and one handler class per route.
How to read the request?$_GET, $_POST, etc.The $request parameter (PSR-7 request).
How to write a response?echo, header() function, etc.Returning a PSR-7 response from the handler class.
How does it work?Bref turns an API Gateway event into a FastCGI (PHP-FPM) request.Bref turns an API Gateway event into a PSR-7 request.
Is each request handled in a separate PHP process?Yes (that’s how PHP-FPM works).Yes by default (Bref replicates that to avoid surprises) but can be disabled for optimal performance.

With the PHP-FPM runtime

This is perfect for most use-cases: PHP works like on any server with PHP-FPM. HTTP routing based on the URL is done by the application/the framework.

This approach is already covered by most of the Bref documentation, so we won’t go into details here.

You can read more about the PHP-FPM runtime here.

With the Event-Driven Function runtime

This is more advanced, as PHP does not run in a traditional PHP-FPM environment. It can be used with or without a PHP framework.

When used with a framework, understand that the whole HTTP stack (like HTTP middlewares) of the framework does not run. You are responsible for invoking the PHP code that should run.

Note: this approach is used to run Laravel Octane or Symfony with a keep-alive process (like Roadrunner/Swoole). These use cases are not detailed here, read about Laravel Octane or about Symfony “Keep-Alive” instead.

The handler must be a PHP function, or a PSR-15 implementation. Indeed, Bref natively supports the PSR-15  and PSR-7  standards. Here is an example:

<?php namespace App; use Nyholm\Psr7\Response; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; class MyHttpHandler implements RequestHandlerInterface { public function handle(ServerRequestInterface $request): ResponseInterface { $name = $request->getQueryParams()['name'] ?? 'world'; return new Response(200, [], "Hello $name"); } }

Then, create a Lambda function that listens to HTTP events with the handler you created:

serverless.yml
functions: # ... hello: handler: App\MyHttpHandler runtime: php-84 # Lambda Function URL url: true # Or API Gateway events: # API Gateway v2 - httpApi: 'GET /hello' # API Gateway v1 - http: 'GET hello'

The App\MyHttpHandler class will be instantiated by Laravel’s service container.

Since a handler is a controller for a specific route, we can use the API Gateway routing to deploy multiple functions:

serverless.yml
functions: create-article: handler: App\CreateArticleController runtime: php-84 events: - httpApi: 'POST /articles' get-article: handler: App\GetArticleController runtime: php-84 events: - httpApi: 'GET /articles/{id}'

Path parameters (e.g. {id} in the example above) are available as request attributes in the PSR-7 request:

$id = $request->getAttribute('id');

Full reference of HTTP events in serverless.yml.

Lambda event and context

The API Gateway event and Lambda context are available as attributes on the PSR-7 request:

/** @var $event Bref\Event\Http\HttpRequestEvent */ $event = $request->getAttribute('lambda-event'); /** @var $context Bref\Context\Context */ $context = $request->getAttribute('lambda-context');

If you’re looking for the request context array, for example when using a Lambda authorizer :

$requestContext = $request->getAttribute('lambda-event')->getRequestContext();

Cold starts

On applications with regular traffic, cold starts only represent about 0.2% of requests. For low-traffic applications, you can pre-warm your HTTP function to avoid them.

Read the full Cold starts documentation for details and the warming configuration.

Last updated on