Problem

This is my first time dealing with AWS. Initially, I setup my Laravel project in a normal EC2 instance, and manually install mysql, web server, etc.

Then a friend of mine told me that this kind of infrastructure is not scalable. Eventually I go for Elastic Beanstalk.

Create an AWS account

You need a valid credit card to perform this action. Just follow the instruction will do.

Create a new app in Elastic Beanstalk

AWS services

Then choose Web server environment.

AWS Elastic Beanstalk environment

Platform select PHP.

AWS Elastic Beanstalk - environment config

You can temporary choose Sample application first.

Or you can use command line to create (see here)

1
2
$ cd /path/to/project/
$ eb create <environment> -t webapp

AWS Elastic Beanstalk - overview

Now you can click on the link above to see your sample page

Install eb in your local

Follow the instruction here

Once you have installed. Run the following command

1
$ eb init

You will be prompted to key in AWS credential. Refer the instruction here

Enable ssh login

Go to Configuration on your left side, then select Instances

AWS Elastic Beanstalk - ssh login

If you don’t have a key, create it. Download it and put into your ~/.ssh/ directory.

NOTE: Bare in mind that whenever you ssh in and change something, the changes only temporary, once it scale up and down the changes will be lost

Add environment variables

If you notice, there is a file named .env.sample in all laravel project, and you need to rename/copy it as .env (without sample), this is because the configuration cannot be push to git repo, due to security reason.

Now, in elasticbeanstalk, if you ssh in and rename it to .env, then when it scale up and down, the .env will be lost, and the site may be down. Thus, the you have to add the config settings to environment variables

AWS Elastic Beanstalk - software configuration

AWS Elastic Beanstalk - environment variables

Configure RDS for your project

Go to Services (in the top left), and choose RDS, then follow the instruction.

Now go to your instances

AWS Elastic Beanstalk - RDS instances

Get the link and put it in your environment variable.

1
DB_HOST=zzzzz.xxxxxxxxxxxx.ap-southeast-1.rds.amazonaws.com

Deploy

Now, the deployment is pretty simple (assume you’re using git here)

In your local, cd to the project root, then run a command

1
$ eb deploy <environment>

It will deploy the latest commit, if you have any changes which not committed yet, it won’t be deployed.

Mission Completed!!!

References: