Programmatically create the AWS serverless infrastructure for a PHP Laravel application using Terraform.
Thanks to Bref for providing the AWS Lambda layers for PHP.
- Use
examples/simple-app
as root folder for your new application and include theterraform-aws-serverless-bref
inside themodules
folder. E.g.:cp -r ~/code/terraform-aws-serverless-bref/examples/simple-app ~/code cd ~/code/simple-app mkdir modules cd modules cp -r ~/code/terraform-aws-serverless-bref .
- Change directory to the
artifact
folder:cd ~/code/simple-app/artifact
- Create a new or copy an existing Laravel application inside the
artifact
folder:composer create-project laravel/laravel example-app cd example-app
-
Install the Javascript dependecies:
npm install npm build
-
Generate the app key:
php artisan key:generate
-
Include the Bref packages:
composer require bref/bref bref/laravel-bridge --update-with-dependencies
-
Install the PHP dependecies:
composer install
-
Create a static configuration cache file:
php artisan config:cache
-
Delete the existing archive (if it exists):
rm ../simple-app.zip
-
Compress the application in an archive:
zip -r ../simple-app.zip . -x 'node_modules/*' 'public/storage/*' 'resources/assets/*' 'storage/*' 'tests/*'
If you want to exclude additional directories or files, add their patch after the
-x
argument. -
Create a checksum for the archive:
cd ~/code/simple-app/artifact openssl dgst -sha256 -binary simple-app.zip | openssl enc -base64 > simple-app.zip.sha256
-
Create the
terraform.tfvars
file:cd ~/code/simple-app/ cp terraform.tfvars.example terraform.tfvars
-
Edit the
terraform.tfvars
file:vi terraform.tfvars
Please, make sure to use the AWS Lambda layers matching version of the
bref/bref
PHP package for the exact region you are provisioning the infrastructure to.
-
Initate Terraform:
terraform init
-
Preview the infrastructure:
terraform plan
-
Create the infrastructure:
terraform apply # Review and respond with "yes"