CI/CD pipeline on AWS : Part-3

CI/CD pipeline on AWS : Part-3

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.

Read about Appspec.yaml file for CodeDeploy.

The application specification file (AppSpec file) is a YAML -formatted or JSON-formatted file used by CodeDeploy to manage deployment. The AppSpec file for an EC2/On-Premises deployment must be named appspec. yml or appspec.yaml, unless you are performing a local deployment.

The appspec.yaml file is a configuration file that defines how the deployment should proceed. It specifies the deployment process, including which files should be deployed, where they should be deployed, and any scripts or hooks that should be executed during the deployment.

The appspec.yaml file typically includes the following sections:

  1. version: This section specifies the version of the AppSpec file format being used.

  2. os: This section specifies the operating system of the target instances.

  3. files: This section specifies the source and destination locations of the files to be deployed.

  4. hooks: This section specifies any scripts or hooks that should be executed during the deployment, such as scripts to stop and start the application.

The AppSpec file must be located in the root directory of the application source code and must be named “appspec.yml” or “appspec.yaml”. When you create a deployment group in CodeDeploy, you can specify the location of the AppSpec file in the deployment configuration.

Deploy index.html file on EC2 machine using nginx

In previous blogs we have had gone through CodeCommit and CodeBuild https://kshitijaa.hashnode.dev/cicd-pipeline-on-aws-part-1 https://kshitijaa.hashnode.dev/cicd-pipeline-on-aws-part-2

Create a CodeDeploy application:

You need to create a CodeDeploy application to deploy your index.html file. You can do this in the AWS Management Console.

In CodeDeploy, go to Applications and click on 'Create application'.

Select compute platform 'EC2/on premises' and click on 'Create application'.

The application is successfully created.

Create Service Role

For enabling communication between code deploy and other AWS services. Go to IAM service and create 'code-deploy-service-role' with below in trusted policy.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "",
            "Effect": "Allow",
            "Principal": {
                "Service": [
                    "codedeploy.amazonaws.com"
                ]
            },
            "Action": "sts:AssumeRole"
        }
    ]
}

Set up an EC2 instance:

You will need to create an EC2 instance on which you want to deploy the index.html file.

Launch a ubuntu, t2.micro EC2 instance

Create a deployment group:

Once you have created a CodeDeploy application, you need to create a deployment group. A deployment group is a set of EC2 instances where you want to deploy your application.

Mention Name of the deployment group and Service Role that was created before

Choose how to deploy -> Deployment type-In-place ,Add instance name, in KeyValue's Value section

Install AWS CodeDeploy Agent - Now, Keep deployment settings as is. Uncheck Load Balancer which is not required for now.

Click on 'Create deployment group'. and see details

You have to setup a CodeDeploy agent in order to deploy code on EC2.

Install the CodeDeploy agent:

You need to install the CodeDeploy agent on your Ubuntu EC2 instance. The CodeDeploy agent is a software package that runs on your instance and interacts with CodeDeploy to deploy your application. You can install the CodeDeploy agent by running the following script on your EC2 instance:

Refer below launch script - Run the script using the command bash install.sh

#!/bin/bash
sudo apt-get update
sudo apt-get install ruby
sudo apt-get install wget
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 status
sudo service codedeploy-agent start
sudo service codedeploy-agent status

Code agent is running.

Create an index.html file:

You need to create an index.html file that you want to deploy. You can create a simple HTML file using any text editor.

Task-2 :

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

Create an appspec.yaml file:

You need to create an appspec.yaml file that tells CodeDeploy what to do with your application. Here is an appspec.yaml file that deploys the index.html file on nginx. also create 2 scripts for installing nginx and starting nginx.

appspec.yaml

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

start_nginx.sh

#!/bin/bash
sudo systemctl start nginx
sudo systemctl enable nginx

install_nginx.sh

#!/bin/bash
sudo apt-get update -y
sudo apt-get install nginx -y

Push all the files to code commit using 'git add' and 'git commit' and 'git push' commands like below

All the files updated in code commit.

Now Let's edit artifacts.

First create 'S3 bucket'.

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

Now ReBuild the project.

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

See the Logs for details, Logs are configured to save to S3 when configured Build

Go to S3 bucket and download the log file

See the log content

Get Build artifacts.

Go to the S3 bucket where you have chosen to save your artifacts

Your artifacts has index.html file

Now create an S3 bucket for Code eployment.

AWS Services > S3 > Create bucket > bucket name > region > Create bucket

Create Deployment

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

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

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

    Create deployment!

    OMG! Deployment Got Failed!

    This is so bad to see the deployment fail even after setting each and everything so well!

    Deployment has failed due to insufficient permissions

So create a new service role for enabling communication between EC2 and S3, code deploy.

Attach that service role to EC2 instance.

Select EC2 instance, In actions, go to security and click on 'Modify IAM role'.

Role has attached

After updating IAM role, restart code-deploy agent.

sudo service codedeploy-agent restart

Now after the changes we can see the deployment is successful

Browse the Public URL of EC2 Instance! Your deployment is successful!

Thanks for Reading!🙏😀