Project: AWS DevOps Deployment

Harshit SahuHarshit Sahu
6 min read

I built a complete Continuous Integration and Continuous Deployment (CI/CD) pipeline using various AWS services in this project. The goal was to automate deploying an application from a GitHub repository to an EC2 instance using AWS CodeCommit, CodeBuild, S3, CodeDeploy, and CodePipeline.

The project begins by cloning the code from a GitHub repository and setting up AWS CodeCommit as the version control system. The code is then pushed to CodeCommit via the AWS CLI. Next, AWS CodeBuild is configured to build the application, and the compiled code is stored in an S3 bucket. AWS CodeDeploy is used to automate the deployment process by pulling the code from S3 and deploying it to an Ubuntu EC2 instance.

The EC2 instance is configured with the AWS CodeDeploy Agent to manage deployments. A deployment group is created in CodeDeploy to manage the EC2 instance and deploy the application. Finally, AWS CodePipeline is set up to automate the entire process, from code commits in CodeCommit to deployment in EC2, providing a fully automated DevOps workflow.

STEPS -

  1. Clone the code from GitHub using the following command:

     git clone https://github.com/harshitsahu2311/AWS-DevOps-Project
    
  2. Set up a CodeCommit repository in AWS.

  3. Push the cloned code to AWS CodeCommit using the AWS CLI.

  4. Configure AWS CodeBuild to compile and build the code.

  5. Create an S3 bucket to store the built code.

  6. Set up AWS CodeDeploy.

  7. Launch an EC2 instance where the application will be deployed.

  8. Install and configure the AWS CodeDeploy Agent on the Ubuntu EC2 instance.

  9. Create a Deployment group in CodeDeploy and deploy the application to the EC2 instance.

  10. Set up an AWS CodePipeline to automate the entire process from CodeCommit to deployment.

Let’s Start the Process -

Step 1 → Clone the Repository

git clone https://github.com/harshitsahu2311/AWS-DevOps-Project

above command will pull the code from GitHub to your Local Machine.

Step 2 → Setup CodeCommit Repository

  1. Go to the AWS console. Click on Services, search for codecommit and create a project repository.

  2. To use aws code commit with aws cli, you must first create an IAM user with aws code commit permissions. Search IAM in Services and click on create user

  3. Click Next and Set Permissions for the user

  4. Click Next and review the user and then create a user.

  5. Click on username (harshit-aws) → Security Credentials → scroll down for HTTPS Git credentials for AWS CodeCommit → Click on generate credentials and download csv.credentials file.

Step 3 - Push the cloned code to AWS CodeCommit

  1. Again, go to codecommit service → project repository → Clone URL → Copy the path of the repository. Open the Terminal and run

     git clone <Clone URL>
    
  2. You will be asked for your login and password. Give it to the code commit credentials you generated.

  3. Now you have to push all the cloned code of the project repository to your codecommit repository using commands.

     cd project
     git clone https://github.com/harshitsahu2311/AWS-DevOps-Project
     git add .
     git commit -m "aws devops"
     git push -u origin master
    

Step 4 - Configure AWS CodeBuild to compile and build the code

  1. Navigate to the AWS interface and search for AWS code build.

  2. Click on Create Project option.

  3. Give your project the name "project-app-build".

  4. On the repo option Click on Project and Branch to Master.

  5. For OS choose Ubuntu, Runtime to standard, and image to the latest version.

  6. BuildSpec:- A buildspec is a collection of build commands and related settings, in YAML format, that CodeBuild uses to run a build. You can include a buildspec as part of the source code or you can define a buildspec when you create a build project. S

  7. o “click on use a buildsepc file” option

  8. Click on the Create Build project option and keep everything as it is.

  9. Now Click on start Build option.

  10. Now, you need to store this code in S3, which is then used by code deploy for deployment.

Step 5 - Create an S3 bucket to store the built code

  1. Go to the console, search for S3, and create a bucket. (bucket name should be unique globally)

  2. Navigate to Code Build and select Edit → Artifact → Change no artifact to s3 and select a bucket created in the preceding stage.

  3. Select the zip file for storing.

  4. Click on Update artifacts and rebuild the project with this extra step of artifacts.

  5. After building, you'll see that your code is stored in an S3 bucket.

Step 6 - Set up application for CodeDeploy

  1. Go to console and search for aws code deploy and click on Create Application option

Step 7 - Launch an EC2 instance where the application will be deployed

  1. Go to EC2 and launch an instance on Ubuntu 22.04 AMI.

    Note: Choose this AMI only.

  2. Create an AWS ec2-service-role with the necessary permissions to communicate with code deployment.

    Trust Entity Type: AWS Service

    Service or use case: EC2

  3. Attach that role to your EC2 → Actions → Security → Modify IAM role

    create a aws code_deploy_service_role

    this role needs to be attached on codedeploy to allow it to make contact with EC2 and S3.

Step 8 - Setup and configure the AWS CodeDeploy Agent on the Ubuntu EC2 instance

  1. To deploy your project to EC2, CodeDeploy requires an agent to actually deploy the code on your EC2.

    So let us set it up.
    Create a shell script with the following contents and run it.

  2. Connect to your EC2 and execute the following commands.

sudo su
apt update 
vim agent.sh
  1. Copy and paste the code below into your file.
#!/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
  1. Don’t forget to change the region “us-east-1” as per your AWS Region.

  2. To Run this file, use the command → bash agent.sh

Step 9 - Create a Deployment group in CodeDeploy and deploy the application to the EC2 instance

  1. Go to your codedeploy and create a deployment group

  2. Choose In-place in Deployment type

  3. Select ec2 instances → Name → Choose your EC2 in which codedeploy agent is running

  4. Untick on the enable load balancing option and never on install aws code deploy agent and click on Create Deployment option you will see the following page

  5. Click on Create deployment

  6. Select the above options and paste the path of your build code that is stored in S3 just go to S3 choose folder and copy S3 URL.

  7. Click on Create Deployment after a few seconds.

Now go to your EC2 and click on your public IP you will see your application is running.

Congrats!!! Your deployment is Done

Step 10 - Set up an AWS CodePipeline to automate the entire process from CodeCommit to deployment.

  1. Navigate to codepipeline from services and create a pipeline

  2. Add Source Stage

  3. Add Build Stage

  4. Add Deploy Stage

  5. Review the pipeline and start it

  6. To check if it triggers or not whenever I commit some changes in the file and now the result is the following image.

We finished this project and hope you like it. See you on the next project.

0
Subscribe to my newsletter

Read articles from Harshit Sahu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Harshit Sahu
Harshit Sahu

Enthusiastic about DevOps tools like Docker, Kubernetes, Maven, Nagios, Chef, and Ansible and currently learning and gaining experience by doing some hands-on projects on these tools. Also, started learning about AWS and GCP (Cloud Computing Platforms).