Bref Documentation
repository·master·Indexed 25 days ago
https://github.com/brefphp/brefBref is a toolset for running PHP applications on serverless infrastructure, specifically AWS Lambda. It provides runtimes including a PHP-FPM runtime for migrating legacy monoliths and a console runtime for CLI commands. The ecosystem includes the Bref CLI and Bref Cloud for simplified deployment, AWS credential management via IAM roles, and integration with GitHub Actions.
What's inside Bref
- Bref Cloud is a managed hosting service that allows you to deploy, monitor, and run PHP applications on AWS Lambda. It simplifies AWS infrastructure by providing an intuitive dashboard and automated deployment, removing the need for deep DevOps or AWS expertise while maintaining the scalability and reliability of serverless architecture.
Overview of Bref and Serverless PHP
masterBref is an open-source Composer package that enables you to deploy PHP applications to AWS and run them on AWS Lambda. It provides PHP runtimes for AWS Lambda, deployment tooling, and deep integrations for frameworks like Laravel and Symfony.
Key benefits of using Bref and serverless include:
- No server maintenance: AWS manages the underlying infrastructure.
- Pay-per-use: You are only charged while your code is actually executing.
- Automatic scaling: The application scales horizontally to meet traffic demands.
Bref uses the
serverlessCLI for configuration and deployment.Overview of Bref
masterBref provides tools and documentation to easily deploy and run serverless PHP applications. It simplifies the process of running PHP on serverless platforms like AWS Lambda.Overview of X-Ray for PHP on AWS Lambda
masterX-Ray is an integration for PHP applications running on AWS Lambda that allows you to trace application performance, errors, and distributed tracing. It provides visibility into how your application interacts with various services and components.Monitor applications with Bref Cloud
masterBref Cloud provides a unified monitoring interface for serverless PHP applications, including:
- Logs: A simplified UI to view and search CloudWatch logs.
- Metrics: Monitoring of Lambda invocations, duration, errors, and more.
- Traces: Visualization of X-Ray traces to analyze performance, including database queries, HTTP calls, and AWS SDK calls.
Bref Cloud users receive a free Bref X-Ray license to enable performance tracing. To activate it, contact support via
bref.cloud/supportor Slack.Understand Laravel caching behavior in Bref
masterBy default, the Bref bridge moves Laravel's
storageandcachedirectories to/tmpbecause the rest of the filesystem is read-only in AWS Lambda.Important Limitation: The
/tmpdirectory is local to each Lambda instance and is not shared. If your application scales up or redeploys, new instances will have an empty cache. For shared state (e.g., data caching, API rate limiting, or locking mechanisms), you must use a centralized store like a database, Redis, or DynamoDB.Sentry integration for PHP on AWS Lambda
masterBref provides an advanced Sentry integration for PHP applications running on AWS Lambda. This integration allows you to track issues that occur outside of the standard PHP-FPM lifecycle and provides deep visibility into serverless-specific metrics.
Key capabilities include:
- Advanced error tracking: Capture AWS Lambda errors that occur outside of PHP-FPM, such as timeouts or "response too big" errors.
- Non-HTTP exception tracking: Monitor exceptions in event-driven handlers like SQS, EventBridge, S3, and more.
- Cold start tracking: Monitor cold starts and Bref startup duration within Sentry performance tracing.
- Lambda handler performance: Trace the performance of event-driven PHP Lambda handlers.
- AWS SDK tracking: Measure AWS SDK calls within performance tracing to identify bottlenecks.
Avoid vendor lock-in with Bref abstractions
masterBref is designed to minimize vendor lock-in by providing abstractions that allow PHP code to run similarly to a traditional server environment.
Key features that enable this:
- FPM Runtime: The default runtime runs PHP-FPM exactly like a classic server.
- Console Runtime: Console commands run as usual through the dedicated Console runtime.
- Framework Integrations: Bref provides preconfigured integrations for common needs like file storage and queues (e.g., SQS + Lambda runtime integrations for Symfony Messenger and Laravel Queues).
- Logging: Bref preconfigures logs to be sent to AWS CloudWatch automatically.
Automatic tracing features
masterThe Bref Sentry integration provides several automatic tracing capabilities:
Event-driven PHP runtime
When using the event-driven function runtime, event handlers (e.g., EventBridge, SQS, S3, SNS) are automatically traced. Exceptions are captured and reported, and event handlers appear in performance tracing with advanced metadata and tags.
Cold starts tracing
Cold starts are automatically traced in Laravel and Symfony. They appear in Sentry performance tracing as transactions named
lambda.cold-start, including a sub-span namedbref.startupfor Bref initialization.Bref process restarts
When using the event-driven runtime, Bref may restart the PHP process between invocations (if not using
BREF_LOOP_MAX). These restarts are traced as a separate sub-transaction namedbref.startup. While not part of the main HTTP response time, they are part of the same distributed trace and can be viewed together with the main transaction.Understand the AWS Lambda filesystem
masterThe AWS Lambda filesystem is read-only, except for the
/tmpdirectory. Files stored in/tmpare not shared between Lambda instances and can be lost when a Lambda scales down or stops.Key directories:
/opt/: Where Lambda runtimes and layers (like Bref) are unzipped./var/task/: Where your application code is unzipped. This directory is limited to 250MB. If you exceed this, deploy via Docker images instead./tmp/: The only writable directory, used for temporary files.
Monitor with Tideways
masterTideways is a PHP-specific monitoring and profiling tool compatible with Bref. To use it, you must set up a Tideways daemon on an EC2 instance within a VPC.Use the Bref Dashboard for logs and metrics
masterThe Bref Dashboard is a lightweight alternative for projects not using Bref Cloud. It fetches data directly from AWS CloudWatch to provide a simple UI for logs and metrics. It requires no additional AWS setup and is available for immediate use.