CI/CD Pipeline using AWS CodeCommit, Codebuild, CodeDeploy and CodePipeline

Arjun Menon - Jan 26 - - Dev Community

Introduction

Welcome to this comprehensive guide where we'll walk through the process of creating a CI/CD pipeline for deploying a Todo List web app using AWS DevOps services. This tutorial is designed to be beginner-friendly, providing detailed steps for setting up CodeCommit, CodeBuild, CodeDeploy, and CodePipeline. We'll be installing Nginx and deploying a Todo List web app on an EC2 instance.

Code

All the required code is available at this repository.

Step 1: Setting Up CodeCommit

  1. Create a CodeCommit Repository:

  2. Create IAM User and Generate Credentials:

  3. Clone Repository Locally:

  4. Create Project Files:

git add .git commit -m "Initial commit"git push origin main
Enter fullscreen mode Exit fullscreen mode

Step 2: Configuring CodeBuild

  1. Create a Build Project:
# buildspec.yamlversion: 0.2phases: install: commands: - echo Installing Nginx... - sudo apt-get update - sudo apt-get install nginx -y build: commands: - echo Build started on `date` - cp index.html /var/www/html/ - cp styles.css /var/www/html/ - cp app.js /var/www/html/ - cp appspec.yml /var/www/html/ post_build: commands: - echo Restarting Nginx...artifacts: files: - index.html - appspec.yml - scripts/** - app.js - styles.css
Enter fullscreen mode Exit fullscreen mode
  1. Configure Artifacts:

  2. Run the Build:

Step 3: Implementing CodeDeploy

  1. Create CodeDeploy Application:

  2. Create Deployment Group:

  3. Install CodeDeploy Agent on EC2 Instance:

#!/bin/bash # This installs the CodeDeploy agent and its prerequisites on Ubuntu 22.04. sudo apt-get update sudo apt-get install ruby-full ruby-webrick wget -y cd /tmp wget https://aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com/releases/codedeploy-agent_1.3.2-1902_all.deb mkdir codedeploy-agent_1.3.2-1902_ubuntu22 dpkg-deb -R codedeploy-agent_1.3.2-1902_all.deb codedeploy-agent_1.3.2-1902_ubuntu22 sed 's/Depends:.*/Depends:ruby3.0/' -i ./codedeploy-agent_1.3.2-1902_ubuntu22/DEBIAN/control dpkg-deb -b codedeploy-agent_1.3.2-1902_ubuntu22/ sudo dpkg -i codedeploy-agent_1.3.2-1902_ubuntu22.deb systemctl list-units --type=service | grep codedeploy sudo service codedeploy-agent status
Enter fullscreen mode Exit fullscreen mode
  1. Create appspec.yaml and Scripts:

appspec.yaml:

# appspec.yamlversion: 0.0os: linuxfiles: - source: / destination: /var/www/html/hooks: AfterInstall: - location: scripts/install_nginx.sh timeout: 300 runas: root ApplicationStart: - location: scripts/start_nginx.sh timeout: 300 runas: root
Enter fullscreen mode Exit fullscreen mode

install_nginx.sh

# scripts/install_nginx.sh#!/bin/bashapt-get updateapt-get install nginx -y
Enter fullscreen mode Exit fullscreen mode

start_nginx.sh

# scripts/start_nginx.sh#!/bin/bashservice nginx start
Enter fullscreen mode Exit fullscreen mode
  1. Build and Deploy:

  2. Configure EC2 Role:

  3. Restart CodeDeploy Agent:

Step 4: Setting Up CodePipeline

  1. Configure CodePipeline:

  2. Create Pipeline:

  3. Test the Web App:

Conclusion

Congratulations! You've successfully set up a CI/CD pipeline for deploying a Todo List web app using AWS DevOps tools. This tutorial has covered each step in detail, providing you with a solid foundation for implementing similar projects in the future.

Follow me on LinkedIn.

Checkout my GitHub profile.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .