Deploy a Laravel PHP App on Github to AWS Elastic Beanstalk using CodePipeline – Complete Guide

How to Deploy a Laravel PHP App to AWS Elastic Beanstalk using CodePipeline

This article demonstrates how to deploy a PHP (Laravel) application to Elastic Beanstalk using AWS CodePipeline. A breakdown of the article is as follows.

This article is a complete walkthrough; by the end, you’ll have a CodePipeline ready to auto-deploy from GitHub to Elastic Beanstalk.

Before We Deploy to AWS Elastic Beanstalk from GitHub, We Must Have a Laravel App

If you’re replicating the steps of this walkthrough, make sure you do the following.

  • Fork or clone this repository from GitHub.
  • Deploy the PHP Laravel application to Elastic Beanstalk.

Don’t worry if you are not familiar with Elastic Beanstalk. FuelingPHP’s “Host and Deploy a Laravel PHP App to AWS Elastic Beanstalk” is a complete walkthrough of AWS Elastic Beanstalk and could help you understand deployment to AWS Elastic Beanstalk. This article uses the same linked repository – a sample Laravel application.

This article is a continuation or an extension of the previous article, where we deployed a Laravel application to Elastic Beanstalk. We left a side note about CI/CD pipeline and how that can help us deploy from the GitHub repository whenever there’s a change.

So, let’s build a CI/CD pipeline using AWS CodePipeline.

Background on AWS CodePipeline. What is it?

AWS CodePipeline is an AWS service for continuous delivery. Continuous delivery is a popular concept in agile software development, where code changes are automatically prepared for deployment to a production server. As it is a widespread industry practice, there’s much to expand on CI/CD (Continuous Integration/ Continuous Delivery).

You can learn more about CI/CD in this starter’s blog

So, AWS CodePipeline will integrate with the Laravel GitHub repository and detect changes. It will deploy the Laravel app changes to AWS Elastic Beanstalk in the last article.  Nevertheless, you can also host a sample application from scratch. The walkthrough is equally valid for any scenario.

Deploy PHP App to Elastic Beanstalk using CodePipeline
AWS CodePipeline

AWS CodePipleline can integrate with third-party servers like GitHub and Bitbucket, as well as other AWS services, including S3, ECR, and CodeCommit. It offers one active pipeline per month free of cost. AWS charges $1 monthly for every active pipeline, excluding the free tier. Learn more about the price plan here.

Alternative to CodePipeline? GitHub Actions?

CodePipeline is a continuous delivery pipeline from Amazon. GitHub also has a CI/CD platform called GitHub Actions. It is a promising feature from GitHub that comes with many built-in actions from the GitHub marketplace and helps you define an end-to-end deployment and integration pipeline.

Here’s a comparison between CodePipeline and GitHub Actions.

CodePipelineGitHub Actions
Supports GitHub, AWS CodeCommit, BitBucket, and Amazon S3Supports GitHub only.
The free tier offers one pipeline with 50 minutes per monthFree for public repositories: 2000 build minutes per month.
It works best with AWS; in theory, it could deploy to other cloud platforms.Works with any environment, including AWS.
IAM role defined. No need to worry about user credentials.Store IAM user secrets in repository secrets.
Comes with pre-built integrations provided by AWS.GitHub marketplace has thousands of integrations available that you can use as actions in your CI/CD. 
Pipeline as Code possible with CloudFormation or Terraform.Uses Pipeline as Code – a .yml file.
AWS CodePipeline vs. GitHub Actions

Interested in GitHub Actions? Learn how to deploy Laravel PHP on AWS Elastic Beanstalk using GitHub Actions at FuelingPHP.

Deploy Our Laravel PHP App from Github to AWS Beanstalk using CodePipeline

1. Click Create Pipeline in the CodePipeline dashboard.

AWS CodePipeline Dashboard
AWS CodePipeline Dashboard

2. Provide a Pipeline name, leaving the rest of the settings as such, and click Next.

Deploy PHP App to Elastic Beanstalk using CodePipeline
CodePipeline Settings

3. In the Source Provider drop-down, select GitHub (version 2) and click Connect to GitHub.

Connect to GitHub
Connect to GitHub

4. In the new window, provide a Connection name and click Connect to GitHub.

Deploy PHP App to Elastic Beanstalk using CodePipeline
Create Connection

5. Next, click Install a new app.

Install a new app
Install a new app

6. On the GitHub auth window, click on your GitHub profile.

Deploy PHP App to Elastic Beanstalk using CodePipeline
GitHub Auth

7. Select the correct repository, then click Install.

Select Repository
Select Repository

8. Back to AWS, select the correct repository and branch, then click Next.

Deploy PHP App to Elastic Beanstalk using CodePipeline
CodePipeline Repository

9. Click on Skip Build Stage.

Build Stage
Build Stage

10. Select Elastic Beanstalk as the deploy provider in the deployment stage. Choose the application name and environment. Then click Next.

Deploy PHP App to Elastic Beanstalk using CodePipeline
Deploy Stage

11. Next, review the details of the pipeline and then click Create Pipeline. The creation will take a while.

12. The pipeline will be created and will start deploying from the source (GitHub) to the destination (Elastic Beanstalk).

Deploying
Deploying

Congratulations! We have created a pipeline to auto-deploy from GitHub to Elastic Beanstalk. Let’s put it to the test right away.

Commit and Push to Github to Deploy PHP App to Elastic Beanstalk using CodePipeline

Open your Laravel project locally and run the following command in the local main branch.

git pull origin main

Make a small change – it could be as simple as adding a comment. Afterward, run the following two commands

git commit -am “your commit message”

git push origin main

Switch to the CodePipeline in the AWS, and you’ll see that deployment has been initiated.

Deploy PHP App to Elastic Beanstalk using CodePipeline
Deploying

Voila! We have successfully tested the pipeline, and it has worked. So, it is time for a goodbye until next time  🙂

Conclusion

This article demonstrates how to deploy a PHP Laravel app in Github to AWS Elastic Beanstalk using CodePipeline. AWS CodePipeline is a CI/CD platform from Amazon that helps you readily set up a pipeline from your code repository to Elastic Beanstalk. The article overviews CodePipeline and also discusses an alternative – GitHub Actions.

 The last two sections include the bulk of the article as they explore how to create a pipeline and put it to the test by committing to the source repository in GitHub. Hopefully, you’ve enjoyed the article. Stay tuned for more at FuelingPHP.

Want to learn more about PHP?

We have many fun articles related to PHP. You can explore these to learn more about PHP.


© 2022 Confident.Systems