Laravel applications can be successfully deployed to AWS serverless infrastructure using projects such as Bref. On deployments, for optimal performance you want to create cached files for your configuration, events, routes, and views by using php artisan optimize.
One challenge with compiled Laravel configuration files is that they generate absolute, environment-dependent paths. This behavior can be problematic—particularly in CI/CD pipelines. Below is an example of how the compiled configuration appears when built on AWS CodeBuild:
The issue is that the highlighted paths do not exist when the application is deployed to Lambda. Fortunately, Laravel allows you to override this behavior by utilizing the APP_BASE_PATH
environment variable. To resolve the issue, create a symlink that simulates the Lambda entry path and set APP_BASE_PATH
to that path. In my case, that path is /var/task
. Here’s an example of how you could apply this in your buildspec.yml
:
version: 0.2
env:
variables:
phases:
install:
runtime-versions:
php: 8.2
pre_build:
commands:
build:
commands:
- ln -s "$(pwd)" /var/task # <-- Symlink
- export APP_BASE_PATH=/var/task
- php artisan optimize
- unset APP_BASE_PATH
After that your generated cache files should look like this: