Day 52: Your CI/CD pipeline on AWS - Part 3 ๐Ÿš€ โ˜

Day 52: Your CI/CD pipeline on AWS - Part 3 ๐Ÿš€ โ˜

ยท

4 min read

Task-01 :

  • Read about Appspec.yaml file for CodeDeploy.

  • Deploy index.html file on EC2 machine using nginx

  • you have to setup a CodeDeploy agent in order to deploy code on EC2

    Step 1: Read about Appspec.yaml file for CodeDeploy

    Before we jump into deployment, it's essential to understand the appspec.yaml file, which plays a crucial role in defining how your application should be deployed.

    This file is used by CodeDeploy to determine what actions to take during the deployment process.

    It specifies hooks, permissions, and other details required for a successful deployment.

    Here's a simple 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/install_nginx.sh
            timeout: 300
            runas: root
        ApplicationStart:
          - location: scripts/stop_nginx.sh
            timeout: 300
            runas: root
        ApplicationStop:
          - location: scripts/start_nginx.sh
            timeout: 300
            runas: root
    

    Let's break down its sections:

    1. Version: Specifies the version of the deployment specification (e.g., 0.0).

    2. OS: Defines the operating system used by the deployment target (e.g., Linux).

    3. Resources: Allows you to manage AWS resources like Auto Scaling groups or EC2 instances.

    4. Hooks: Defines lifecycle event hooks for different deployment phases (e.g., BeforeInstall, AfterInstall, ApplicationStop, and ApplicationStart). Each hook specifies the location of a script file to be executed during that phase.

    5. Files: Specifies which files should be copied from the source location to the destination location on the deployment target.

Step 2: Deploy index.htmlfile on EC2 using Nginx

Creating a CodeDeploy Application

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

  2. Click on "Applications" and then select "Create application."

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

  4. Your application is now successfully created.

Creating a Service Role

To enable communication between CodeDeploy and other AWS services, you need to create a service role.

  1. Go to AWS Identity and Access Management (IAM) and create a role named 'code-deploy-service-role' with the required permissions.

Setting Up an EC2 Instance

Before deploying your application, create an EC2 instance where you want to deploy the index.html file.

  1. Launch an Ubuntu EC2 instance on AWS.

Creating a Deployment Group

Once you've created a CodeDeploy application, it's time to create a deployment group. This group defines the EC2 instances where your application will be deployed.

  1. Add instance names to the deployment group.

  2. Click on "Create deployment group."

Installing the CodeDeploy Agent

To deploy code on your EC2 instance, you must install the CodeDeploy agent.

  1. Install the CodeDeploy agent on your Ubuntu EC2 instance by running the provided commands & Check whether the Code agent is running.

      # 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
    

Creating an index.html File

Before moving forward, create the index.html file that you want to deploy on your EC2 instance.

Task-02 :

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

Adding an appspec.yamlfile to CodeCommit Repository

  1. Create an appspec.yaml file that specifies how CodeDeploy should handle your application.

  2. Push all the files, including the appspec.yaml file, to your CodeCommit repository.

    install_nginx.sh

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.

Deploying Your Application

  1. In AWS CodeDeploy, go to "Applications," "Deployment Groups," and select your deployment group.

  2. Click on "Create deployment," and paste the object URL in the Revision location.

    Now we can see the deployment is successful and you can check whether the file is running or not by going intoartifact.zip>index.html and click on index.html file to have output.

ย