Automating Builds and Deployment with AWS CodeCommit, CodeBuild, and CodeDeploy

Automating Builds and Deployment with AWS CodeCommit, CodeBuild, and CodeDeploy

In this tutorial, we will walk through the process of building and deploying a web application using various AWS services. We will leverage AWS CodeCommit for version control, AWS CodeBuild for building the application, and AWS CodeDeploy for deploying the application to an EC2 instance. Additionally, we will automate the entire process using AWS CodePipeline. By the end of this tutorial, you will have a complete understanding of how to set up a CI/CD pipeline for your web application on AWS.

Prerequisites: To follow along with this tutorial, you should have the following prerequisites:

  • An AWS account with appropriate permissions to create and manage resources.

  • Basic knowledge of AWS services like EC2, IAM, CodeCommit, CodeBuild, CodeDeploy, and CodePipeline.

  • Familiarity with Git and version control concepts.

Step 1: Set up AWS CodeCommit Repository

  1. Open the AWS Management Console and navigate to AWS CodeCommit.

  2. Click on "Create repository" and provide a name for your repository.

  3. Follow the instructions to create the repository.

  4. Clone the repository to your local machine.

  5. Place your code files in the local repository.

  6. Commit and push the code to the CodeCommit repository.

Step 2: Set up AWS CodeBuild Project

  1. Open the AWS Management Console and navigate to AWS CodeBuild.

  2. Click on "Create build project" and provide a name for your build project.

  3. In the "Source provider" section, select AWS CodeCommit and choose your repository.

  4. In the "Environment" section, select "Managed image" and choose Ubuntu as the operating system.

  5. Choose "Standard" for the runtime and select the aws/standard6.0 image.

  6. Scroll down to the "Buildspec" section and select "Use a buildspec file".

  7. Just Create a buildspec.yml file with the following content:

     version: 0.2
    
     phases:
       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/
       post_build:
         commands:
           - echo Configuring NGINX
    
     artifacts:
       files:
         - '**/*'
    

    Now push this file to CodeCommit Repository and select the buildspec file in the CodeBuild

  1. Upload the buildspec.yml file to your CodeCommit repository.

  2. Configure the remaining options for your CodeBuild project and click on "Create build project"

Step 3: Set up AWS CodeDeploy Application and Deployment Group

  1. Open the AWS Management Console and navigate to AWS CodeDeploy.

  2. Click on "Create application" and provide a name for your application.

  3. Choose "EC2/on-premises" as the compute platform.

  4. Click on "Create deployment group" and provide a name for your deployment group.

  5. Attach the required service role (e.g., my-codedeploy-service-role) that has the necessary permissions.

  6. Select "In-place" as the deployment type.

  7. Now go to EC2 Service and create a basic Ec2 instance for your application

  8. Choose "EC2 instances" that you created as the environment and select the EC2 instance you want to deploy to.

  9. Select Never for Agent configuration for CodeDeployAgent and Disable load balancing if not required .

  10. Configure the remaining options for your deployment group and click on "Create deployment group".

Step 4: Install CodeDeploy Agent on EC2 Instance

  1. Connect to the EC2 instance you just created using SSH.

  2. Create a bash script (e.g., install_codedeploy_agent.sh) and paste the following code:

     #!/bin/bash 
    
     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
    
    1. Make the script executable by running chmod +x install_codedeploy_agent.sh.

    2. Run the script using ./install_codedeploy_agent.sh to install the CodeDeploy agent.

    3. Verify the installation by running sudo service codedeploy-agent status.

Step 5: Create appspec.yml File and Scripts

  1. Now create a file named "appspec.yml" in the root of your repository with the following content:

     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/start_nginx.sh
           timeout: 300
           runas: root
    
  2. Now in your CodeCommit repository or local repo, create a folder named "scripts". (Make sure to push the code to CodeCommit)

  3. Inside the "scripts" folder, create a file named "install_nginx.sh" and add the following code:

     #!/bin/bash
     sudo apt-get update
     sudo apt-get install -y nginx
    
  4. Create another file named "start_nginx.sh" in the "scripts" folder and add the following code:

#!/bin/bash
sudo service nginx start

Step 6: Create CodePipeline for Automation

  1. Open the AWS Management Console and navigate to AWS CodePipeline.

  2. Click on "Create pipeline" and provide a name for your pipeline.

  3. Choose your CodeCommit repository as the source provider.

  4. Select the appropriate branch and configure the change detection options.

  5. Choose AWS CodeBuild as the build provider and select your CodeBuild project.

  6. Select AWS CodeDeploy as the deploy provider and choose your application and deployment group.

  7. Review the pipeline settings and click on "Create pipeline".

Step 7: Test the Automation

  1. Make changes to your code in the CodeCommit repository and push the changes.

  2. The CodePipeline will automatically trigger the build and deployment process.

  3. Monitor the pipeline's progress in the AWS CodePipeline console.

  4. Once the pipeline is successfully completed, access your deployed application using the EC2 instance's public IP in a web browser.

Congratulations! You have successfully set up a CI/CD pipeline using AWS CodeCommit, CodeBuild, and CodeDeploy. The pipeline will automatically build and deploy your code whenever changes are pushed to the repository.