CI/CD pipeline on AWS - Code Deploy

CI/CD pipeline on AWS - Code Deploy

ยท

6 min read

Introduction:)

๐Ÿš€ Welcome to Day 52 of #90DaysOfDevOps! ๐Ÿš€

Today, we're diving deep into AWS CodeDeploy, a robust deployment service provided by Amazon Web Services. It streamlines the process of deploying applications and code to various compute services, including EC2 instances, AWS Lambda functions, and on-premises servers.

What is CodeDeploy?

AWS CodeDeploy is a fully managed deployment service provided by Amazon Web Services. It automates the process of deploying applications or code to various compute services, including Amazon EC2 instances, AWS Lambda functions, and on-premises servers.

It can also deploy a wide range of content and applications, including:

  • Code

  • Serverless AWS Lambda functions

  • Web and configuration files

  • Executables

AWS CodeDeploy makes it easy to put your latest software into action on your chosen servers. It comes with handy features like different ways to deploy and the ability to go back to an earlier version if needed.

Tasks:)

Task 1: Understanding Appspec.yaml and Deploying to EC2

  1. Read about Appspec.yaml file for CodeDeploy.

    The appspec.yaml file is a crucial configuration file used by AWS CodeDeploy. It defines the deployment process and instructs CodeDeploy on how to manage the deployment on your target environment. You can find detailed information on creating a appspec.yaml file in the official AWS documentation here.

    Here is a basic example of an appspec.yaml file:

     version: 0.0
     os: linux
     files:
       - source: /
         destination: /var/www/html/
     hooks:
       BeforeInstall:
         - location: scripts/install_dependencies.sh
           timeout: 300
           runas: root
       AfterInstall:
         - location: scripts/start_server.sh
           timeout: 300
           runas: root
    

    Below are key components commonly found in an appspec.yaml file:

    | Component | Description | | --- | --- | | Version | Specifies the version of the deployment specification (e.g., 0.0). | | OS | Defines the operating system used by the deployment target (e.g., Linux). | | Resources | Allows you to manage AWS resources like Auto Scaling groups or EC2 instances. | | Hooks | Defines lifecycle event hooks for different deployment phases (e.g., BeforeInstall, AfterInstall, ApplicationStop, etc.). Each hook specifies the location of a script file to be executed during that phase. | | Files | Specifies which files should be copied from the source location to the destination location on the deployment target. |

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

    To deploy an index.html file on an EC2 instance using Nginx and AWS CodeDeploy, follow these steps:

    1. Creating a CodeDeploy Application:

      • Navigate to the AWS Management Console and access CodeDeploy.

      • Click on "Applications," then select "Create application."

      • Choose the compute platform as "EC2/on-premises" and click "Create application."

      • Your application is now successfully created.

    2. Setting Up a Service Role:

      • Create a role named 'code-deploy-service-role' in AWS Identity and Access Management (IAM) with the necessary permissions. This role facilitates communication between CodeDeploy and other AWS services.

    3. Configuring an EC2 Instance:

      • Launch an Ubuntu EC2 instance on AWS, where you'll deploy the index.html file.
    4. Creating a Deployment Group:

      • After creating a CodeDeploy application, proceed to form a deployment group. This group defines the EC2 instances for deployment.

      • Add instance names to the deployment group.

      • Click on "Create deployment group."

    5. Installing the CodeDeploy Agent:

      • To enable code deployment on your EC2 instance, you must install the CodeDeploy agent. Execute the provided commands.
         # This installs the CodeDeploy agent and its prerequisites on Ubuntu  
         #!/bin/bash
         sudo apt-get update
         sudo apt-get install ruby
         sudo apt-get install wget
         wget https://aws-codedeploy-ap-south-1.s3.ap-south-1.amazonaws.com/latest/install
         chmod +x ./install
         sudo ./install auto
         sudo service codedeploy-agent status
         sudo service codedeploy-agent start
         sudo service codedeploy-agent status

  1. Creating an index.html File:

    • Create the index.html file that you intend to deploy on your EC2 instance.


Task-02: Adding appspec.yaml to CodeCommit Repository

  1. Adding an appspec.yamlfile to CodeCommit Repository

    1. Create the appspec.yaml File:

       yamlCopy codeversion: 0.0
       os: linux
       files:
         - source: /
           destination: /var/www/html
       hooks:
         BeforeInstall:
           - location: install_nginx.sh
             timeout: 300
             runas: root
         AfterInstall:
           - location: start_nginx.sh
             timeout: 300
             runas: root
      

      Also add the install_nginx.sh, and start_nginx.sh files as needed above

      • install_nginx.sh

          #!/bin/bash
        
          # Update package list and install Nginx
          sudo apt-get update
          sudo apt-get install nginx -y
        

      • start_nginx.sh

          #!/bin/bash
        
          # Start Nginx service
          sudo service nginx start
        

    2. Push Files to CodeCommit Repository:

      • Commit the changes.

          git add appspec.yaml install_nginx.sh start_nginx.sh
          git commit -m "Added appspec.yaml and scripts"
          git push origin <branch-name>
        

        Replace <branch-name> with the actual branch name.

    3. Verify Changes in CodeCommit:

      • Go to your CodeCommit repository in the AWS Management Console and verify that the files have been successfully pushed.

  1. Rebuilding the Project and Preparing for Deployment

    1. Edit the artifacts in your CodeBuild project and rebuild it.

    2. Create an S3 bucket for code deployment, or use an existing one.

    3. Add the artifacts to the S3 bucket and take note of the object URL.

      build the project it will automatically added the artifacts

  2. Deploying Your Application

    1. Open AWS CodeDeploy:

      • Go to AWS CodeDeploy in the AWS Management Console.
    2. Navigate to Deployment Groups:

      • In the CodeDeploy dashboard, click on "Applications" in the left sidebar.

      • Select your application.

    3. Select Your Deployment Group:

      • Choose the deployment group you created earlier.
    4. Create a Deployment:

      • Click on "Create deployment."

    5. Specify Revision:

      • In the "Revision location" section, paste the object URL from your S3 bucket where your artifacts are stored.

        1. Deployment Configuration:

          • Choose the deployment configuration based on your requirements.
        2. Deployment Group Settings:

          • Review and adjust deployment settings as needed.

  1. Deploy:

    • Click on "Create deployment" to initiate the deployment process.
  2. Monitor Deployment:

    • You can monitor the progress of the deployment in the CodeDeploy console.

  3. Verify Deployment:

  • Once the deployment is complete, verify that your application is running as expected.

With these steps, you have successfully deployed your application using AWS CodeDeploy. Ensure to monitor the deployment progress to address any issues that may arise.


Thank you for diving into this blog with me! I trust you found the information both helpful and enlightening. To stay updated on the latest in DevOps ๐Ÿš€, make sure to follow me. Remember, staying informed means staying ahead in the dynamic world of DevOps!

Feel free to connect with me on:

LinkedIn

Twitter

GitHub

For more updates and engaging discussions on DevOps, let's connect! ๐Ÿš€ #DevOpsCommunity

ย