Day 52:CI/CD Pipeline on AWS - Part 3 🚀 ☁

Day 52:CI/CD Pipeline on AWS - Part 3 🚀 ☁

Day#52 Of 90 Days Of DevOps Challenge

👋Introduction

Welcome back to our journey of building a robust CI/CD pipeline on AWS.

In the previous parts, we covered AWS CodeCommit and CodeBuild.

Today, we'll dive into two more essential tools/services: CodeDeploy and CodePipeline, along with utilizing Amazon S3.

These tools will take us one step closer to achieving our fully automated deployment process.

⚓What is CodeDeploy?

AWS CodeDeploy is a powerful service that automates application deployments to various computing platforms, including Amazon EC2 instances, on-premises servers, serverless Lambda functions, and Amazon ECS services.

It allows you to streamline your deployment processes, making them more efficient and reliable.

In this blog post, we'll explore the fundamentals of AWS CodeDeploy and walk you through the process of deploying an HTML file on an EC2 instance using CodeDeploy.

We'll also delve into the crucial AppSpec file, which plays a central role in managing deployments.

📜Task-01: Understanding Appspec.yaml and Deploying to 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: Adding appspec.yaml to CodeCommit Repository

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

    start_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.

🌈Conclusion

AWS CodeDeploy simplifies and automates the deployment process, ensuring the smooth delivery of your applications to various environments.

In this guide, we've covered the basics of CodeDeploy, the importance of the AppSpec file, and the step-by-step process of deploying an HTML file on an EC2 instance.

By following these steps, you can leverage CodeDeploy to efficiently manage your deployments and ensure your applications run smoothly on your chosen infrastructure.

Your voyage toward becoming a proficient AWS CI/CD practitioner persists! 🚀 ☁

Happy learning🎉 & may your cloud journey be filled with exciting discoveries!🎊

Thank you for joining us on this exciting Day 52 of the 90 Days of DevOps challenge. I hope you found the information helpful and insightful.💥

So please keep yourself updated with my latest insights and articles on DevOps 🚀 by following me on :

Hashnode: vishaltoyou.hashnode.dev

LinkedIn: linkedin.com/in/vishalphadnis