brefphp / bref

Serverless PHP on AWS Lambda
https://bref.sh
MIT License
3.06k stars 364 forks source link

Feedback for “EventBridge event bus” -> handler can not be found #1716

Open Eviltoastey opened 6 months ago

Eviltoastey commented 6 months ago

the documentation states implementing this will create a lambda called events and create an eventRule too:

functions:
    # ...
    events:
        handler: App\MyHandler
        events:
            - eventBridge:
                pattern:
                    detail-type:
                        - 'MyCustomEvent'

However when I add my handlers (I gave 2 examples) I walk into two issues:

My example code:

functions:
    vacancy-applied-send-to-dynamics:
        handler: App\EventBridgeHandlers\SendVacancyToDynamicsHandler\SendVacancyToDynamicsHandler
        runtime: php-82-fpm
        events:
          - eventBridge:
              pattern:
                  detail-type:
                      - 'VancancyApplied'

    vacancy-import-cluen-scheduled:
      handler: App/EventBridgeHandlers/ImportVacanciesFromCluen/ImportVacanciesFromCluenHandler
      runtime: php-82-fpm
      events:
        - eventBridge:
            name: vacancy-import-cluen-scheduled
            description: Imports vacancies from external service every 60 minutes
            schedule: rate(60 minutes)

What am I doing wrong here? Serverless creates the correct eventRule and couples the lambda correctly. I can see in cloudwatch that the lambda actually gets fired but the result is always the same: Handler can not be found.

I hope someone can help me out! Thanks in advance :)

Eviltoastey commented 6 months ago

Small update on the matter.

As runtime you should use: runtime: php-82

It seems like it will go a step further then. Only problem after adding this is that I will get the following error:

{
  "errorMessage": "app/EventBridgeHandlers/CreateVacancyInCraftHandler/CreateVacancyInCraftHandler",
  "errorType": "Illuminate\\Container\\EntryNotFoundException",
  "stackTrace": [
    "#0 /var/task/vendor/bref/laravel-bridge/src/HandlerResolver.php(46): Illuminate\\Container\\Container->get('app/EventBridge...')",
    "#1 /var/task/vendor/bref/bref/src/FunctionRuntime/Main.php(27): Bref\\LaravelBridge\\HandlerResolver->get('app/EventBridge...')",
    "#2 /opt/bref/bootstrap.php(17): Bref\\FunctionRuntime\\Main::run()",
    "#3 {main}"
  ]
}

It seems like the entry now cant be found? The App folder is autoloaded and all namespaces is correct. I'm wondering why it can't find the handler.

mnapoli commented 6 months ago

Regarding the 1st point: runtime: php-82 is the correct configuration, yes. That's something I thought was obvious when writing the docs, but actually isn't at all. This is because the EventBridge docs (like SQS and others) have been split up from a long page that told you to use this runtime. It's not the case anymore, so I understand how confusing this is.

TL/DR: the documentation should be fixed, thank you for the report 👍

Regarding the 2nd issue: App/EventBridgeHandlers/ImportVacanciesFromCluen/ImportVacanciesFromCluenHandler isn't a valid class name. You might want to use \ instead of / as the namespace separator.