Creating a Continuous Delivery Pipeline in AWS (Hands-On)
Tags: devops, continuousdelivery, aws, codepipeline
Published: 2024-02-20 17:09:25


This week, I decided to dive deeper into the world of DevOps and SRE. As I was exploring various resources, I stumbled upon a hands-on AWS module focusing on Creating Continuous Delivery Pipelines. Recognizing the importance of this topic and its relevance in modern software development, I felt compelled to delve into it as a starting point for my journey.

For those unfamiliar with the concept, continuous deployment is a method that enables automatic deployment of code revisions to a production environment, eliminating the need for manual approval from developers. This automation streamlines the software release process and enhances efficiency.

Throughout this tutorial, I gained valuable insights into the creation of a continuous delivery pipeline. I learned how to set up a system that automatically deploys a sample web application whenever changes are made to the source code. This hands-on experience provided a practical understanding of the mechanisms behind continuous delivery and its significance in modern software development practices.

_Everything done in this tutorial is Free Tier eligible._

**Prerequisite**
- An AWS account
- A GitHub account
- Git installed on your computer
 
**What I learned from this:**

1. Set up a **GitHub repository** for the application 
   code

2. Create an **AWS Elastic Beanstalk** environment 
   to deploy the application

3. Configure **AWS CodeBuild** to build the source 
   code from **GitHub**

4. Use **AWS CodePipeline** to set up the continuous 
   delivery pipeline with source, build, and deploy 
   stages

Visual representation of End Architecture:

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

## Task 1: Set Up Git Repo
**Step 1:** Fork a GitHub repository to create a new one
 - navigate to **GitHub** 
 - open the [aws-elastic-beanstalk-express-js-sample](https://github.com/aws-samples/aws-elastic-beanstalk-express-js-sample) **repo**
 - Choose the white **Fork** button on the top right corner of the screen and choose **Create a fork**

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

**Step 2:** Store code and metadata in GitHub Repository(**Push Code**)
- Go to the repository, click on the green Code button and copy the url of the repo to clone it.

- Open the terminal or Bash platform of your choice and enter the following command and paste the URL

`git clone https://github.com/YOUR-USERNAME/aws-elastic-beanstalk-express-js-sample`
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/46zft9qqon5cs8os67pk.PNG)

- In the new folder there is a file named app.js. Open app.js Change the message in line 5 to say something other than "Hello World!" and save the file.

- Go to the folder created with the name aws-elastic-beanstalk-express-js-sample/ and Commit the change with the following commands:

`git add app.js
git commit -m "change message"
`
- Push the changes by entering the following command:

`git push`

**Step 3:** Test the changes
- On Github under Repositories, select the one named aws-elastic-beanstalk-express-js-sample

- Choose the app.js file. The contents of the file, including your change, should be displayed

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

## Task 2: Deploy a Web App

**Step 1:** Configure and create an AWS Elastic Beanstalk environment

- open the AWS Elastic Beanstalk console and click on Create Application button

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f7xy0z9qov2d4eahjmg2.PNG)
- under the heading Application name, enter _DevOpsGettingStarted_

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

- Select Node.js from the Platform dropdown menu.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9l6emgg20wsz6h2yicrg.PNG)
- Ensure that sample application is selected.

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

 - Click create application button

_Once you see the green checkmark, you have successfully created an AWS Elastic Beanstalk application and deployed it to an environment._

**Step 2:** Test the web app 
- To test your sample web app, select the link under the name of your environment.

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

_Once the test has completed, a new browser tab should open with a webpage congratulating you!_

We will use this environment and our continuous delivery pipeline to deploy the Hello World! web app we created in the previous module.


## Task 3:Create Build Project

**Step 1:** Create a build project with AWS CodeBuild

- Open the AWS CodeBuild console and choose the 
  create project button.

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

- In the Project name field, enter Build-DevOpsGettingStarted.

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

- Select GitHub from the Source provider dropdown menu, Connect using OAuth and Choose the white Connect to GitHub button.

- Choose the green Authorize aws-codesuite button and Enter your GitHub password.

- Choose the Confirm button and Select Repository in my GitHub account.

- Enter aws-elastic-beanstalk-express-js-sample in the search field.

- Select the repo you forked 

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

- Confirm that Managed Image is selected.

- Select Amazon Linux 2 from the Operating system dropdown menu.

- Select Standard from the Runtime(s) dropdown menu.

- Select aws/codebuild/amazonlinux2-x86_64-standard:3.0 from the Image dropdown menu.

- Confirm that Always use the latest image for this runtime version is selected for Image version.

- Confirm that Linux is selected for Environment type.

- Confirm that New service role is selected.

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


**Step 2:** Create a Buildspec file for the project

- Select Insert build commands.
- Choose Switch to editor.
- Replace the Buildspec in the editor with the code 
  below:
`version: 0.2
phases:
    build:
        commands:
            - npm i --save
artifacts:
    files:
        - '**/*'`

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

_Create build project button. You should now see a dashboard for your project._

**Step 3:** Test the CodeBuild Project 
- Choose the Start build button to configure the build process

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

- Wait for the build to complete. After a couple minutes, a green checkmark and a Succeeded message confirming the build worked.

_We have created a build project on AWS CodeBuild to run the build process of the Hello World! web app from our GitHub repository. We will be using this build project as the build step in our continuous delivery pipeline._


## Task 4: Create Continuous Delivery Pipeline

**Step 1:** Create a new Pipeline
- Open the AWS CodePipeline console and click the Create pipeline button

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

- In the Pipeline name field, enter Pipeline-DevOpsGettingStarted.

- Confirm that New service role is selected.
- Choose the Next button.

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

**Step 2:** Configure a source stage using your GitHub repo

- Select GitHub version 1 from the Source provider dropdown menu.

- Choose Connect to GitHub button. 

- Choose Authorize aws-codesuite button.

- From the Repository dropdown, select the repo you created. 


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

- Select main from the branch dropdown menu.
- Confirm that GitHub webhooks is selected.
- Choose the Next button.

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

**Step 3:** Configure the build stage
- From the Build provider dropdown menu, select AWS 
  CodeBuild.

- Under Region confirm that the US West (Oregon) 
  Region is selected.

- Select Build-DevOpsGettingStarted under Project 
  name.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5x701776pxckci8lztcx.PNG)
- Choose the Next button.

**Step 4:** Configure Deploy stage

- Select AWS Elastic Beanstalk from the Deploy 
  provider dropdown menu.

- Under Region, confirm that the US West (Oregon) 
  Region is selected.

- Select the field under Application name and 
  confirm you can see the app DevOpsGettingStarted 

- Select DevOpsGettingStarted-env from the 
  Environment name textbox.

- Choose the Next button. 

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

_You will now see a page where you can review the pipeline configuration._

- Choose the Create pipeline button.

**Step 5:** Watch the pipeline execution

- Once the Deploy stage has switched to green and it says Succeeded, choose AWS Elastic Beanstalk. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3s75uvhcpbi5xxtl3zt6.PNG)

- A new tab listing your AWS Elastic Beanstalk environments will open.

- Select the URL in the Devopsgettingstarted-env row. 

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

- You should see a webpage with a white background and the text you included in your GitHub commit

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

We have created a continuous delivery pipeline on AWS CodePipeline with three stages: source, build, and deploy. 
The source code from the GitHub repo  is part of the source stage. That source code is then built by AWS CodeBuild in the build stage.
Finally, the built code is deployed to the AWS Elastic Beanstalk environment.

## Task 5:Finalize Pipeline and Test

**Step 1:** Create Review Stage in Pipeline

- Open the AWS CodePipeline console

- Select Pipeline-DevOpsGettingStarted

- Choose the Edit button and click the Add stage button between the Build and Deploy stages.


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

- In the Stage name field, enter Review.
- Choose the Add stage button.
- In the Review stage, choose the Add action group 
  button.


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

- Under Action name, enter Manual_Review.
- From the Action provider dropdown, select Manual 
  approval.
- Confirm that the optional fields have been left 
  blank.
Choose the  Done button.
Choose the  Save button at the top of the page.
Choose the orange Save button to confirm the changes. You will now see your pipeline with four stages: Source, Build, Review, and Deploy.

##Conclusion:

In summary, this tutorial has equipped you with the knowledge to set up a continuous delivery pipeline on AWS using services like CodePipeline, Elastic Beanstalk, and CodeBuild. By automating the deployment process for a sample web application, you've learned the benefits of continuous delivery, including faster release cycles and improved efficiency.

As you delve deeper into DevOps and SRE practices, remember to regularly review and optimize your pipelines to meet evolving organizational needs. By embracing automation and continuous improvement, you'll be better positioned to respond to customer feedback and market demands effectively.

I hope this tutorial has empowered you to embark on your continuous delivery journey with confidence. Happy coding and deploying!