Implementing a CI/CD pipeline on AWS - Part 3 πŸš€ ☁

Β·

4 min read

Implementing a CI/CD pipeline on AWS - Part 3 πŸš€ ☁

In a journey of making a CI/CD pipeline on AWS with these tools, we have done AWS CodeCommit & CodeBuild.

Let's learn these tools/services also:

  • CodeDeploy

  • CodePipeline

  • S3

What is CodeDeploy?

  • AWS CodeDeploy is a deployment service that automates application deployments to Amazon EC2 instances, on-premises instances, serverless Lambda functions, or Amazon ECS services.

CodeDeploy can deploy application content that runs on a server and is stored in Amazon S3 buckets, GitHub repositories, or Bitbucket repositories. CodeDeploy can also deploy a serverless Lambda function. You do not need to make changes to your existing code before you can use CodeDeploy.

Task-01

  • Read about the "Appspec.yaml" file for CodeDeploy.

    The application specification (AppSpec) file is a YAML-formatted file used by AWS CodeDeploy to manage the deployment of your application to Amazon EC2 instances or on-premises servers. It provides the necessary instructions for CodeDeploy to know how to deploy your application, including how to stop and start services, where to copy files, and how to run scripts.

    The AppSpec file can include the following sections:

    • version: The version of the AppSpec file schema being used.

    • os: The operating system of the deployment target.

    • files: Specifies the files to be deployed and their destination paths on the deployment target.

    • hooks: Specifies the lifecycle event hooks and the scripts to be run during each event. The available events are "BeforeInstall", "AfterInstall", "ApplicationStart", and "ApplicationStop".

  • Deploy index.html file on EC2 machine using nginx.

    We did the CodeCommit and CodeBuild steps and if you haven't read it, please check out the previous blog.

    Let's start the CodeDeploy part.

    Step 1: First we create an application for CodeDeploy.

    To enable communication between CodeDeploy and EC2, we must first set up a service role for the deployment group.

    and also create an instance.

    After completing these, we create a deployment group and provide its details as below.

  • You have to set up a CodeDeploy agent to deploy code on EC2.

    To deploy your app to EC2, CodeDeploy requires an agent that can deploy the code on your EC2 instance.

    To set it up, create a shell script with the following contents and run it on an 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
    

    Thus we set up the CodeDeploy agent manually in our instance successfully.

Task-02

  • Add the appspec.yaml file to CodeCommit Repository and complete the deployment process.

    We also need the appspec file to deploy the code, in addition to the CodeDeploy agent. To do this, open the VS code editor on your local system, and add the appspec.yml file to the demo_app1 folder, as shown below.

    Then add and commit it via git commands.

    These committed files are then pushed back to the CodeCommit repository.

    CodeCommit Repo will be updated like this.

    Change are done in the buildspec file.

    We need to specify the artifacts in build projects.

    The artifacts upload location is now visible on the build project details.

    Start build again to update these changes.

    Copy the S3 URI by selecting the S3 bucket object.

    Now create deployment & paste here S3 URI in the revision location.

    Here we can see deployment is in progress and events are pending.

    This happens because code-deploy has permission to access EC2 due to the service role but EC2 doesn't have any role policy to retrieve the data from S3 to CodeDeploy. So we create a role policy.

    Attach it to the EC2 instance.

    Then restart the service.

    Finally, the pending deployment status is now succeeded.

    If you paste the public IP into the address bar, it will display this result

    That's all we understand the CodeDeploy.

Hope it will help you as well.

Reference link: click here on the video for more details.


Thanks for your valuable time and reading the articles.

Keep hustling, motivating and learning.

Peace out!!

Β