Simplifying Website Deployment with AWS CodeCommit, CodeDeploy, and CodePipeline

Simplifying Website Deployment with AWS CodeCommit, CodeDeploy, and CodePipeline

In this blog, I’ll walk you through how I automated my website deployment using AWS CodeCommit, AWS CodeDeploy, and AWS CodePipeline. The website is hosted on an NGINX server, and AWS services make the process efficient and seamless.


1. Setting Up the Repository on AWS CodeCommit

First, I created a Git repository on AWS CodeCommit and pushed my website’s files (HTML and scripts). Here's the basic structure of my project:

bashCopy code/my-website
  ├── index.html
  ├── scripts/
  │   ├── install_nginx.sh
  │   └── startnginx.sh
  ├── buildspec.yml
  └── appspec.yml
Push this complete structure to AWS CodeCommit Repository

Cloning the Repository

After creating the repository, I cloned it on my local machine using an IAM user with CodeCommitPowerUserAccess. Don't forget to set up HTTP Git credentials in IAM before cloning the repository.

Next, I created the index.html file for my website, then used basic Git commands (git status, git add, git commit, and git push) to push the code back to CodeCommit.


2. Creating the buildspec.yml for CodeBuild (CodeBuild part started so click on Codebuild and do the needful)

Next up, I created a buildspec.yml file to define the build process before deployment. This file instructs AWS CodeBuild on how to compile the project and prepare it for deployment:

version: 0.2
phases:
  install:
    commands:
      - echo "Building the application"
      - sudo apt-get update
      - sudo apt-get install nginx -y
  build:
    commands:
      - sudo cp index.html /var/www/html
artifacts:
  files:
    - '**/*'

I pushed this file to CodeCommit after creating it locally.

Artifact Creation in S3

After running the build, make sure to specify the artifact location in S3 under the Build Settings in CodeBuild. Once that’s done, your build will automatically create the artifacts in S3.
you first have to create a s3 bucket in AWS for this and then do the above.


3. Setting Up AWS CodeDeploy

Now, let's move to AWS CodeDeploy. First, create a CodeDeploy application and a deployment group that specifies where the application will run (an EC2 instance).

Instead of setting up the CodeDeploy agent manually via the console, I created an EC2 instance and then ran the following script to install the CodeDeploy agent.

bashCopy code#!/bin/bash
# This script installs the CodeDeploy agent and its prerequisites on Ubuntu 22.04.

# Step 1: Update the package list and install required packages
sudo apt-get update
sudo apt-get install ruby-full ruby-webrick wget -y

# Step 2: Download the CodeDeploy agent package
cd /tmp
wget https://aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com/releases/codedeploy-agent_1.3.2-1902_all.deb

# Step 3: Extract the downloaded Debian package
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

# Step 4: Modify the package's dependencies for compatibility with Ubuntu 22.04
sed 's/Depends:.*/Depends:ruby3.0/' -i ./codedeploy-agent_1.3.2-1902_ubuntu22/DEBIAN/control

# Step 5: Rebuild the Debian package
dpkg-deb -b codedeploy-agent_1.3.2-1902_ubuntu22/

# Step 6: Install the CodeDeploy agent
sudo dpkg -i codedeploy-agent_1.3.2-1902_ubuntu22.deb

# Step 7: Verify that the CodeDeploy agent service is installed and running
systemctl list-units --type=service | grep codedeploy

# Step 8: Check the status of the CodeDeploy agent service
sudo service codedeploy-agent status

This script installs the CodeDeploy agent on the EC2 instance and sets it up for deployment.


4. Crafting the appspec.yml for CodeDeploy

Next, I created the appspec.yml file, which defines the deployment steps for CodeDeploy. Here’s my version of the file:

version: 0.0
os: linux
files:
  - source: /
    destination: /var/www/html
hooks:
  AfterInstall:
    - location: scripts/install_nginx.sh
      timeout: 300
      runas: root
  ApplicationStart:
    - location: scripts/startnginx.sh
      timeout: 60
      runas: root

The install_nginx.sh script installs NGINX, while startnginx.sh starts the NGINX service to serve the website.

Installing NGINX


# Install NGINX on the EC2 instance
sudo apt-get update
sudo apt-get install nginx -y

Starting NGINX


# Start NGINX service
sudo service nginx start

Remember to create a deployment in code deploy, specify the artifacts URI from s3 to here in the revision location, and boom, the project is deployed.


5. Setting Up AWS CodePipeline

Finally, I set up AWS CodePipeline to automate the entire process:

  1. Source Stage: Pulls code from AWS CodeCommit.

  2. Build Stage: Compiles the code using AWS CodeBuild.

  3. Deploy Stage: Deploys the website using AWS CodeDeploy to the EC2 instance.

This made the deployment process fully automated, saving me a lot of time and effort. Every time I push new code to the repository, the build and deployment process happens automatically.


Conclusion

By using AWS CodeCommit, AWS CodeDeploy, and AWS CodePipeline, I’ve created a powerful CI/CD pipeline that automates my entire website deployment. This setup allows me to focus more on building features rather than manually managing deployments.

Let me know if you have any questions or need help setting this up! 😊