Your CI/CD Pipeline on AWS - Part 3

Your CI/CD Pipeline on AWS - Part 3


What is AWS CodeDeploy?

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

CodeDeploy supports deploying application content from sources like Amazon S3 buckets, GitHub repositories, or Bitbucket repositories. It simplifies the deployment process, ensuring that you don’t need to make changes to your existing code.

Task 01: Deploying index.html on EC2 Using nginx

Step 1: Understanding the appspec.yaml File

The appspec.yaml file is crucial for AWS CodeDeploy as it specifies the deployment actions. It defines where to get the application from and what to do with it once it’s on the destination server. Here’s a breakdown of its components:

  • version: The version of the appspec.yaml file format.

  • os: The operating system of the deployment environment (e.g., linux).

  • files: Specifies the source and destination paths of files to be copied.

  • hooks: Scripts that run at specific lifecycle events during the deployment.

Here’s an example of a basic 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
  • BeforeInstall: Runs before the application revision is installed.

  • AfterInstall: Runs after the application revision is installed.

Step 2: Setting Up an EC2 Instance with nginx

  1. Launch an EC2 Instance:

    • Go to the EC2 dashboard on AWS.

    • Click "Launch Instance".

    • Choose an Amazon Machine Image (AMI), like Amazon Linux 2 AMI.

    • Select an instance type (e.g., t2.micro).

    • Configure instance details, add storage as needed, and configure security groups to allow HTTP (port 80) access.

    • Review and launch the instance.

  2. Install nginx on the EC2 Instance:

    • Connect to your EC2 instance via SSH.

    • Run the following commands to install nginx:

        sudo yum update -y
        sudo amazon-linux-extras install nginx1 -y
        sudo systemctl start nginx
        sudo systemctl enable nginx
      
  3. Create an index.html File:

    • Create a simple index.html file to deploy:

        <html>
        <head>
            <title>Welcome to nginx!</title>
        </head>
        <body>
            <h1>Deployment Successful!</h1>
        </body>
        </html>
      

Step 3: Setting Up the CodeDeploy Agent

  1. Install the CodeDeploy Agent:

    • On your EC2 instance, install the CodeDeploy agent with the following commands:

        sudo yum install -y ruby
        cd /home/ec2-user
        wget https://aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com/latest/install
        chmod +x ./install
        sudo ./install auto
        sudo service codedeploy-agent start
      

Task 02: Adding appspec.yaml to CodeCommit and Completing the Deployment

  1. Create a CodeCommit Repository:

    • Go to the CodeCommit dashboard on AWS.

    • Click "Create repository" and name your repository (e.g., MyAppRepo).

  2. Add Files to the Repository:

    • Clone the repository to your local machine:

        git clone https://git-codecommit.<region>.amazonaws.com/v1/repos/MyAppRepo
        cd MyAppRepo
      
    • Add your index.html and appspec.yaml files to the repository:

        cp /path/to/index.html .
        cp /path/to/appspec.yaml .
        git add index.html appspec.yaml
        git commit -m "Added index.html and appspec.yaml"
        git push
      
  3. Create a Deployment Application in CodeDeploy:

    • Go to the CodeDeploy dashboard on AWS.

    • Click "Create application".

    • Name your application and select "EC2/On-Premises" as the compute platform.

  4. Create a Deployment Group:

    • In your application, create a new deployment group.

    • Name the deployment group and select your service role.

    • Choose the EC2 instances to include in this deployment group by selecting tags or specific instances.

    • Choose the deployment settings and save the deployment group.

  5. Deploy Your Application:

    • In your application, click "Create deployment".

    • Select the CodeCommit repository and the commit ID.

    • Choose the deployment group you just created.

    • Click "Create deployment".

AWS CodeDeploy will now deploy your index.html file to the specified EC2 instance using the appspec.yaml file. Once the deployment is complete, you can navigate to the public IP address of your EC2 instance in your web browser to see your deployed index.html file.

Detailed Steps and Explanations

To make the process clearer, let’s go through each step with more detail:

Setting Up an EC2 Instance

  • Step-by-step EC2 Launch:

    • Step 1: Go to the AWS Management Console and open the EC2 Dashboard.

    • Step 2: Click on the "Launch Instance" button to start the setup.

    • Step 3: Select an AMI, like Amazon Linux 2, which is free-tier eligible.

    • Step 4: Choose an instance type, such as t2.micro, which is also part of the free tier.

    • Step 5: Configure the instance details, where you can leave most settings as default.

    • Step 6: Add storage if needed, but the default is usually sufficient for small deployments.

    • Step 7: Add tags to help identify your instance (optional).

    • Step 8: Configure the security group to allow HTTP traffic on port 80. This step is crucial to ensure your web server is accessible from the internet.

    • Step 9: Review all settings and click "Launch". You’ll need to select or create a key pair to access your instance via SSH.

  • Connecting to the EC2 Instance:

    • Step 1: Open a terminal or command prompt on your local machine.

    • Step 2: Use the SSH command to connect to your instance. Replace <key-pair-name> and <ec2-public-ip> with your actual key pair file and the public IP address of your EC2 instance.

        ssh -i <key-pair-name>.pem ec2-user@<ec2-public-ip>
      
  • Installing nginx:

    • Step 1: After connecting to your EC2 instance, update the package manager with sudo yum update -y.

    • Step 2: Install nginx using sudo amazon-linux-extras install nginx1 -y.

    • Step 3: Start the nginx service with sudo systemctl start nginx.

    • Step 4: Enable nginx to start on boot with sudo systemctl enable nginx.

Creating and Deploying the index.html File

  • Creating the index.html File:

    • Step 1: On your local machine, create a simple HTML file named index.html with the content provided earlier.

    • Step 2: This file will be the webpage that gets deployed to your EC2 instance.

  • Setting Up CodeDeploy Agent:

    • Step 1: Still connected to your EC2 instance via SSH, install Ruby with sudo yum install -y ruby.

    • Step 2: Download the CodeDeploy agent installation script using wget.

    • Step 3: Make the script executable with chmod +x ./install.

    • Step 4: Run the installation script with sudo ./install auto.

    • Step 5: Start the CodeDeploy agent with sudo service codedeploy-agent start.

Creating a CodeCommit Repository

  • Creating the Repository:

    • Step 1: Navigate to the CodeCommit service in the AWS Management Console.

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

  • Cloning the Repository:

    • Step 1: Copy the clone URL from the CodeCommit console.

    • Step 2: Open a terminal or command prompt on your local machine.

    • Step 3: Clone the repository using the git command:

        git clone https://git-codecommit.<region>.amazonaws.com/v1/repos/MyAppRepo
        cd MyAppRepo
      
  • Adding Files to the Repository:

    • Step 1: Copy your index.html and appspec.yaml files into the cloned repository directory.

    • Step 2: Add the files to the git staging area with git add index.html appspec.yaml.

    • Step 3: Commit the changes with git commit -m "Added index.html and appspec.yaml".

    • Step 4: Push the changes to the CodeCommit repository with git push.

Creating a Deployment in CodeDeploy

  • Creating the Application:

    • Step 1: Open the CodeDeploy service in the AWS Management Console.

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

    • Step 3: Select "EC2/On-Premises" as the compute platform.

  • Creating the Deployment Group:

    • Step 1: Inside your application, create a new deployment group.

    • Step 2: Name the deployment group, select a service role, and choose the target EC2 instances.

    • Step 3: Configure deployment settings and save the deployment group.

  • Creating the Deployment:

    • Step 1: Inside your application, click on "Create deployment".

    • Step 2: Select the CodeCommit repository and specify the commit ID to deploy.

    • Step 3: Choose the deployment group you created earlier and click "Create deployment".

AWS CodeDeploy will then handle the deployment process, copying files to the specified directory on your EC2 instance and running any specified hooks. Once completed, you should see the deployed index.html file by navigating to the public IP address of your EC2 instance in a web browser.

Conclusion

This guide has walked you through deploying a simple index.html file on an EC2 instance using AWS CodeDeploy. You’ve learned how to set up the EC2 instance, install nginx, create and use the appspec.yaml file, and deploy your application using CodeDeploy. This foundational knowledge is crucial for automating deployments and managing applications on AWS.

Keep exploring more advanced features of AWS CodeDeploy to handle complex deployment scenarios, such as blue/green deployments, canary deployments, and rolling updates. These skills will help you build a robust CI/CD pipeline and streamline your development and deployment processes.