Day 52 Task: Your CI/CD pipeline on AWS Part- 3

Day 52 Task: Your CI/CD pipeline on AWS Part- 3

ยท

4 min read

On your journey of making a CI/CD pipeline on AWS with these tools, you completed AWS CodeCommit & CodeBuild.

Next few days you'll learn these tools/services:

  • 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: CodeDeploy agent to deploy code on EC2

  • Read about Appspec.yaml file for CodeDeploy.
    The appspec.yaml file in AWS CodeDeploy is a configuration file that:

    • Specifies how your application should be deployed.

    • Defines file mappings for copying files to target instances.

    • Sets permissions for files and directories.

    • Configures hooks to run scripts or functions during deployment.

    • Enables validation checks to ensure the deployed service works.

    • Supports environment variables for scripts.

    • Allows conditional deployments and rollbacks.

In essence, it's a critical guide for CodeDeploy to ensure your application deploys correctly and consistently.

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

    1. Launch an EC2 Instance: If you don't already have an EC2 instance, you can launch one. Ensure that the instance has the CodeDeploy agent installed.

    2. IAM Role: Attach an IAM role to your EC2 instance that grants permissions for CodeDeploy to interact with the instance.

    3. Install and Configure the CodeDeploy Agent

      1. Go to CodeDeploy and create an application.

  1. Give the application name and select EC2/On-premises.

  1. Now Create a deployment group and select service role.

    Add your roles ARN to service role:

    Add instance:

    Deployment group created.

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

    1. SSH into the EC2 Instance: Connect to your EC2 instance using SSH.

    2. Install the CodeDeploy Agent: You can install the CodeDeploy agent on your EC2 instance by following the instructions provided by AWS. Typically, it involves running commands like:

    3. Start and Enable the Agent: After installation, start and enable the agent to ensure it runs automatically on boot:

    4. I create a bash file for all the above installation processes.

       #!/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
      


๐Ÿ”ถ Task-02: Add appspec.yaml file to CodeCommit Repository and complete the deployment process.

  1. Create an appspec.yml file.

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

  2. Now create 2 scripts for installing and starting nginx.

  3. Push all the files to code commit.

  4. Now check your code commit repo.

  5. Now create S3 Bucket for the code commit.

  6. Now go to build projects and select artifacts.

  7. Artifacts updated and now start build.

  8. Verify the zip file has been created in the s3 bucket.

  9. Now Create an IAM role for the EC2 Instance:

  10. Now go to EC2 and attach the IAM role.

  11. Now Create a deployment under your codeDeploy section.

    Select S3 and paste your S3 url to the revision location.

    Deployment created:

  12. Now Browser instance public IP address:

In conclusion, AWS CodeDeploy is a powerful service for automating code deployments to various compute environments, including EC2 instances, AWS Lambda functions, and even on-premises servers. It plays a crucial role in enabling continuous integration and continuous deployment (CI/CD) practices, ensuring that your applications are consistently and reliably deployed across different environments.

For more details watch this video.


Happy Learning :)

Stay in the loop with my latest insights and articles on cloud โ˜๏ธ and DevOps โ™พ๏ธ by following me on Hashnode, LinkedIn (https://www.linkedin.com/in/chandreshpatle28/), and GitHub (https://github.com/Chandreshpatle28).

Thank you for reading! Your support means the world to me. Let's keep learning, growing, and making a positive impact in the tech world together.

#Git #Linux Devops #Devopscommunity #90daysofdevopschallenge #python #docker #Jenkins #Kubernetes #Terraform #AWS

Did you find this article valuable?

Support Chandresh Patle's Blog by becoming a sponsor. Any amount is appreciated!

ย