Bref Documentation

repository·master·Indexed 25 days ago

https://github.com/brefphp/bref

Bref 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.

Tokens
63.7K
Snippets
203
Records
364
Agent score
85%

What's inside Bref

  1. Overview of Bref Cloud

    master
    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.
  2. Overview of Bref and Serverless PHP

    master

    Bref 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 serverless CLI for configuration and deployment.

  3. Overview of X-Ray for PHP on AWS Lambda

    master
    X-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.
  4. Monitor applications with Bref Cloud

    master

    Bref 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/support or Slack.

  5. Understand Laravel caching behavior in Bref

    master

    By default, the Bref bridge moves Laravel's storage and cache directories to /tmp because the rest of the filesystem is read-only in AWS Lambda.

    Important Limitation: The /tmp directory 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.

  6. Sentry integration for PHP on AWS Lambda

    master

    Bref 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.
  7. Avoid vendor lock-in with Bref abstractions

    master

    Bref 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.
  8. Automatic tracing features

    master

    The 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 named bref.startup for 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 named bref.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.

  9. Understand the AWS Lambda filesystem

    master

    The AWS Lambda filesystem is read-only, except for the /tmp directory. Files stored in /tmp are 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.
  10. Use the Bref Dashboard for logs and metrics

    master
    The 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.