top of page
Writer's pictureSoham Pingat

CI/CD pipeline using AWS CodePipeline to automate source code repository, code build & deployment

Updated: Nov 30, 2021

Create CI/CD pipeline using AWS CodePipeline to automate source code repository, code build & code deployment


Tutorial Objectives:

1. Learn to create CodeCommit repository for the application source code.

2. Learn to configure CodeBuild to build the application source code. 3. Learn to automate software release process by continuous delivery pipeline with AWS CodePipeline.

Step 1:

Set your region as US East (Ohio) us-east-2

Log into your AWS management console and navigate to Identity and Access Management (IAM)

From the left navigation pane click on Users.

Click on Add users


Name this user as Developer1

Tick the checkbox for both Access Key – Programmatic access and Password – AWS Management Console access

Give this account a secure custom password and make sure the checkbox for Require Password reset is unchecked. Afterwards, click on Next: Permissions

Click on the radio button to Attach existing policies directly.

Attach the following policies to your user

· IAMFullAccess

· AWSCodeCommitFullAccess

· AWSCodeBuildAdminAccess

· AWSCodePipeline_FullAcess

· AdministratorAccess-AWSElasticBeanstalk

· AWSCloud9Administrator

Click on Next: Tags

There’s no need for any tags for now, Click on Next: Review

Verify User Details and click on Create User

Log out of our AWS Management console from your current user and log back in as Developer1. In the end, download the user credential.csv file.


Step 2: Download the CI_CD_With_CB.zip file from here

Right-click on this zip file and click on Extract All…

Click on Extract


Step 3: Open AWS Elastic Beanstalk

Click on Create Application

Name your application as DevOpsGettingStarted

In Platform open the dropdown and select NodeJs as your platform

Inside the Application code click on Upload your code

Under Source Code Origin make sure local file is selected and then click on Choose File

Select the Version 1.zip file from your previously extracted CodeCommit folder

Finally, click on Create Application and Version 1 of your NodeJs web application will be hosted

Wait for a few minutes while Elastic Beanstalk is hosting your web application.

Once the application is up and running form the left navigation pane click on Environments and click on the URL given for your Web application environment.

And as you can see our Web Application Version 1 is now hosted on the internet.


Step 4: Open your AWS management console in another tab and navigate to AWS CodeCommit.Click on Create repository

Give an appropriate name to your repository. For now, we’ll call it NodeJs_WebApplication_Repo

Once our repository is created scroll down and click on the Copy button to copy the git command to clone our repository.


Step 5: Now, open your AWS management console in another new tab and navigate to Cloud9.

Click on Create environment

Give a name to your Environment, We’ll call it CodeCommit_Playground

Once the environment is up and running paste the previously copied git clone command in the terminal.

For remote accessing the CodeCommit bucket we need to provide our user’s http credentials to the AWS via the terminal. For that, run the following commands.

cd NodeJs_WebApplication_Repo/

git config --global credential.helper '!aws codecommit credential-helper $@'

git config --global credential.UseHttpPath true

These commands will provide your HTTP credentials to access the bucket.

Now, we need to specify the user who is going to commit the changes in our CodeCommit repository.

For that, we need to execute the following commands to specify the user.

git config --global user.name "<Your Name>"

git config --global user.email <your.email@email.com>

Now, In the left navigation pane, where you can see the folder structure of your environment click on your repository name. For now, it is NodeJs_WebApplication_Repo.

This will ensure that when we upload our Version 2 files, they will be uploaded in the folder of the repository.

From the Welcome tab click on Upload Files…


When the Upload Files pop-up appears make sure the path specifies Upload to folder: /NodeJs_WebApplication_Repo and click on Select Files to upload the version 2 files of our Web Application

From the extracted CodeCommit folder, Go into the Version 2 folder and select the following files to upload: app.js index.html package.json

Click on Open and these files will be uploaded in our repository folder.


As you can see in the left folder structure pane, all our files are uploaded inside our NodeJs_WebApplication_Repo folder.

Run the following commands to push these files to our CodeCommit repository.

git add .
git commit -m "Version 2 of the Web Application"
git push

git add . : This command will add all the uploaded files to the staging area to commit the change.

git commit -m “Version 2 of the Web Application” : This command will commit our new changes with the message “Version 2 of the Web Application”.

git push : This command will push all these changes to the master branch of our CodeCommit repository.

Now, go back to your CodeCommit tab and refresh the page. And you’ll be able to see the new Version 2 files are uploaded inside the CodeCommit repository


Step 6: Open AWS management console in a new tab and navigate to AWS CodeBuild.

Click on Create Build Project

Project Name: Build-DevOpsGettingStarted

Under Source:

· Open the Dropdown and select AWS CodeCommit as a source.

· For Repository open the dropdown and select NodeJs_WebApplication_Repo.

· Let the reference tag be Branch

· Select Master Branch as your branch.

Under Environment:

· Let it be Managed Image

· For operating system select Amazon Linux 2

· Runtimes : Standard

· Image : aws/codebuild/amazonlinux2-x86_64-standard:3.0

· Visually confirm that Always use the latest image for this runtime version" is selected for "Image version.

· Visually confirm that Linux is selected for Environment type

· Visually confirm that New service role is selected.


For Buildspec:

Click on the Radio button for Insert build Commands and click on Switch to editor

Replace the Buildspec in the editor with the code below

version: 0.2
phases:
    build:
        commands:
            - npm i --save
artifacts:
    files:
        - '**/*'

This code is to install the required runtime libraries from Package.json which we uploaded previously to host and run our web application.

Leave the rest of the settings as it is and scroll down to click on Create Build Project.


Step 7: Open AWS Management console in a new tab and open AWS CodePipeline.

Click on create Create Pipeline

· Give an appropriate name to your pipeline. For now, We’ll call it Pipeline-DevOpsGettingStarted

· Visually confirm that "New service role" is selected

Under Source

· Source Provider : AWS CodeCommit

· Repository Name : NodeJs_WebApplication_Repo

· Branch Name : Master

· Visually Confirm Amazon CloudWatch Events is selected for Change Detection option.

· For Output artifact format, select Full Clone

Click on Next

Under Build:

· Build Provider: AWS CodeBuild

· Region: Your default region. For now, it is Ohio

· Project Name: Build-DevOpsGettingStarted

· Visually confirm Single Build is selected for Build Type

Click on Next to continue to the Deployment stage.

Under Deployment:

· Deploy Provider: AWS Elastic Beanstalk

· Region: Your default region. For now, it is Ohio

· Application Name: DevOpsGettingStarted

· Environment Name: DevOpsGettingStarted-env

Click on Next and Create the Pipeline.

Once the Pipeline is created you can see 3 Modules Source Build Deploy

Are under execution.

Once the Deploy phase displays “Succeeded”, Go to Elastic Beanstalk and again click on the hosting URL.

And you can see the Version 2 of our Web Application is now hosted.


Step 8: Now to test the continuous delivery of our tutorial we’re going to commit a Version 3 of our Web Application to the CodeCommit repository.

Go back to Cloud9 IDE tab and click on your Repository Folder from the Left Folder Structure pane (as instructed in Step 5)

Click on Upload Files. Make sure the Upload to folder path displays /NodeJs_WebApplication_Repo and click on Select files.


Open the Version 3 folder from the extracted CodeCommit folder and select the index.html file. Click on Open

The Cloud9 will give a warning about a file with similar name is already existing and will confirm if you want to overwrite the existing index.html file. Click on Overwrite


Again run the git commands to push this change to the CodeCommit repository

git add .
git commit -m "Version 3 of the Web Application"
git push

Now, Navigate to CodePipeline and observe all 3 stages Source, Build, and Deploy in progress and succeed for the new change we made in our repository.

Once the Deploy stage displays Succeeded go to Elastic Beanstalk and click on the hosting URL.


This is how we have successfully created a continuous delivery pipeline using AWS CodeCommit.

Note: If you no longer need the resources, Delete the following resources:

· CodeCommit Bucket

· Cloud9 Environment

· CodeBuild Build

· CodePipeline Pipeline

· Developer1 IAM user (you’ll need to login back again as admin to delete this user)


Was this document helpful? How can we make this document better? Please provide your insights. You can download PDF version for reference.

We provide best AWS trainings from Pune, India.

For aws certification contact us now.


367 views6 comments

6 Comments


Aditya Joshi
Aditya Joshi
Oct 03, 2023

Good Blog

Like

Prajval Suryaji
Prajval Suryaji
Sep 28, 2023

nice

Like

Parth Rewoo
Parth Rewoo
Aug 19, 2022

Very informative tutorial explaining code repository, code build & deployment.

Like

Great tutorial. Nicely explained all the steps and their functions as well. Helped me to understand the automated process for code repository, code build & deployment.

Like

Through this blog we understand how we can automate source code repository, code build & deployment.

Like
bottom of page