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-appas root folder for your new application and include theterraform-aws-serverless-brefinside themodulesfolder. 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
artifactfolder:cd ~/code/simple-app/artifact
- Create a new or copy an existing Laravel application inside the
artifactfolder: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
-xargument. -
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.tfvarsfile:cd ~/code/simple-app/ cp terraform.tfvars.example terraform.tfvars -
Edit the
terraform.tfvarsfile:vi terraform.tfvarsPlease, make sure to use the AWS Lambda layers matching version of the
bref/brefPHP 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"