Skip to content
Permalink
master
Switch branches/tags
Go to file
10 contributors

Users who have contributed to this file

@mnapoli @DvDty @swiffer @Nyholm @t-richard @eddmann @shouze @chapeupreto @Luitame @allan-simon
title current_menu introduction previous next
Console commands
console-applications
Learn how to run serverless console commands on AWS Lambda with Symfony Console or Laravel Artisan.
link title
/docs/websites.html
Website assets
link title
/docs/web-apps/cron.html
Cron 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.

Configuration

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.

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

Usage

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);