### Automate Deployments with CI/CD Source: https://github.com/brefphp/bref/blob/master/docs/deploy.mdx Example command for automating deployment in a CI/CD pipeline, including installing Composer dependencies optimized for production. ```bash # Install Composer dependencies optimized for production composer install --prefer-dist --optimize-autoloader --no-dev ``` -------------------------------- ### Install X-Ray Integration Package Source: https://github.com/brefphp/bref/blob/master/website/src/pages/xray/docs.mdx Install the bref/xray-integration package using Composer. ```bash composer require bref/xray-integration -W ``` -------------------------------- ### Install Sentry Integration Package Source: https://github.com/brefphp/bref/blob/master/website/src/pages/sentry/docs.mdx Install the bref/sentry-integration package using Composer. ```bash composer require bref/sentry-integration ``` -------------------------------- ### Pre-generate Config Cache (Advanced) Source: https://github.com/brefphp/bref/blob/master/docs/laravel/getting-started.mdx Optionally pre-generate the config cache for potentially faster cold starts. This requires careful environment setup to avoid hardcoded paths. ```bash php artisan config:clear && php artisan config:cache ``` -------------------------------- ### Direct Database Connection Example (PHP/PDO) Source: https://github.com/brefphp/bref/blob/master/docs/environment/database.mdx Example of a direct database connection string for PHP applications using PDO. Ensure the endpoint and credentials are correct. ```text mysql://user:password@dbname.e2sctvp0nqos.us-east-1.rds.amazonaws.com/dbname ``` -------------------------------- ### Install Serverless CLI Source: https://github.com/brefphp/bref/blob/master/docs/setup.mdx Install the OSS Serverless CLI globally using npm. This tool is required for deploying applications with Bref. ```bash npm install -g osls ``` -------------------------------- ### Simple index.php for testing Source: https://github.com/brefphp/bref/blob/master/docs/default/getting-started.mdx Create a basic `index.php` file to test your deployment. This example simply outputs 'Hello world!'. ```php .env.local.php serverless deploy ``` ```bash # Install dependencies composer install --classmap-authoritative --no-dev --no-scripts # Warmup the cache bin/console cache:clear --env=prod # Disable use of Dotenv component echo " .env.local.php bref deploy ``` -------------------------------- ### Install Tideways PHP Extension Source: https://github.com/brefphp/bref/blob/master/docs/monitoring/tideways.md Install the Tideways PHP extension using Composer. This package provides the necessary extension for Bref. ```bash composer require bref/extra-php-extensions ``` -------------------------------- ### Install Composer Dependencies for Production Source: https://github.com/brefphp/bref/blob/master/docs/deploy.mdx Optimize Composer dependencies for production by preferring stable distributions, optimizing the autoloader, and excluding development dependencies. ```bash composer install --prefer-dist --optimize-autoloader --no-dev ``` -------------------------------- ### Run Docker Compose for Bref application Source: https://github.com/brefphp/bref/blob/master/docs/local-development.mdx Start the Bref application locally using Docker Compose. The application will be accessible via localhost. ```bash docker-compose up ``` -------------------------------- ### Define S3 Bucket and Policy with CloudFormation Functions Source: https://github.com/brefphp/bref/blob/master/docs/environment/serverless-yml.mdx This example demonstrates defining an S3 bucket and an IAM policy that references it using CloudFormation functions `!Ref` and `!Sub`. The `!Ref MyBucket` syntax is used to reference the bucket ARN, and `!Sub '${MyBucket.Arn}/*'` is used to construct a resource path. ```yaml #... resources: Resources: MyBucket: Type: AWS::S3::Bucket # IAM policy that makes the bucket publicly readable MyBucketPolicy: Type: AWS::S3::BucketPolicy Properties: Bucket: !Ref MyBucket PolicyDocument: Statement: - Effect: Allow Principal: '*' # everyone Action: s3:GetObject Resource: !Sub '${MyBucket.Arn}/*' ``` -------------------------------- ### PHP Handler Class with Composer Autoloading Source: https://github.com/brefphp/bref/blob/master/docs/runtimes/function.mdx Example demonstrating how to structure a PHP Lambda handler class in a separate file (e.g., `src/`) and rely on Composer for autoloading. The `serverless.yml` must point to the file that returns the handler instance. ```php bootEnv(dirname(__DIR__).'/.env'); $kernel = new \App\Kernel($_SERVER['APP_ENV'], (bool)$_SERVER['APP_DEBUG']); $kernel->boot(); // Return the Bref consumer service return $kernel->getContainer()->get(SqsConsumer::class); ``` -------------------------------- ### Basic PHP Runtime Configuration Source: https://github.com/brefphp/bref/blob/master/docs/runtimes.mdx This is the simplified configuration using the `runtime` property. Bref automatically translates this to the correct `provided.al2023` runtime and the appropriate Lambda layer. ```yaml functions: hello: # ... runtime: php-84 ``` -------------------------------- ### Example Layer ARN Source: https://github.com/brefphp/bref/blob/master/docs/runtimes/runtimes-details.mdx This is an example of an AWS Lambda layer ARN for Bref. Replace placeholders with your specific region, layer name, and version. ```text arn:aws:lambda:us-east-1:873528684822:layer:php-84:21 ``` -------------------------------- ### AWS Lambda Filesystem Overview Source: https://github.com/brefphp/bref/blob/master/docs/environment/storage.mdx Provides a high-level view of the available directories and their purposes within the AWS Lambda environment. ```bash /opt/ # Where Lambda runtimes and layers (like Bref) are unzipped /var/task/ # Where your application code is unzipped /tmp/ # Temporary files ... ``` -------------------------------- ### Bref v2 `serverless.yml` Simplified Runtime Configuration Source: https://github.com/brefphp/bref/blob/master/website/src/pages/news/02-bref-2.0.mdx Demonstrates the simplified Bref v2 syntax for configuring PHP runtimes directly in `serverless.yml`. This new syntax eliminates the need for `runtime: provided.al2` and explicit layer definitions. ```yaml provider: name: aws functions: api: handler: public/index.php # ... runtime: php-84-fpm ``` -------------------------------- ### Run Composer Update Source: https://github.com/brefphp/bref/blob/master/docs/upgrading/v3.mdx After updating dependencies in composer.json, run this command to install the new versions. ```bash composer update --with-all-dependencies ``` -------------------------------- ### Serverless Deploy Equivalent Source: https://github.com/brefphp/bref/blob/master/docs/cloud-deploy.mdx This command shows the equivalent `serverless deploy` command for deploying to a specific stage, which is replaced by the `--env` option in `bref deploy`. ```shell serverless deploy --stage production ``` -------------------------------- ### Configure Packagist.com Repository Source: https://github.com/brefphp/bref/blob/master/website/src/pages/sentry/docs.mdx Add the Packagist.com sub-repository URL to your composer.json file to enable installation of private packages. ```json { "repositories": [ {"type": "composer", "url": "https://bref.repo.packagist.com//"} ] } ``` -------------------------------- ### Set idle_in_transaction_session_timeout for PostgreSQL Source: https://github.com/brefphp/bref/blob/master/docs/laravel/octane.mdx For PostgreSQL, configure idle_in_transaction_session_timeout to manage persistent connections. This example sets it to 10 seconds. ```sql ALTER DATABASE SET idle_in_transaction_session_timeout = '10000' -- 10 seconds in ms ``` -------------------------------- ### PHP Cron Function Example Source: https://github.com/brefphp/bref/blob/master/docs/use-cases/cron.mdx Define a cron job function in PHP that returns a closure. This closure will be executed by Bref. ```php { let sponsors; try { sponsors = await getSponsors(); } catch (e) { console.error(e); return { props: { ssg: { sponsors: [], contributors: [], }, }, }; } const contributors = mergeAndSortContributors([ ...(await getContributors('brefphp/bref')), ...(await getContributors('brefphp/aws-lambda-layers')), ...(await getContributors('brefphp/laravel-bridge')), ...(await getContributors('brefphp/symfony-bridge')), ...(await getContributors('brefphp/extra-php-extensions')), ...(await getContributors('brefphp/examples')), ]); // See https://nextra.site/docs/guide/ssg return { props: { ssg: { sponsors, contributors, }, }, // This page will be generated statically on deployment and never regenerated revalidate: false, }; } ``` -------------------------------- ### Deploy Application to Specific Environments Source: https://github.com/brefphp/bref/blob/master/docs/deploy.mdx Deploy your application to different environments (stages) using the Serverless CLI or Bref Cloud. The default environment is 'dev'. ```bash serverless deploy --stage=prod ``` ```bash bref deploy --env=prod ``` ```bash bref deploy -e prod ``` -------------------------------- ### JavaScript Lambda Function Example Source: https://github.com/brefphp/bref/blob/master/docs/runtimes/function.mdx A JavaScript equivalent to the PHP anonymous function, demonstrating the similarity in handler structure for AWS Lambda. ```javascript exports.myHandler = async function (event, context) { return "Hello " + event.name; } ``` -------------------------------- ### Run Laravel Database Migrations with Serverless Framework Source: https://github.com/brefphp/bref/blob/master/docs/environment/database.mdx Execute Laravel database migrations using the `serverless bref:cli` command when using the Serverless Framework. ```bash serverless bref:cli --args="migrate --force" ``` -------------------------------- ### Bref v2 Runtime Configuration Equivalents Source: https://github.com/brefphp/bref/blob/master/website/src/pages/news/02-bref-2.0.mdx Shows the mapping between the new Bref v2 runtime syntax and the older Bref v1 syntax for different runtime types (FPM, function, console). This helps in understanding the simplification introduced in v2. ```yaml # PHP-FPM runtime (web apps) runtime: provided.al2 layers: - ${bref:layer.php-84-fpm} # becomes: runtime: php-84-fpm ``` ```yaml # Function runtime runtime: provided.al2 layers: - ${bref:layer.php-84} # becomes: runtime: php-84 ``` ```yaml # Console runtime runtime: provided.al2 layers: - ${bref:layer.php-84} - ${bref:layer.console} # becomes: runtime: php-84-console ``` -------------------------------- ### Basic serverless.yml Structure Source: https://github.com/brefphp/bref/blob/master/docs/environment/serverless-yml.mdx An overview of a typical serverless.yml file, including service name, provider configuration, Bref plugin, function definition, and AWS resources. ```yaml service: app provider: name: aws plugins: - ./vendor/bref/bref functions: foo: handler: index.php runtime: php-84 resources: Resources: MyBucket: Type: AWS::S3::Bucket Properties: BucketName: 'my-bucket' ``` -------------------------------- ### PHP function example Source: https://github.com/brefphp/bref/blob/master/docs/local-development/event-driven-functions.mdx A basic PHP function that accepts an event array and returns a greeting. This is the handler file invoked by Bref. ```php return function (array $event) { return 'Hello ' . ($event['name'] ?? 'world'); }; ``` -------------------------------- ### Compile Symfony Assets Source: https://github.com/brefphp/bref/blob/master/docs/use-cases/websites.mdx Compile assets for a Symfony application. This includes installing assets and potentially running Webpack Encore for production builds. ```bash php bin/console assets:install --env prod # if using Webpack Encore, additionally run ``` ```bash yarn encore production ``` -------------------------------- ### Run Symfony Database Migrations with Serverless Framework Source: https://github.com/brefphp/bref/blob/master/docs/environment/database.mdx Execute Symfony Doctrine database migrations using the `serverless bref:cli` command when using the Serverless Framework. Use `--no-interaction` to prevent prompts. ```bash serverless bref:cli --args="doctrine:migrations:migrate --no-interaction" ``` -------------------------------- ### Basic Dockerfile for Bref PHP 8.4 Source: https://github.com/brefphp/bref/blob/master/docs/deploy/docker.mdx This Dockerfile sets up a basic environment for deploying a PHP 8.4 application with Bref using FPM. It copies your application code and configures the entrypoint. ```dockerfile FROM bref/php-84:3 # Copy the source code in the image COPY . /var/task # In this example we're serving an HTTP application with php-fpm ENV BREF_RUNTIME=fpm # Configure the handler file (the entrypoint that receives all HTTP requests) CMD ["public/index.php"] ``` -------------------------------- ### serverless.yml for Multiple Runtimes with Single Image (Option 2) Source: https://github.com/brefphp/bref/blob/master/docs/upgrading/v3.mdx Configure different BREF_RUNTIME values (fpm, console, function) for each function type in serverless.yml when using a single Docker image. ```yaml functions: web: image: name: my-app-image environment: BREF_RUNTIME: fpm # Web/HTTP function events: - httpApi: '*' console: image: name: my-app-image environment: BREF_RUNTIME: console # Console commands worker: image: name: my-app-image environment: BREF_RUNTIME: function # Queue worker events: - sqs: arn: !GetAtt MyQueue.Arn ``` -------------------------------- ### PHP Anonymous Function Handler Source: https://github.com/brefphp/bref/blob/master/website/src/pages/news/01-bref-1.0.mdx Example of an event-driven function using an anonymous PHP function as a handler. Suitable for simple, single-purpose functions. ```php return function ($event) { return 'Hello ' . $event['name']; }; ``` -------------------------------- ### Specify the Handler File Source: https://github.com/brefphp/bref/blob/master/docs/runtimes/fpm-runtime.mdx Configure the `handler` in `serverless.yml` to point to your application's entry point, similar to Apache or Nginx configurations. ```yaml functions: app: handler: public/index.php ``` -------------------------------- ### Create Personal Passport Client via Serverless CLI Source: https://github.com/brefphp/bref/blob/master/docs/laravel/passport.mdx Use the Serverless CLI to execute the command for creating a personal Passport client. This command is run through the `bref:cli` helper. ```bash serverless bref:cli --args="passport:client --personal --name 'Laravel Personal Access Client'" ``` -------------------------------- ### Write logs to stderr with Monolog and CloudWatchFormatter Source: https://github.com/brefphp/bref/blob/master/docs/environment/logs.mdx Use Monolog with Bref's `CloudWatchFormatter` for structured logs in CloudWatch. Install the `bref/monolog-bridge` package first. ```bash composer require bref/monolog-bridge ``` ```php setFormatter(new CloudWatchFormatter); $log->pushHandler($handler); $log->warning('This is a warning!'); ``` -------------------------------- ### Default Command Execution Source: https://github.com/brefphp/bref/blob/master/docs/runtimes/console.mdx When invoked, the Console runtime executes the handler script directly. For example, `php the-php-script-to-run.php` will run if it's set as the handler. ```sh php the-php-script-to-run.php ``` -------------------------------- ### Anonymous PHP Lambda Function Source: https://github.com/brefphp/bref/blob/master/docs/runtimes/function.mdx An example of a PHP Lambda function written as an anonymous function. It requires the autoloader and returns a closure that processes an event. ```php "}}}' ``` -------------------------------- ### Invoke Lambda Function from PHP Application Source: https://github.com/brefphp/bref/blob/master/docs/runtimes/function.mdx Use the AWS PHP SDK to invoke a Lambda function programmatically. Ensure the SDK is installed via Composer. ```bash $ composer require aws/aws-sdk-php ``` ```php $lambda = new \Aws\Lambda\LambdaClient([ 'version' => 'latest', 'region' => '', ]); $result = $lambda->invoke([ 'FunctionName' => '', 'InvocationType' => 'RequestResponse', 'LogType' => 'None', 'Payload' => json_encode(/* event data */), ]); $result = json_decode($result->get('Payload')->getContents(), true); ``` -------------------------------- ### Deploy using Serverless CLI Source: https://github.com/brefphp/bref/blob/master/docs/default/getting-started.mdx Deploy your application to AWS Lambda using the Serverless CLI. This command initiates the deployment process based on your `serverless.yml` configuration. ```bash serverless deploy ``` -------------------------------- ### Customize AWS SQS Client Configuration Source: https://github.com/brefphp/bref/blob/master/docs/symfony/messenger.mdx Example of customizing the SQS client service definition to apply specific configurations, such as region, when running outside AWS Lambda. ```yaml services: bref.messenger.sqs_client: class: AsyncAws\Sqs\SqsClient public: true # the AWS clients must be public arguments: # Apply your own config here - region: us-east-1 ``` -------------------------------- ### Enable Direct Deployment Method Source: https://github.com/brefphp/bref/blob/master/website/src/pages/news/02-bref-2.0.mdx Set `deploymentMethod: direct` in your `serverless.yml` to enable faster deployments. This option is enabled by default in Bref v2. ```yaml provider: # ... deploymentMethod: direct ``` -------------------------------- ### PowerShell JSON escaping Source: https://github.com/brefphp/bref/blob/master/docs/local-development/event-driven-functions.mdx On Windows PowerShell, JSON data passed via `--data` must have double quotes escaped. This example shows the correct syntax. ```bash $ serverless bref:local -f hello --data '{\"name\": \"Bill\"}' ``` -------------------------------- ### Configure Server-Side Website (PHP) Source: https://github.com/brefphp/bref/blob/master/docs/use-cases/websites.mdx Configure the server-side website construct in serverless.yml for a generic PHP application. Specify assets to be served from S3. ```yaml plugins: - ./vendor/bref/bref - serverless-lift constructs: website: type: server-side-website assets: '/js/*': public/js '/css/*': public/css '/favicon.ico': public/favicon.ico '/robots.txt': public/robots.txt # add here any file or directory that needs to be served from S3 ``` -------------------------------- ### Configuring APP_ENV in serverless.yml Source: https://github.com/brefphp/bref/blob/master/docs/laravel/environments.mdx Configure the Laravel application environment (APP_ENV) within your serverless.yml file. This example maps the deployment stage to APP_ENV, with a specific override for the 'prod' stage to 'production'. ```yaml provider: environment: APP_ENV: ${param:appEnv} params: default: appEnv: ${sls:stage} prod: appEnv: production ``` -------------------------------- ### Set Custom Composer Autoload Path Source: https://github.com/brefphp/bref/blob/master/docs/environment/php.mdx Customize the path where Bref looks for the Composer autoloader by setting the BREF_AUTOLOAD_PATH environment variable in your serverless.yml. The path must be absolute and start with /var/task. ```yaml provider: # ... environment: BREF_AUTOLOAD_PATH: '/var/task/foo-bar/vendor/autoload.php' ``` -------------------------------- ### Deploying with Serverless CLI Source: https://github.com/brefphp/bref/blob/master/docs/laravel/environments.mdx Use this command to deploy your Laravel application to a specific stage using the Serverless CLI. ```bash serverless deploy --stage=prod ``` -------------------------------- ### Create EventBridge Consumer Handler Script Source: https://github.com/brefphp/bref/blob/master/docs/symfony/messenger.mdx Create the PHP handler script that boots the Symfony kernel and retrieves the EventBridgeConsumer service. ```php bootEnv(dirname(__DIR__).'/.env'); $kernel = new \App\Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']); $kernel->boot(); // Return the Bref consumer service return $kernel->getContainer()->get(EventBridgeConsumer::class); ``` -------------------------------- ### Dockerfile with escaped JSON for OctaneHandler Source: https://github.com/brefphp/bref/blob/master/docs/deploy/docker.mdx This example demonstrates how to correctly format the CMD instruction in a Dockerfile when using specific handlers like Bref\LaravelBridge\Http\OctaneHandler, ensuring proper JSON array formatting and escaping. ```dockerfile CMD ["Bref\\LaravelBridge\\Http\\OctaneHandler"] ``` -------------------------------- ### Deploy using Bref Cloud CLI Source: https://github.com/brefphp/bref/blob/master/docs/default/getting-started.mdx Deploy your application using the Bref Cloud CLI. This is an alternative deployment method for Bref Cloud users. ```bash bref deploy ```