Console commands

Bref provides a way to run console commands on AWS Lambda.

This can be used to run PHP scripts, the Symfony Console, as well as Laravel Artisan commands in production.


The lambda function used for running console applications must use two Lambda layers:

  • the base PHP layer that provides the php binary,
  • the console layer that overrides the base runtime to execute our console commands.

Below is a minimal serverless.yml. To create it automatically run vendor/bin/bref init and select "Console application".

service: app
    name: aws
    runtime: provided.al2
    - ./vendor/bref/bref
        handler: bin/console # or 'artisan' for Laravel
            - ${bref:layer.php-74} # PHP runtime
            - ${bref:layer.console} # Console layer


To run a console command on AWS Lambda, run bref cli on your computer:

vendor/bin/bref cli <function-name> -- <command>

<function-name> is the name of the function that was deployed on AWS. In our example above that would be hello-dev because Serverless adds the stage (by default dev) as a suffix.

Pass your command, arguments and options by putting them after --. The -- delimiter separates between options for the bref cli command (before --) and your command (after --).

vendor/bin/bref cli hello-dev <bref options> -- <your command, your options>

For example:

# Runs the CLI application without arguments and displays the help
$ vendor/bin/bref cli hello-dev
# ...

$ vendor/bin/bref cli hello-dev -- doctrine:migrations:migrate
Your database will be migrated.
To execute the SQL queries run the command with the `--force` option.

$ vendor/bin/bref cli hello-dev -- doctrine:migrations:migrate --force
Your database has been migrated.

# Use environment variables to configure your AWS credentials
$ AWS_DEFAULT_REGION=eu-central-1 AWS_ACCESS_KEY_ID=foo AWS_SECRET_ACCESS_KEY=bar vendor/bin/bref cli my-function
# ...

Lambda context

Lambda provides information about the invocation, function, and execution environment via the lambda context.

This context is usually available as a parameter (alongside the event), within the defined handler. However, within the console runtime we do not have direct access to this parameter. To work around that, Bref puts the Lambda context in the $_SERVER['LAMBDA_INVOCATION_CONTEXT'] variable as a JSON-encoded string.

$lambdaContext = json_decode($_SERVER['LAMBDA_INVOCATION_CONTEXT'], true);