Example project that deploys a Laravel application on AWS via Elastic Beanstalk.
-
Copy
.env.example
to.env
, then update environment variables in.env
. -
Start Docker containers
docker-compose -f docker-compose.yml -f .docker/docker-compose/docker-compose.local.yml up -d
-
Compile PHP and JS
-
SSH into app container
docker exec -it app bash
-
Compile PHP and JS
composer install npm install && npm run dev
-
-
Migrate and seed data if required
-
SSH into app container
docker exec -it app bash
-
Migrate and seed data
php artisan migrate php artisan db:seed
-
-
Access Laravel application at http://localhost:80
-
Create AWS VPC
-
Update parameters in
vpc-parameters.json
-
Create AWS VPC
aws cloudformation create-stack --stack-name REPLACE_ME_VPC_STACK_NAME --template-body file://.cfn/vpc.yml --parameters file://.cfn/vpc-parameters.json
-
-
Create AWS RDS
-
Update parameters in
rds-parameters.json
-
Create AWS RDS
aws cloudformation create-stack --stack-name REPLACE_ME_RDS_STACK_NAME --template-body file://.cfn/rds.yml --parameters file://.cfn/rds-parameters.json
-
-
Add environment variables to AWS System Manager Parameter Store. The parameter names should begin with
/Laravel/
-
Create AWS Elastic Beanstalk application and environment
-
Update parameters in
eb-parameters.json
-
Create AWS Elastic Beanstalk application and environment
aws cloudformation create-stack --stack-name REPLACE_ME_EB_STACK_NAME --template-body file://.cfn/eb.yml --parameters file://.cfn/eb-parameters.json --capabilities CAPABILITY_NAMED_IAM
-
-
Create AWS CodePipeline to deploy code to AWS Elastic Beanstalk
Post deploy script is used to perform database migrations after every deployment to AWS Elastic Beanstalk. See: .platform/hooks/postdeploy/01_database_migrations.sh
This example uses multi-stage Dockerfile to allow the local environment to mimic the production environment as much as possible.
Docker compose file | Description |
---|---|
.docker/docker-compose/docker-compose.local.yml |
Local development Docker compose file |
.docker/docker-compose/docker-compose.eb.yml |
Elastic Beanstalk specific Docker compose file |
This example uses AWS System Manager Parameter Store to manage environment variables. Environment variables added to Elastic Beanstalk's option settings are merged with environment variables from Parameter Store. The priority of environment variables are:
- Environment variables added to Elastic Beanstalk's option settings
- Environment variables from Parameter Store
A pre-build script is used to get environment variables from Parameter Store and saves them to .env file. See: .platform/hooks/prebuild/02_environment_variables.sh
Docker volumes are used to allow logs from Laravel application and Nginx web server to be retrieved from Elastic Beanstalk instance. See: .docker/docker-compose/docker-compose.eb.yml