Building a Seamless CI/CD Pipeline on AWS: Exploring CodeCommit, CodeBuild, and CodeDeploy🚀 ☁

Building a Seamless CI/CD Pipeline on AWS: Exploring CodeCommit, CodeBuild, and CodeDeploy🚀 ☁

Day 52 : #90DaysOfDevOps Challange

Welcome to Day 52 of our DevOps journey! In our previous blog posts, we have explored the power of AWS CodeCommit for version control and AWS CodeBuild for streamlining the build process. If you missed them, catch up on our CodeCommit blog here and our CodeBuild blog here. Today, we take the next step forward as we dive into AWS CodeDeploy. This blog post will guide you through deploying applications with CodeDeploy and completing your end-to-end CI/CD pipeline on AWS. Let's get started!

Summary of Our CI/CD Pipeline on AWS

In our CI/CD pipeline journey, we began by harnessing the capabilities of CodeCommit for secure source control and collaboration. Next, we optimized our build process using CodeBuild, automating compilation, testing, and packaging. Now, with CodeDeploy, we complete the final piece of the puzzle—automated deployments to ensure fast, reliable, and scalable application releases.

Deploying Applications with AWS CodeDeploy

AWS CodeDeploy is a fully managed deployment service that automates application deployments across various compute platforms. Let's walk through the key steps involved in deploying applications with CodeDeploy:

  1. Deployment Groups: Create deployment groups within CodeDeploy to specify the instances or Auto Scaling groups where your application will be deployed. Define the target environment for your deployments.

  2. AppSpec File: Customize the AppSpec file, which contains deployment instructions, including pre- and post-deployment steps, file mappings, and more. Tailor it to suit your application's specific requirements.

  3. Deployment Configuration: Configure the deployment settings, including the deployment type (in-place or blue/green), traffic routing options, and alarms for monitoring deployment health and rollback criteria.

  4. Automated Deployments: Leverage the power of CodeDeploy to perform automated deployments based on your desired deployment strategy. Whether it's gradually rolling out new features or performing canary deployments, CodeDeploy provides flexibility and control.

To complete Task 1 and Task 2, follow the steps below:

Task 1: Deploying index.html on EC2 using Nginx and setup a CodeDeploy agent in order to deploy code on EC2 CodeDeploy

  1. Set up an Application in CodeDeploy: Create a new application in CodeDeploy and name it as "demo-cicd-app". Select the compute platform as EC2.

  2. Create a Deployment Group: Within the application, create a deployment group named "demo-app-deploy-grp". Specify the deployment type as "in-place" and assign the CodeDeploy service role.

    CodeDeploy Services Role: click here to create a CodeDeploy Service Role

  3. Configure Environment: Set up the environment configuration by launching an EC2 instance. Select the "demo-app-ec2" EC2 instance and provide a key-value pair, with the key as "name" and the value as "demo-app-ec2".

  4. Install CodeDeploy Agent: Connect to the EC2 instance and install the CodeDeploy agent. Run the following commands:

    • Run vim install.sh and paste the script that contains all the required dependencies.

        #!/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
      
    • Execute bash install.sh to install the dependencies.

    • In the project directory, add an appspec.yml file with the following contents:

    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
  1. Add Script Files: Inside the scripts folder, add two files: install-nginx.sh and start-nginx.sh. These scripts will install and start the Nginx server on the EC2 instance.

  2. Push Changes to CodeCommit Repository: Commit and push the changes made to the CodeCommit repository.

  3. Build the Demo App: Trigger the build process again to incorporate the latest changes.

Task 2: Deploying using appspec.yaml and creating a deployment

  1. Create a Deployment: Within the CodeDeploy console, create a deployment. Select the deployment group created earlier and choose the revision type as "Amazon S3". Make sure to create an Amazon S3 bucket to store the deployment files.

    To create an Amazon S3 bucket (console)

    1. Open the Amazon S3 console at https://console.aws.amazon.com/s3/.

    2. In the Amazon S3 console, choose Create bucket.

    3. In the Bucket name box, type a name for the bucket.

    4. In the Region list, choose the target region, and then choose Create.

Give permissions to the Amazon S3 bucket and your AWS account

You must have permissions to upload to the Amazon S3 bucket. You can specify these permissions through an Amazon S3 bucket policy. For example, in the following Amazon S3 bucket policy, using the wildcard character (*) allows AWS account 111122223333 to upload files to any directory in the Amazon S3 bucket named codedeploydemobucket:

    {
        "Statement": [
            {
                "Action": [
                    "s3:PutObject"
                ],
                "Effect": "Allow",
                "Resource": "arn:aws:s3:::codedeploydemobucket/*",
                "Principal": {
                    "AWS": [
                        "111122223333"
                    ]
                }
            }
        ]
    }
  1. Provide S3 URL: Paste the URL of the S3 bucket containing the deployment files and initiate the deployment.

  2. Grant EC2 Permissions: Assign the "ec2-code-deploy" IAM role to the EC2 instance to grant necessary permissions. Modify the security group to allow inbound traffic on port 80.

  3. Update IAM Role: Connect to the EC2 instance and run the command sudo service codedeploy-agent restart to restart the CodeDeploy agent.

    To create a service role for CodeDeploy

  4. Check Deployment Status: The deployment status should now show as "deployed" upon successful completion.

By following these steps, you will successfully deploy the index.html file on an EC2 instance using AWS CodeDeploy, creating a seamless CI/CD pipeline.

Conclusion

By incorporating AWS CodeDeploy into our CI/CD pipeline, we have achieved a seamless and automated end-to-end software delivery process on AWS. From source control with CodeCommit to streamlined builds with CodeBuild and automated deployments with CodeDeploy, we have established a foundation for fast, efficient, and reliable application releases.

Stay tuned for our upcoming blog posts, where we will dive deeper into advanced DevOps practices and explore additional tools and services to further enhance our CI/CD pipeline.

Unlock the full potential of your CI/CD pipeline with AWS CodeDeploy! Read my previous blog posts on CodeBuild and CodeCommit to complete the journey. #DevOps #CI/CDPipeline #AWSCodeDeploy

Did you find this article valuable?

Support Dhananjay Kulkarni by becoming a sponsor. Any amount is appreciated!