A template for deploying Laravel applications with AWS CodeDeploy across an autoscaling group.
The following roles should be created BEFORE any launch configurations, autoscaling groups or CodeDeploy applications are created as they'll be needed during that creation process.
code-deploy-role
- Requires the pre-baked policy AWSCodeDeployRole
code-deploy-ec2-instance-role
- Requires the pre-baked policy AmazonEC2FullAccess
- Requires a custom policy code-deploy-ec2-permissions with the following:
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"s3:Get*",
"s3:List*"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
- A compatible Ubuntu 16.04 AMI (use the provided provision.sh script on a fresh instance and save an AMI image from that)
- A classic load balancer (preferably with sticky connections [5mins+])
- An AWS launch configuration (using the aforementioned AMI) and an autoscaling group
- A CodeDeploy application and deployment group that references the aforementioned autoscaling group
- A S3 bucket for your application with two folders contained within:
bundles
andenv
- Copy the
scripts
directory into the root of your Laravel application, as well as thebundle.sh
andappspec.yml
files - Replace the
XXX
placeholder inside ofbundle.sh
andscripts/finish_install.sh
with your S3 bucket name - Inside of the S3 bucket folder
env
place aproduction.env
file that contains your production settings. This will be copied to each instance as a.env
file during each deployment cycle
- Make sure
composer
andnpm
have been run and all dependencies are up to date and that the application works as expected locally and on staging - Use
bundle.sh
to automatically create atar.gz
archive of your Laravel application and upload it to the S3 bucket folderbundles
- The
bundle.sh
will tar only the directories and files listed inbundle.conf
- Create a new CodeDeploy revision deployment using S3 as the source and point the endpoint to the one provided by the
bundle.sh
script
The provided provision.sh
script used to generate the instance needed for the AMI does the following:
- Installs NGINX, PHP7.0-FPM, Composer, NPM, the CodeDeploy Agent, the AWS CLI and a bunch of related/required dependencies
- Configures NGINX to serve content out of the
/var/www/public
directory - Only allows
index.php
to be executed viaPHP7.0-FPM