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

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

ยท

6 min read

In the Upcoming next 4 days, I will be making a CI/CD pipeline on AWS with these tools.

  • CodeCommit

  • CodeBuild

  • CodeDeploy

  • CodePipeline

  • S3 Bucket

What is CodeDeploy?

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

  • You can deploy a nearly unlimited variety of application content, including:

  1. Code

  2. Serverless AWS Lambda functions

  3. Web and configuration files

  4. Executables

  5. Packages

  6. Scripts

  7. Multimedia files

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

  • CodeDeploy makes it easier for you to:

  1. Rapidly release new features.

  2. Update AWS Lambda function versions.

  3. Avoid downtime during application deployment.

  4. Handle the complexity of updating your applications, without many of the risks associated with error-prone manual deployments.


Before moving to task let's create a CodeDeploy application.

  • Step-01: Go to CodeDeploy service on AWS console.

Screenshot from 2023-08-06 22-49-11

  • Step-02: Click on Create application button.

Screenshot from 2023-08-06 22-49-44

  • Step-03: Enter Application name and Deployment group name and select EC2/On-premises as Environment configuration and click on Create application button.

What is EC2/On-premises?

  • An on-premises instance is any physical device that is not an Amazon EC2 instancethat can run the CodeDeploy agent and connect to public AWS service endpoints.

Screenshot from 2023-08-06 22-50-28

  • Thus we have created a CodeDeploy application.

Screenshot from 2023-08-06 22-50-53


Now Let's create a IAM role for CodeDeploy.

  • Step-01: Go to IAM service on the AWS console and create a IAM role for CodeDeploy.

Screenshot from 2023-08-06 23-05-34

  • Step-02: Select CodeDeploy as Service that will use this role and click on Next:Permissions button and select AWSCodeDeployFullAccess, AmazonS3FullAccess and AmazonEC2FullAccess as Permissions and click on Next:Tags button.

Screenshot from 2023-08-06 23-39-42

  • Step-03: Now update the trust relationship of the role by clicking on Edit trust relationship button. Change the Service to codedeploy.amazonaws.com and click on Update Trust Policy button.

Screenshot from 2023-08-06 23-52-31


Now Let's create a CodeDeploy application.

  • Step-01: Go to CodeDeploy service on the AWS console and go inside the application that we created earlier.

Screenshot from 2023-08-06 23-57-11

  • Step-02: Click on Create deployment group button.

Screenshot from 2023-08-06 23-57-20

  • Step-03: Enter Deployment group name and select IAM role that we have created earlier and click on Create deployment group button.

Screenshot from 2023-08-06 23-58-18

  • Step-04: Select In-place deployment as Deployment type and select Amazon EC2 instances as Environment configuration and select Tag as Key and Value as Name and click on Create deployment group button.

Screenshot from 2023-08-06 23-59-21

  • Step-05: Agent configuration is optional so we will skip it and Select Never and click on Create deployment group button.

Screenshot from 2023-08-07 00-01-53

  • Step-06: Thus we have created a CodeDeploy application.

Screenshot from 2023-08-07 00-02-53


Task-01:

Setup a CodeDeploy agent in order to deploy code on EC2

  • Updates the package manager to ensure it has the latest information about available packages.

  • Installs Ruby because the CodeDeploy agent is written in Ruby and requires it to run.

  • Downloads the installation script provided by AWS CodeDeploy and makes it executable.

  • Executes the installation script in "auto" mode to install the CodeDeploy agent with default settings.

  • Starts the CodeDeploy agent service to begin the deployment process.

  • Optionally, enables the CodeDeploy agent to start automatically on boot.

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

Screenshot from 2023-08-07 15-09-07

sudo chmod 777 installation_codedeploy_agent.sh

Screenshot from 2023-08-07 15-11-37

./installation_codedeploy_agent.sh

Screenshot from 2023-08-07 15-12-56


Read about Appspec.yaml file for CodeDeploy.

  • BeforeInstall, AfterInstall, and ApplicationStart are different hooks specifying scripts that will run at different stages of the deployment process.

  • Replace scripts/before_install.sh, scripts/after_install.sh, and scripts/application_start.sh with the actual paths to your scripts.

  • timeout specifies how long (in seconds) AWS CodeDeploy will wait for each script to finish before considering it a failure.

  • runas specifies the user context under which the script will run. In this case, the scripts will run as the root user (root).

  • files section specifies that all files from the source (/) should be deployed to the destination (/var/www/html).

example of "appspec.yaml" file

version: 0.0
os: linux

files:
  - source: /
    destination: /var/www/html

hooks:
    BeforeInstall:
      - location: scripts/before_install.sh
        timeout: 300
        runas: root

    AfterInstall:
      - location: scripts/after_install.sh
        timeout: 300
        runas: root

    ApplicationStart:
      - location: scripts/application_start.sh
        timeout: 300
        runas: root

Screenshot from 2023-08-08 14-07-48


Deploy index.html file on EC2 machine using the nginx web server.

  • Let's create install-nginx.sh the file.
#!/bin/bash
sudo apt-get update
sudo apt-get install nginx -y

Screenshot from 2023-08-07 16-02-28

  • let's create start-nginx.sh the file.
#!/bin/bash
sudo service nginx start

Screenshot from 2023-08-07 16-02-49

  • At last, we push all the files to Git Hub.
git add .
git commit -m "add all files"
git push origin master

Screenshot from 2023-08-07 16-06-45

  • Thus we can verify the file uploaded on CodeBuild.

Screenshot from 2023-08-07 16-12-10


  • Now Let's edit artifacts.

  • Developer tools > CodeBuild > Build projects > project name > edit artifacts

Screenshot from 2023-08-07 16-27-28

  • Now ReBuild the project.

  • Developer tools > CodeBuild > Build projects > project name > Rebuild

Screenshot from 2023-08-07 16-30-36


Now create an S3 bucket for code deployment.

  • Services > S3 > Create bucket > bucket name > region > Create bucket

  • I'm using my previous bucket which I created for code build.

  • Now add the artifacts in the S3 bucket which we have created for code deploy and rebuild again.

  • Thus we can verify the file uploaded on the S3 bucket and copy the object url.

Screenshot from 2023-08-07 16-32-25

  • Step-01:: Go to Developer tools > CodeDeploy > Applications > Deployment Groups > Select the deployment file

Screenshot from 2023-08-07 23-37-15

  • Step-02:: Now click on Create deployment button and paste the object url in the Revision location and click on Create deployment button.

Screenshot from 2023-08-08 00-01-20

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


Happy Learning :)

Did you find this article valuable?

Support DevOps by becoming a sponsor. Any amount is appreciated!

ย