0

I am investigating on how I can create an AWS lambda in php using the bref library

Therefore, according to documentation I set up the environment with the following command cocktail:

sudo -H npm install -g serverless
composer require bref/bref

Then using the following command created my first php lambda:

 vendor/bin/bref init

And I selected the first option PHP Function provided by default. Creating the following creating an index.php file:

declare(strict_types=1);

require __DIR__.'/vendor/autoload.php';

lambda(function ($event) {
    return 'Hello ' . ($event['name'] ?? 'world');
});

Then I changed my serverless.yml into that:

service: app

provider:
    name: aws
    region: eu-central-1
    runtime: provided
    stage: ${opt:stage,'local'}

package:
    exclude:
        - '.gitignore'

plugins:
    - ./vendor/bref/bref

functions:
    dummy:
        handler: index.php
        name: Dummy-${self:provider.stage}
        description: 'Dummy Lambda'
        layers:
            - ${bref:layer.php-73}

And I try to launch it via the following command:

sls invoke local --stage=local --docker --function dummy

But I get the following error:

{"errorType":"exitError","errorMessage":"RequestId: 6403ebee-13b6-179f-78cb-41cb2f517460 Error: Couldn't find valid bootstrap(s): [/var/task/bootstrap /opt/bootstrap]"}

Therefore, I want to ask why I am unable to run my lambda localy?

Dimitrios Desyllas
  • 9,082
  • 15
  • 74
  • 164

3 Answers3

0

Since this question is getting a lot of views, I recommend to have a look at the Bref documentation:

Local development for PHP functions

That involves using the bref local CLI command instead of serverless invoke local:

$ vendor/bin/bref local hello
Hello world

# With JSON event data
$ vendor/bin/bref local hello '{"name": "Jane"}'
Hello Jane

# With JSON in a file
$ vendor/bin/bref local hello --file=event.json
Hello Jane
Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261
0

On my local, clearing cache before invoking lambda worked fine, I'm using linux / ubuntu

docker system prune --all
sudo apt-get autoremove
sudo apt-get clean
sudo apt-get autoclean
sudo rm -rf ~/.cache/
sudo rm -rf /var/cache/
-1

It is a known bug for bref. It can be solved via providing the layer manually on your function in serverless.yml. So the functions section at serverless.yml should change from:

functions:
    dummy:
        handler: index.php
        name: Dummy-${self:provider.stage}
        description: 'Dummy Lambda'
        layers:
            - ${bref:layer.php-73}

Into:

functions:
    dummy:
        handler: index.php
        name: Dummy-${self:provider.stage}
        description: 'Dummy Lambda'
        layers:
            - 'arn:aws:lambda:eu-central-1:209497400698:layer:php-73:15'

The reason why is that ${bref:layer.php-73} cannot be resolved into a php layer. Therefore, you need to provide manually the arn for the lambda layer.

Keep in mind that the arn comes into various "versions" that is being inidcated from the last number in the arn seperated with :. So in the arn

arn:aws:lambda:eu-central-1:209497400698:layer:php-73:15

Indicated that the layer is in version "15" whitch is thje latest at the moment of the answer. The next one logically should be the:

arn:aws:lambda:eu-central-1:209497400698:layer:php-73:16
Dimitrios Desyllas
  • 9,082
  • 15
  • 74
  • 164
  • 1
    If anyone's looking for the bref AWS runtimes, they are documented here: [https://runtimes.bref.sh/](https://runtimes.bref.sh/). – alexkb Oct 22 '20 at 04:18