Set up a continuous deployment pipeline using AWS CodePipeline

Published on: Fri, 31 Jul, 2020

By: Amit Rai


In this tutorial, we are going to build a CI/CD pipeline for deploying a small pre-built sample application. we will use AWS Code Pipeline service to create pipeline to build test and deploy application code whenever there is any change has been committed.

 

AWS services/ frameworks used

 

  • AWS CodeCommit
  • AWS Elastic BeanStalk
  • AWs CodePipeline

Continuous deployment allows to deploy latest revisions to a production environment automatically, making the entire software release process automated.

Everything done in this tutorial is free tier eligible.

 

Creating deployment environment on AWS Elastic Beanstalk

 

We need to create a target environment having virtual servers or instances where application code will be deployed by pipeline. We will use the AWS elastic beanstalk to host the sample web application without needing to launch, configure or manage virtual servers. Elastic Beanstalk automatically provision and manage infrastruture and provide application stack.

Launch Sample PHP application -  select PHP as your platform and Single Instance as your environment type. Don't select a key pair since we are not going to login into instance. Elastic Beanstalk will create a sample environment. It will create an Amazon EC2 instance, a security group, an Auto Scaling group, an Amazon S3 bucket, Amazon CloudWatch alarms, and a domain name for your sample application.

 

continuous-delivery-pipeline-1c

 

 

Creating a repository for sample code in CodeCommit


Create a repository in AWS CodeCommit as our source repository. On the Create new repository, give repository a name and select create repository. We can retrieve the sample code from the AWS GitHub repository and we can save it to out local computer for any changes, we can upload code base to CodeCommit repository.

 

continuous-delivery-pipeline-2c3

 

In codeCommit console, select the repository and copy the https url link. Run below command to connect your local repository with CodeCommit repo.

 

$ git clone << CodeCommit https urls >>

 

Use below link to download the sample code on your local computer

https://github.com/awslabs/aws-codepipeline-s3-aws-codedeploy_linux

We can use blow git command to upload sample code on CodeCommit repository

 

$ git init
$ git add .
$ git commit -m "first commit"
$ git push

 

Sample web application code will be uploaded on CodeCommit repository

 

Creating deployment pipeline

 

We will create and configure an AWS CodePipeline with the locations of your source repository and deployment environment.

 

  • Open the AWS codePipeline console and click Create Pipeline on welcome screen. For first time user, an introduction screen will appear with option Get Started. Click Get Started to begin

 

continuous-delivery-pipeline-3

 

  • We need to give a name to pipeline in name screen and on the source screen, select the location of source repository, in our case it will be codeCommit. Choose the proper Repository Name and Branch Name, click next

 

continuous-delivery-pipeline-3c3

 

  • On build screen, we can define build stage where the code is compiled and unit tested. We are not going to use build in this tutorial, so choose No Build and click next
  • We need to give details of our target environment in deploy screen.
    • Deployment provider will be AWS Elastic Beanstalk. 
    • Application name will be our Application name. 
    • Click Next step.

Once the pipeline has been created and launched, it will detects the sample application code in our source location, bundles up the files, and then move them to the deploy stage.

 

continuous-delivery-pipeline-4a

 

To verify the deployed application, Click the URL that appears in AWS Elastic Beanstalk application console.  

 

continuous-delivery-pipeline-6e

 

AWS CodePipeline is a service for continuous deployment so revised the code and commit in our repository will be detected by CodePipeline and CodePipeline will automatically initiate deploying it via Elastic Beanstalk. 

Click application url to verify the new changes.

 

Clean up resources

 

We will delete all the resources those we have launched throughout this tutorial to avoid future charges 

a. First, we need to delete pipeline:

  • In the codePipeline view, click Edit
  • Click Delete.

b. Second, delete your Elastic Beanstalk application:

  • Visit the Elastic Beanstalk console. 
  • Click Actions.
  • Then click Terminate Environment.

 

We have successfully created an automated application release pipeline to continuously deploy changes using CodePipeline.