In this blog we will discuss how to deploy Laravel applications using AWS Elastic Beanstalk and the step by step process, to have a clear view of the configuration to be done.

What is AWS Elastic Beanstalk?

AWS Elastic Beanstalk is a platform as a Service ( PaaS ) Which allows developers to deploy applications at an ease. It uses the following AWS products.

  • Elastic Compute Cloud (EC2)
  • Simple Storage Service (S3)
  • CloudWatch
  • Simple Notification Service (SNS)

EB can also be managed with AWS Relational Database Service ( RDS ) instance but this is not recommended for a Laravel application as an RDS instance created in association with EB environment will also be terminated when the environment is terminated.

Requirements that need to be checked before startup.

  • Git repository should have initialized with the Laravel application.
  • AWS account must have been created
  • Need to know the basics of Web Server Environment.

Creating an RDS instance

We need to create an MySQL RDS instance for our Laravel MySQL database .

Step 1:

1

Step 2:

Depending on our application requirement we can chose whether we want Multi-AZ deployment for the Database. As we are using it for Development we can use free tier usage.

2

Step 3: Settings for DB

If we have one RDS instance running at a time we can use free usage tier.

3

Step 4: Advanced Settings

We can select a default VPC and default DB Subnet Group. Finally we can give a database name and Launch DB Instance Name.

4

Step 5: Security Credentials

If we already have an Access Key ID and Secret Access Key for our account, we can skip this section else we can select create key Pair. The Key Pair is downloaded as .pem we need this file to do SSH for our EC2 instance.

Step 6: Install the EB CLI

We can interact with Elastic Beanstalk by GUI in the AWS Management Console. For quicker and easy way we need to use eb command line interface (CLI).  we can download the tool from http://aws.amazon.com/code/6752709412171743.

We need to Navigate to the root directory of our Laravel git Repository and type the command

eb init

We will be promoted to enter our AWS Key ID and AWS Secret Key. Which we generated in the previous step 5.

We can choose single instance to ensure that we are in free usage tier and as we are only in development.

Next we need to attach an instance profile. This gives EC2 instance to create security permissions to access other AWS services such as S3.

This Creates the Elastic Beanstalk application associated with a git.

In the Management Console we will see our application listed under “All Applications”  by navigating to our project directory we will see a new directory “.elasticbeanstalk” which contains a config file with all our preferences which we just set. This Directory is also added in the .gitignore file.

There is  also an option to create RDS instance Manually using the code method.

We can off the auto-scaling option by giving Max and MinSize both as 1. This means that we will have only one server in running if that servers gets down another server gets launched automatically.

If we have the RDS instance in the same zone as EC2 instance this will cost less and at the same time it will be faster.

Next step is we will edit the EC2KeyName with the same name as the key pair which we created this will allow us to SSH into our EC2 instance .

The aws:elasticbeanstalk:application:environment contains our environment variables, which are added by our config files it just displays them and there is no impact on edit.

Step 7: Modification needed

under aws:elasticbeanstalk:container:php:phpini. We need to set composer_options to –no-dev, so that dev add-ons aren’t installed when composer install is running. Last, we’ll set document_root to /public so that it points to Laravel’s public folder.

Step 8: Add Environment Config Files

Located in the .ebextensions directory in the root of the project, the config files (*.config) contain commands for the environment to run and options to set. These config files run every time git aws.push is run (i.e., the environment is updated or a new EC2 instance within the environment is started), and they are run in alphabetical order. These config files SHOULD NOT be in your .gitignore file.

To start, we will create three files:

00environmentVariables.config,
01composer.config,  and
02artisan.config.

Environment Variables

In the 00environmentVariables.config file, we will place all of the instructions for the application to modify the environment’s options, in this case to create environment variables (e.g., DB_HOST). Add the following code to this file:

option_settings:
– namespace: was:elasticbeanstalk:application:environment
option_name: DB_HOST
value: mysqldbname.ebs.us-east-1.rds.amazonaws.com
– option_name: DB_PORT
value: 3306
– option_name: DB_NAME
value: EBS
– option_name: DB_USER
value: EBS
– option_name: DB_PASS
value: EBS

Here, namespace refers to the specific groups of options in .elasticbeanstalk/optionsettings.app-environment-name. Using the namespace elasticbeanstalk:application:environment we are stating that the options and their values below are for that namespace.

DB_HOST will be the endpoint shown in the RDS dashboard for the RDS instance set up earlier. All other environmental variables need to be the same.

Composer Commands

In the 01composer.config file, we will place all the composer commands to be run when a new instance is created or an existing instance is updated. Add the following code to this file:

commands:
01updateComposer:
command: export COMPOSER_HOME=/root && /usr/bin/composer.phar self-update

option_settings:
   – namespace: was:elasticbeanstalk:application:environment
     option_name: COMPOSER_HOME
     value: /root1

container_commands:
01optimize:
command: “/usr/bin/composer.phar dump-autoload —optimize”

commands are executed first, which are run before the application and web server are set up. Here we self-update composer.phar to ensure the latest version is running on the instance.

Next we set a COMPOSER_HOME environment variable.

Note:  If a Vendor Folder is not there in the root directory EB will automatically run composer.phar install if it sees a composer.json file in the root directory. If your vendor folder is not in .gitignore, you will need to add composer.phar install to this file ourself.

Artisan Commands

In the 02artisan.config file, we specify container commands to run migrations and seeding.

These commands should ideally be run only once, or if you are adding/modifying tables. I also tend to just migrate:refresh the database every now and then on the development server, as error tend to compound themselves and exceptions start cropping up in my app.

container_commands:
01migrateSeed:
command: “php artisan migrate –force”
02seed:
command: “php artisan db:seed —force”

Here we migrate to create the new database (including migrating the Auth Token package) and seed the database.

Step 9: Git Commit

With all of these changes now complete, commit these changes to your git repository.

Step 10: Add Security Group Inbound Rule

This is the last thing to do to give the Elastic Beanstalk application access to the MySQL RDS instance.

Go to AWS Management Console and click on “Security Groups” under “Network & Security”. Here we can see two security groups: the default security group and a security group created for you Elastic Beanstalk application. We need to click the Elastic Beanstalk security group and copy the Group ID.

Next we need to Right click on the default security group and select “Edit Inbound Rules”.

Here we need to add a MySQL type rule with a Custom IP equal to the Elastic Beanstalk security group’s Group ID.

Step 11: Execute the EB environment Commands as required 

eb start
eb status
git aws.push
eb stop
eb delete
eb Deploy

: AWS will set the resource necessary for our environment.
: To Check the Environment Status
: To upload the Git repo to the environment
: To Terminate the Environment
: To Delete our Application
: Deploys the application source bundle from the initialised project directory to the running application.

Hope this blog has cleared you how to deploy the Laravel applications using AWS Elastic Beanstalk. We will also see the things that need to be concentrated in the code while doing SSL In the next coming blogs.

LEAVE A REPLY

Please enter your comment!
Please enter your name here