Deploying Symfony Console applications on AWS Lambda offers a scalable, serverless solution, removing the need to manage infrastructure and allowing for flexible scaling based on demand. With the open-source Bref library, developers can seamlessly deploy PHP applications like Symfony on AWS Lambda, benefiting from Lambda’s serverless infrastructure. In this guide, we’ll walk through the steps required to deploy a Symfony Console application on AWS Lambda using Bref, from setup to deployment and monitoring.

Prerequisites: Setting Up AWS and Bref

Before deploying a Symfony Console application on AWS Lambda, ensure you have the following:

  1. AWS Account: If you don’t have an AWS account, sign up for one. We’ll use several AWS services, including Lambda and EventBridge.
  2. AWS CLI: Install and configure the AWS CLI for seamless interaction with AWS services from the command line.
  3. Composer: Install Composer, a dependency manager for PHP.
  4. Bref: Install Bref, an open-source library that simplifies deploying PHP applications on AWS Lambda.
  5. Symfony CLI: The Symfony CLI is recommended for creating Symfony applications quickly.

With these prerequisites, we’re ready to create and configure our Symfony application for deployment.

Step 1: Creating a Symfony Console Application

Begin by creating a new Symfony Console application:

composer create-project symfony/skeleton my-symfony-console-app

cd my-symfony-console-app

Add the symfony/console component to the application, as it provides the structure for building command-line applications:

composer require symfony/console

In the src/Command directory, create a new Symfony command:

<?php

// src/Command/HelloCommand.php

namespace App\Command;

use Symfony\Component\Console\Command\Command;

use Symfony\Component\Console\Input\InputInterface;

use Symfony\Component\Console\Output\OutputInterface;

class HelloCommand extends Command

{

    protected static $defaultName = ‘app:hello’;

    protected function configure()

    {

        $this->setDescription(‘Outputs Hello World.’);

    }

    protected function execute(InputInterface $input, OutputInterface $output)

    {

        $output->writeln(‘Hello, World!’);

        return Command::SUCCESS;

    }

}

Register this command in src/Kernel.php if Symfony does not detect it automatically.

Step 2: Preparing the Application for AWS Lambda Deployment

Now that our Symfony Console application is ready, it is time to prepare it for AWS Lambda deployment. The Bref library provides Lambda runtimes for PHP, which makes this possible.

  1. Install Bref:
    bash
    Copy code
    composer require bref/bref
  2. Add Bref configuration: Create a serverless.yml file in the root of your application. This file will define the function and configuration for deployment.
    service: my-symfony-console-app

provider:

  name: aws

  runtime: provided.al2

  region: us-east-1

functions:

  hello:

    handler: bin/console

    layers:

      – ${bref:layer.php-81}  # PHP 8.1 layer for Lambda

    timeout: 120  # Lambda timeout in seconds

  1. Install Console Handler: Add Bref’s Console Handler for Symfony:
    composer require bref/console

In the serverless.yml file, ensure the path to bin/console is correctly set as the Lambda handler.

Step 3: Configuring AWS Lambda with Bref

With the serverless.yml configuration in place, we’re ready to deploy the function using the Serverless framework:

  1. Install Serverless Framework:
    npm install -g serverless
  2. Deploy the Application:
    vendor/bin/bref deploy

Serverless will deploy your Symfony application to AWS Lambda, and you’ll receive an endpoint or trigger ARN.

Step 4: Scheduling the Lambda Function with Amazon EventBridge

To automatically run the Symfony Console command at scheduled intervals, configure Amazon EventBridge to trigger the Lambda function.

  1. Set Up EventBridge Rule: In the AWS Management Console, go to EventBridge, and create a rule for scheduling the Lambda function. Set the schedule expression based on the desired interval (e.g., every hour: cron(0 * * * ? *)).
  2. Attach the Lambda Function: Attach the previously deployed Lambda function as the target of this EventBridge rule, ensuring the function runs on schedule.

Step 5: Testing and Monitoring the Deployment

Once deployed and scheduled, it’s essential to test and monitor the Lambda function:

  1. Testing: Manually invoke the function in the AWS Lambda console. Check the logs in Amazon CloudWatch to ensure they output Hello, World! as expected.
  2. Monitoring: Use CloudWatch to monitor the function’s performance, including execution time, memory usage, and errors. Set up alerts for errors or threshold breaches to maintain optimal performance.
  3. Logging: Symfony logs can also be routed to CloudWatch, providing insight into application-specific errors.

Conclusion

Deploying a Symfony Console application on AWS Lambda with Bref enables you to benefit from AWS’s serverless architecture without maintaining servers or infrastructure. This guide covered the entire deployment process, from setup to scheduling and monitoring. Using Bref, AWS Lambda, and EventBridge, you can effortlessly scale your Symfony Console applications to meet demand.

References

AWS Lambda and Application Auto Scaling

Building PHP Lambda functions with Docker container images