Serverless computing has become a popular approach for building scalable and cost-effective applications. With serverless, you can focus on writing code without worrying about managing servers, infrastructure, and scaling. AWS Lambda is a well-known serverless platform that allows you to run code without provisioning or managing servers.

However, until recently, running PHP on AWS Lambda was not very easy or convenient. This is where Bref comes into play. Bref is an open-source project that makes it easy to run PHP applications on AWS Lambda.

In this article, we will show you how to install and use Bref to deploy a serverless PHP application on AWS Lambda. We will walk you through the process step-by-step and provide examples along the way.

What can I do with Bref ?

Bref can be used to run various types of PHP applications on AWS Lambda. Here are some examples:

  1. APIs: Bref can be used to create APIs using popular PHP frameworks such as Symfony, Laravel, and Lumen. APIs created using Bref can handle HTTP requests and respond with JSON data.
  2. Workers: Bref can also be used to run background workers for processing tasks such as sending emails, processing images, or generating reports. Workers can be triggered by a queue system like AWS Simple Queue Service (SQS).
  3. Batch processes/scripts: Bref can be used to run PHP scripts that perform a specific task, such as generating a report or processing a large amount of data.
  4. Websites: Bref can also be used to serve static websites or dynamic websites built with PHP frameworks. The assets can be stored in Amazon S3 and served through Amazon CloudFront.
  5. Legacy applications: Bref can be used to migrate legacy PHP applications to AWS Lambda. However, this might require adapting some parts of the code to work with the read-only filesystem in AWS Lambda, making cron tasks, scripts or asynchronous jobs compatible with AWS Lambda and SQS.

Bref for Enterprise

Bref offers three different plans for users to choose from: Bref, Bref Pro, and Bref Enterprise.

The basic Bref plan is free and open-source, and includes access to the Bref open-source project, its documentation, and framework integrations. With the Bref plan, users can deploy their applications in their own AWS account, or use the public AWS Lambda runtimes hosted in the Bref AWS account.

The Bref Pro plan, priced at $100 per month, includes all the features of the basic Bref plan, as well as the ability to use AWS Lambda runtimes hosted in your own AWS account, and expert support via Slack and email.

For larger organizations with more complex needs, Bref Enterprise offers even more features and support. Starting at $1,000 per month, the Bref Enterprise plan includes all the features of the Bref Pro plan, as well as tailor-made AWS Lambda runtimes optimized for your project, expert support via Zoom, Slack, and email, GitHub/GitLab review for infrastructure code, engineer-led introduction and onboarding, and team access to the Serverless Visually Explained course.

Whether you’re an individual developer, a small team, or a large enterprise, Bref offers a range of plans and features to meet your needs and help you build serverless PHP applications with ease.

Installation:

Before we can start using Bref, we need to install it. Bref requires PHP 7.4 or later and the AWS CLI to be installed on your local machine. Once you have these prerequisites installed, you can install Bref using Composer:

composer require bref/bref

Usage:

To deploy a serverless PHP application using Bref, we need to create a function handler that will receive events and return responses. Let’s create a simple example that will return a “Hello, world!” message:

<?php

use Bref\Context\Context;
use Bref\Event\Http\HttpRequestEvent;

function handler(HttpRequestEvent $event, Context $context): string
{
    return 'Hello, world!';
}

This is a simple function that receives an HTTP request event and a context object and returns a string response. We can now deploy this function using Bref.

To deploy the function, we need to create a serverless.yml file that describes the function and its configuration:

service: my-service

provider:
  name: aws
  runtime: provided.al2

functions:
  hello:
    handler: index.php
    layers:
      - ${bref:layer.php-74-fpm}
    events:
      - http: ANY /

In this example, we define a function called “hello” that uses the handler function we created earlier. We also specify that the function should use the PHP 7.4 runtime provided by Bref and should be triggered by any HTTP request to the root path.

We can now deploy this function using the following command:

vendor/bin/bref deploy

This will create a new AWS Lambda function with the specified configuration and handler. You can test the function by visiting the URL provided by the AWS API Gateway that was automatically created for the function.

Conclusion:

Bref makes it easy to deploy serverless PHP applications on AWS Lambda. With Bref, you can focus on writing code without worrying about managing servers or infrastructure. In this article, we showed you how to install and use Bref to deploy a simple “Hello, world!” function. We hope this article will help you get started with serverless PHP and Bref.

LEAVE A REPLY

Please enter your comment!
Please enter your name here