Deploying a Laravel to Elastic Beanstalk using codeBuild and codePipeline
In this article, we will deploy a laravel application to Elastic Beanstalk for a specific environment using AWS codeBuild and codePipeline.
We will do everything in the most straightforward way possible so, I would avoid topics like DNS configuration, etc.
The main steps for what we are going to do is as follow:
- Setting up the Elastic Beanstalk
- Setting up a proper codePipeline for your environment
- Complete AWS ElasticBeanstalk configuration
- Setting up a codeBuild for your application
- Connecting and finalizing everything
We assume that you already have a laravel application ready to be deployed in one of AWS supporting git providers like Github or bitbucket.
Step 1: Setting up Elastic Beanstalk
First, you need to create a server (environment) to which you want to deploy your code. We use ASW Elastic Beanstalk to cover everything you need to set up the server, like s3, EC2, etc.
Before starting, create a .env file for your environment, like .env.staging, remove all sensitive data like APP_KEY and database information. Push that file to your git repository.
- In the AWS search box, type Elastic Beanstalk and select it.
- Go to Applications And select
Create new application
. - Select a proper name for the application. Remember to not include your environment name like app_staging or app_production.
- You will be redirected to the Application environment list. Here you can add different environments for your application. Click on
Create new environment
. - Select
Web server environment
and click Next. - Select a proper name that also includes the name of the environment.
- Select PHP from the Platform dropdown menu with the desired version.
- Leave everything the same and click
Create environment
. - If you selected Amazon Linux for your environment, create
laravel.conf
in.platform/nginx/conf.d/elasticbeanstalk
with following code. You can add more configs if you want.
location / {try_files $uri $uri/ /index.php?$query_string;gzip_static on;}
You will be redirected to the status page of your environment creation. It will take some time. You can continue with the next steps.
Step 2: Setting up codePipeline
codePipeline helps you to implement continuous delivery and continuous integration. It connects your application to a proper source provider.
- In the AWS search box, type codePipeline and select the service.
- Click on
Create pipeline
. - Enter a proper name for your pipeline. Remember to include the environment name in your pipeline since different environments get information from different source providers (like different branches) and then click on Next.
- Select desired source provider (here, we use bitbucket).
- Select
Connect to BitBucket
to authorise AWS to access your repository. - If you did not have any connection from before you can create one.
- Select
Connect to Bitbucket
- Enter a proper name for your connection, this does not need to have the environment or application name on it.
- Click on
Install a new app
. - Grant access to your bitbucket account by clicking on
Grant access
. - You will be redirected to the previous page. Click
connect
. - Select desired repository name.
- Select desired branch for your environment and click
Next
. - Select
AWS codeBuild
. - Here you need to provide a codeBuild for your pipeline. For that, click on
Create project
. It will redirect you to the codeBuild environment to create a proper code build. - Select the desired name, including your environment name as well.
- Select a preferred operating system for your build environment.
- Leave everything as it is and select
Continue to CodePipeline
- You will be redirected to the codePipeline page. Click
Next
. - In the next step, select
AWS Elastic Beanstalk
. Select desired application name and application environment, and clickNext
. - Review everything and click
Create pipeline
. Your codePipeline will fail since we have not completed the codeBuild.
Step 3: Complete AWS ElasticBeanstalk configuration
By now, your environment should be created. Go to AWS ElasticBeanstalk and select your environment.
- Select
configuration
. - In row
Database
, clickEdit
. - Enter desired username and password and click
Apply
. - Wait until changes are applied.
- In the row called
Software
, click onEdit
. - As we know laravel applications index file is the public folder. In the
Document root
section, enter/public
and clickApply
. - In Environments, enter all your variables from the .env file that you removed, including the database info that you created.
- To find database info, in the search box, search for
RDS
and select it. - You can see there is one DB instance. Click on that.
- In tab
Connectivity and security
, you can find theDB port
andDB host
. - In tab
Configuration
, you can see theDB name
. - Apply changes and wait until it applies. It will fail. Since our codeBuild is not configured, and there is no code in the server, but we want to read info from public folder. We will fix this issue when we finished the build configuration.
Step 4: Setting up codeBuild
We have already created the basic codeBuild. Let’s configure it.
- In the AWS search box, type codeBuild and select the service.
- Select the created codeBuild.
- Click
Edit
and selectEnvironment
- In the
Environment Variables
section, add the .env files info you entered in the previous section. - Click
Update environment
. - Click on
Edit
and selectbuildspec
- Click Insert
build commands
, then clickSwitch to Editor
. - In this editor, you enter all the steps you need for building the application. You can use the following example: remember to change the .env.staging to your desired environment.
version: 0.2
phases:
install:
runtime-versions:
php: 7.4
nodejs: 12.x
commands:
- apt-get update -y
- apt-get install -y libpq-dev libzip-dev
- apt-get install -y php-pgsql
- curl -sS <https://getcomposer.org/installer> | php -- --install-dir=/usr/local/bin --filename=composer
pre_build:
commands:
- cp .env.staging .env
- composer install
- npm install
build:
commands:
- npm run production
- php artisan migrate --force
- php artisan db:seed
artifacts:
files:
- '**/*'
name: $(date +%Y-%m-%dT%H:%M:%S).zip
proxy:
upload-artifacts: yes
logs: yes
9. Update the buildspec
.
Step 5: Finishing up
Now everything is ready to be deployed.
- Select
codePipeline
from the search box - Select created pipeline
- Click on
Release changes
. - Wait until all steps pass.
- If the build step failed in the migration step, try the following steps.
- Go to this page: https://ip-ranges.amazonaws.com/ip-ranges.json
- Search for CODEBUILD and choose your region. Copy the IP.
- Go to the
RDS
and select your database instance. - In the security group list, select the one with
EC2 Security Group Inbound
. - Select the
Inbound rules
tab - Click on
Edit
Inbound rules. - Click Add rule. Select All TCP for type, custom for source, and add the copied IP in the search input in the opened form.
- Click on
Save rules
and try again.