Local development for functions
It is possible to run event-driven functions locally.
To run HTTP applications (like Laravel or Symfony) locally, read Local development for HTTP applications instead.
With Serverless Framework
The serverless bref:local
command invokes your PHP functions locally, using PHP installed on your machine. You can provide an event if your function expects one.
The serverless bref:local
command is a simpler alternative to the native serverless invoke local
command, which tries to run PHP using Docker with very little success. Use serverless bref:local
instead of serverless invoke local
.
For example, given this function:
return function (array $event) {
return 'Hello ' . ($event['name'] ?? 'world');
};
# ...
functions:
hello:
handler: my-function.php
runtime: php-81
You can invoke it with or without event data:
$ serverless bref:local -f hello
Hello world
# With JSON event data
$ serverless bref:local -f hello --data '{"name": "Jane"}'
Hello Jane
# With JSON in a file
$ serverless bref:local -f hello --path=event.json
Hello Jane
On Windows PowerShell, you must escape the "double quote" char if you write JSON directly in the CLI. Example:
$ serverless bref:local -f hello --data '{\"name\": \"Bill\"}'
The serverless bref:local
command runs using the local PHP installation. If you prefer to use Docker, check out the "Without Serverless Framework" section below.
Without Serverless Framework
If you do not use serverless.yml
but something else, like SAM/AWS CDK/Terraform, use the vendor/bin/bref-local
command instead:
$ vendor/bin/bref-local <handler> <event-data>
# For example
$ vendor/bin/bref-local my-function.php
Hello world
# With JSON event data
$ vendor/bin/bref-local my-function.php '{"name": "Jane"}'
Hello Jane
# With a path to a file containing a JSON event.
$cat event.json
{
"name": "Alex"
}
$ vendor/bin/bref-local --path event.json my-function.php
Hello Alex
With Docker
If you want to run your function in Docker:
$ docker run --rm -it --entrypoint= -v $(PWD):/var/task:ro bref/php-81:2 vendor/bin/bref-local my-function.php
# You can also use the `dev` images for a simpler command (and Xdebug and Blackfire in the image):
$ docker run --rm -it -v $(PWD):/var/task:ro bref/php-81-fpm-dev:2 vendor/bin/bref-local my-function.php
You can also use Docker Compose, like described in Local development for HTTP applications:
version: "3.5"
services:
app:
image: bref/php-81-fpm-dev:2
volumes:
- .:/var/task
Then run functions:
$ docker-compose run app vendor/bin/bref-local my-function.php
API Gateway local development
If you build HTTP applications with API Gateway HTTP events, serverless bref:local
is a bit unpractical because you need to manually craft HTTP events in JSON.
Instead, you can use the bref/dev-server
(opens in a new tab) package to emulate API Gateway locally.