CICD in AWS using CodePipeline and EBS
Tags: aws, devops
Published: 2023-03-26 11:50:33

The Cloud has made a lot of things simpler for developers. In this thread, let's explore how easy it is to set up a CICD Pipeline in AWS using GitHub, AWS CodePipeline and Elastic Beanstalk.

## What is CI/CD?

![Image Representation of CICD](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j549rici9ase6cc050s0.png)

CI/CD is the combined practice of continuous integration (CI) with continuous delivery or continuous deployment (CD). The purpose of CI/CD is to allow developers and teams to deliver code changes more frequently and reliably.

The reference to the word *pipeline* is to explain how all of these processes are executed in a sequence, one after the other. The key to CI/CD is automation. The process can be more and more efficient if there is not a lot of manual involvement. It is crucial to have an efficient pipeline when you're dealing with large codebases in production since a lot of changes are made at different parts of an application. 

### Prerequisites
- An AWS Account (preferably Free Tier so that you are not charged for using cloud infrastructure)

## Tools Used

- GitHub
- AWS CodePipeline
- AWS Elastic Beanstalk

For this demonstration, we'll use a sample PHP application and deploy it to Elastic Beanstalk.

### Creating a Deployment Environment in Elastic Beanstalk

![Beanstalk Dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/48e7vbzvn6t5oixn193r.png)

Open up [Elastic Beanstalk](https://console.aws.amazon.com/elasticbeanstalk/home) in the AWS Console and click on **Create Application**.

![Beanstalk App Configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oirmtw2g2pxzj2wntuen.png)
Give a name to your application, choose PHP for the platform and *Sample Application* for your code type. Now, click on **Create Application**.

Wait for a minute, while Beanstalk creates a sample environment for you to deploy your application. By default, it creates an EC2 instance, a security group, an Auto Scaling group, an Amazon S3 Bucket, Amazon CloudWatch alarms and a domain name for your application.

![App Status after creation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gffkqd4uasix3m5dh5ss.png)
![Visiting the website](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3zp347qdr0y0mwnryzrx.png)

You can click on the URL to visit the application. You'll get a custom PHP welcome page by default.

### Getting your sample code

![Screenshot of the GitHub repository](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4y5y9seurtszoezjdzlv.png)

A sample application is provided in [a tutorial](https://docs.aws.amazon.com/codepipeline/latest/userguide/tutorials-simple-s3.html#s3-create-s3-bucket) for creating a simple pipeline in the AWS Documentation. You can either upload those files to your GitHub repository or simply fork my GitHub repository [here](https://github.com/sreekeshiyer/codepipeline-sample-app.git).

Since we are using GitHub as the source, we do not need the scripts folder, so you can delete it if you want. The scripts folder can come in handy if you are using Amazon S3 as the source for your application :)

### Creating a CodePipeline

![CodePipeline Dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/onvbr3zyf0u8h9su28cj.png)

Open up the  [CodePipeline](https://console.aws.amazon.com/codesuite/codepipeline/home)  Dashboard. In this step, we will create a simple pipeline where we specify our GitHub Repository for the source code and deploy the code to the Beanstalk app we just created.

- **Step 1** 

![Pipeline Setup 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ct2iuwegmxvpth9rtfz2.png)

Enter a name for your pipeline and allow AWS to create a service role for this pipeline.

- **Step 2** 

![Pipeline Setup 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/abez8a10qf80tnsmnhoq.png)
Create a Connection to Connect your pipeline to GitHub.

![Authorizing AWS Connector for GitHUb](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n8t1gylbf61hhefdjc8l.png)
Ideally, you should only allow access to the specific GitHub Repository you're currently using. 

![Adding Source Information](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d3xdn7sf5tehco1aeyib.png)


After the connection, choose the repository and the branch and go with the default CodePipeline for Output artifact format. 

- **Step 3** 
![Deploy Stage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vh27v6nl4mb9ctx9xgfu.png)

Skip the build stage (since we do not have any build command) and fill in the Beanstalk application details in the Deployment Stage and lastly, review the data and click on **Create Pipeline**.

### View Pipeline Progress

![Viewing Pipeline progress](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f1got2shi1eg8bhammlt.png)

Wait for a few minutes while the Pipeline is executed and you can see that the process was completed. You can click on **AWS Elastic Beanstalk** which takes you back to the Beanstalk application we created. Now, if you click on the URL, you can see that our application is successfully deployed.

![Visiting the App](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1vnxvkgqdjsm7y22bhvk.png)

> 
Congratulations, we just deployed our application to AWS Elastic Beanstalk via a pipeline. 

### Make Changes to re-run the pipeline

![Viewing the changes on GitHub](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eb0582o7aa4t8shfcdke.png)

Open up the pipeline repository on GitHub and make some changes in the *index.html* file using the default GitHub text editor. 

![Observing changes on CodePipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xgmr7cf4hcvyx76rztlp.png)

After making the changes, quickly switch back to the pipeline and observe the changes. You can notice that the pipeline is being re-executed on your new commit. 

![Visiting updated app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/46lu58mr68lrcrqfiqmc.png)

Once the pipeline execution is complete and successful, go back to the website.
As you can see, our changes were immediately reflected in our application and we didn't have to do anything extra for it. 

You can take this to the next level, where you deploy your Node.js or Python projects to Beanstalk, set up Tests, and run them on every new commit to your production branch. 

### Recommended Cleanup


![Deleting the Pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qygwm5ezan56ciogzu96.png)
![Deleting the Beanstalk App](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b72r50ndr1hc7db82rqr.png)

> 
It is good practice to clean up/delete/terminate all services you have used on AWS (on any Cloud really) so that you are not charged in the future. 

Firstly, delete your CodePipeline. Then, go to the Elastic Beanstalk Dashboard and delete your **Application**. This will also delete and terminate all other services used by the application. 

This was a small demonstration of CICD in AWS. I hope you liked it. If you have used AWS CodePipeline before, do share your experience. 

Thank you for reading :)