CI/CD Pipeline Hands-on | AWS Code Pipeline, Elastic Beanstalk, GitHub
Tags: node, aws, github, elastic
Published: 2022-08-21 11:31:33

## Introduction
In this article we are going to see how to use AWS code pipeline to set up a a very simple CI/CD pipeline for your developments with three products:

- AWS Code Pipeline
- Elastic Beanstalk (EB)
- GitHub

I already have written some article covering each and everything  about CI/CD, Do have a look to previous articles to have a better understanding of this one. 

1. [Why CI/CD? How was a life without CI/CD & how is it now?](https://dev.to/wardaliaqat01/why-cicd-how-was-a-life-without-cicd-how-is-it-now-46bl)
2. [Getting intimate with CI/CD and CI/CD Pipeline]()

## Contents:

- [Intro to AWS Code pipeline](#intro)
- [Architecture of Our Code Pipeline](#archi)
- [Let’s start building our Pipeline](#imple)
1. [Environment Setup](#env)
2. [Set up Pipeline](#pipeline)
- [Testing Our Code Pipeline](#testing)
- [Conclusion](#conclu)

## Intro to AWS Code pipeline <a name="intro"></a>

AWS Code Pipeline is a fully managed continuous delivery service that helps you automate your release pipelines for fast and reliable application and infrastructure updates. `Code Pipeline automates the build, test, and deploy phases of your release process every time there is a code change,` based on the release model you define. This enables you to rapidly and reliably deliver features and updates. You can easily integrate AWS Code Pipeline with `third-party services such as GitHub or with your own custom plugin`. With AWS Code Pipeline, you only pay for what you use. There are no upfront fees or long-term commitments.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dy0ob3p6eypr64uzrrxo.PNG)

## Architecture of Our Code Pipeline <a name="archi"></a>

We are going to deploy a code pipeline on AWS. Below is architecture of our pipeline

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0k628j472jk2nhq7kz16.png)

1. We will be using sample node.js application located in the GitHub Repo.
2. Setup a pipeline so, whenever we commit any changes to our code in GitHub it will trigger AWS code pipeline instance to kick-in and start deployment process
3. Finally code pipeline will deploy our application to our target Web Server which will be setup in AWS Elastic Beanstalk which essentially means our code will live on EC2 instances behind the scene. 

So, that's it very simple one stage pipeline.

- Go to GitHub and host your application there. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bod62e8347qj8qsn5yso.png)

## Let’s start building our Pipeline <a name="imple"></a>

- Login AWS console

### Environment Setup <a name="env"></a>

- Go to elastic beanstalk

> In a nutshell Elastic Beanstalk is just a way to deploy our resources to AWS. In this case it's gonna deploy resources to EC2 instances behind the scene. if this were a great prod application i may directly go to ec2 and set it up directly there just to have a little bit of more control but at the expense of little bit of control it allows us to deploy very quickly.
> You will be landed to this page. Just click on Get started/create application

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mqik1zzy8hvshfy098j5.png)

- Name your application
- Choose platform
- I’ll go with the sample code. I’m not gonna upload our code because I want our pipeline to do it for us. 
- Click on create Application

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7vb3594p71sblbmlbnnb.PNG)

- It will take several minutes to create and you can see all the logs to get to know what’s happening. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5nr581ugwjzsy0yx1hax.png)

- It will be deployed successfully after some time with `health status OK`. It has also deployed sample application into that as well. We used ec2, s3 behind this but you really don’t look at that, we can see in the logs. Elastic beanstalk take care of provisioning of those resources for us. 
- One more thing you `don’t need to install any package for your application`, if Elastic beanstalk detects any package in our application it will automatically install for us. Everything about this process is very automatic where the convenience factor comes in. 
- So, just for the test if it’s working. EB gives us a `URL` at the top to test your running application

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5dotwwpxqo9dycgsnfwe.png)

> And there we go, our first application is running and our very first deployment was successful. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ktj5hd6vqr8yzklcm15g.PNG)

## Set up Pipeline <a name="pipeline"></a>

- Next step is to go to AWS pipeline and setup a pipeline for our application.
- Go to the code pipeline from AWS management console. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tzgmcsuaf7pldkxaluh5.png)

- Now go ahead and click on create pipeline.
You will see 5 stages to complete in order to create your pipeline.  

- Step 1
Choose pipeline settings
- Step 2
Add source stage 
- Step 3
Add build stage 
- Step 4
Add deploy stage 
- Step 5
Review

> Let's explore all 5 steps.

**Step 1:**

- Just name your pipeline and leave all the settings as it is. 
- And click on next

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qbmdyccy2nojx0o34ql6.PNG)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6eriywwvjq3yj2pjvivb.PNG)

**Step 2**

- Detect option this is saying how you want to trigger your pipeline. Go with the default for better ease. Because default one is more event driven model.
 

> Version 1:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t0zctwww57ext810bgei.png)

> Version 2:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ikgnaxn8d0iub343oz50.PNG)

**Step 3**

- Build stage is optional so, we are going to skip it for this project.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2m994vdgx7yo6iz67v5l.PNG)

**Step 4**

- Deploy provider is elastic beanstalk which is our target. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/21jpykqe192m7dt2j4x5.PNG)

**Step 5**

- Then just review everything and click on create pipeline.
- Now we can see pipeline has been created and now it’s deploying to elastic beanstalk.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r5bk8jk1k1ywvy94umhk.png)

- It will take minute or two then we can verify if it’s been deployed. 
- Now go to EB by clicking the URL

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/en9yqzdybgao54gju47w.png)

- You will see your app is running in browser

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4pvctzbv13wxzhvjy8fi.png)

## Testing Our Code Pipeline <a name="testing"></a>

Now let’s do some updates in your code from GitHub and see if our pipeline detect those changes and populate them automatically to our application that is running on Elastic Beanstalk. 
You can update and commit your changes from directly GitHub or you can do it also by cloning your repo, updating your changes and them committing them locally.

## Update changes in code from GitHub:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jmbveo5piq3dka02prh6.png)

## Update changes in code locally & then commit to online repo:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fq3li9mlx3h0fmfhmsx9.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9x4xezjhy6j0l65wollc.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/an5z9n15v4xg1896wn5m.png)

- After updating your changes in code either from GitHub or Locally, when you'll go to your code pipeline you will observe that your pipeline is in action, It will detect changes and then update the source and will deploy changes to EB automatically.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/36qhhaibfa1j5d8gduqv.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/um7441c97y8fhds7e5gu.png)

- When you observe that pipeline is successfully done then if you open/refresh the URL of your application in the browser you will observe that changes have been deployed successfully.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l8fwoh7jjh4t6thyh6ia.png)

## Conclusion <a name="conclu"></a>
See, how code pipeline can make things easy for you when you are working on your development projects, now you don't have to manually write test cases after each change and update those changes manually. Code pipeline will handle all of that automatically for you. Now you just need to relax and focus on adding more new features to your application and do lots of advancements. 

**> Let me know what do you think about it or if you are already using Code Pipeline for your developments?**

## Connect with me:
[LinkedIn](https://www.linkedin.com/in/warda-liaqat/)
[Instagram](instagram.com/warda_Liaqat01)
[Twitter](https://twitter.com/CreativityWarda)