# Project 16: Jenkins Driven: CI/CD for Amazon Clone with Monitoring Excellence(Step By Step Implementation)

### **Introduction:**

Step into the world of "Jenkins Driven: CI/CD for Amazon Clone with Monitoring Excellence" – a guide that shows how to make an Amazon-like app easily. 🚀 We use Jenkins to make it all smooth – from starting with Amazon's resources, setting up things with Terraform, to putting it all together with Docker and Kubernetes. But wait, there's more! We've added Grafana and Prometheus for keeping a close eye on your app. 🌐 This step-by-step guide makes sure your Amazon-style app is safe and works great, taking you from writing code to making it live. 🛠️💻 Join in, and let's make your Amazon Clone together!

### **Technologies Used in This Project:**

1. **Jenkins:** Automate your workflow! Jenkins orchestrates the entire process, ensuring seamless integration and continuous delivery, making your Amazon Clone development faster and more efficient.
    
2. **Terraform:** Meet Terraform – your infrastructure magician. It conjures up AWS resources effortlessly, making the setup of your Amazon Clone environment a breeze.
    
3. **Docker:** Containerization at its best! Docker wraps up your Amazon Clone app, making it portable and easy to deploy across different environments – a key player in ensuring consistency and efficiency.
    
4. **Kubernetes:** Enter Kubernetes, the deployment maestro. It manages your Docker containers, ensuring they run smoothly in your environment, providing scalability and reliability for your Amazon Clone.
    
5. **Grafana:** Visualize, monitor, and excel! Grafana offers insights into your Amazon Clone's performance, ensuring you're always in the loop, and your app is running at its best.
    
6. **Prometheus:** Meet Prometheus – your vigilant guardian. It keeps a watchful eye on your Amazon Clone, collecting and analyzing metrics, ensuring the health and reliability of your application.
    

### **Project Overview:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704389423187/00eaf5cf-8921-4ac9-9d1d-f3fd8636adc9.png align="center")

### **Project:**

GITHUB REPO: [https://github.com/patelajay745/Amazon-FE.git](https://github.com/patelajay745/Amazon-FE.git)

`Step 1 : Launch Ec2 Instance` with Ubnuntu AMI and t2.large type. I have used following terraform file to launch it.

```bash
provider "aws" {
  region = "us-east-2" # Change this to your desired AWS region
}

resource "aws_instance" "my_instance" {
  count = 1


  ami                    = "ami-05fb0b8c1424f266b" # Specify the AMI ID for your desired Amazon Machine Image
  instance_type          = "t2.large"
  key_name               = "admin-ajay" # Change this to your key pair name
  vpc_security_group_ids = [aws_security_group.terraform-instance-sg.id]


  tags = {
    Name = "Jenkins_Server"
  }
 //for storage
  root_block_device {
    volume_size = 30
  }


}

output "jenkins_public_ip" {
    value = [for instance in aws_instance.my_instance : instance.public_ip]

}



#Create security group 
resource "aws_security_group" "terraform-instance-sg" {
  name        = "terraform-created-sg"
  description = "Allow inbound ports 22, 8080"
  vpc_id      = "vpc-0a35a83de5d6649ab"

  ingress = [
    for port in [22, 80, 443, 8080, 9000, 3000,9090,4000] : {
      description      = "inbound rules"
      from_port        = port
      to_port          = port
      protocol         = "tcp"
      cidr_blocks      = ["0.0.0.0/0"]
      ipv6_cidr_blocks = []
      prefix_list_ids  = []
      security_groups  = []
      self             = false
    }
  ]

  #Allow all outbound requests
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}
```

`Step 2 : Create an IAM role for the newly created EC2 instance.`

Navigate to IAM Roles and click on "Create Role."

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704389845019/7ca4993e-75ef-43ab-8ca7-7da52d5b367f.png align="center")

Select "EC2" as the use case and proceed to the next step. Search for "AdministratorAccess" (for learning purposes) and click "Next."

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704389898644/c0ebe4dc-c392-4b8c-bdb9-e46032cc4ef2.png align="center")

Provide a name for the role and click "Create Role."

Now, attach the role to the EC2 instance. Choose the instance, then go to Actions &gt; Security &gt; Modify IAM role.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704389943164/c5401be5-5c6d-43a9-909d-1c0dc972b4af.png align="center")

Select the created role from the list and click on "Update IAM role."

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704390438676/7e0b3a21-2952-4049-8977-e323add0d401.png align="center")

`Step 3: Install Required packages.`

Install the required packages by SSHing into the EC2 instance.

Create two scripts, namely "[`jenkins-docker.sh`](http://jenkins-docker.sh)" and "[`other-packages.sh`](http://other-packages.sh)"

[`jenkins-docker.sh`](http://jenkins-docker.sh)

```bash
#!/bin/bash
sudo apt update -y
sudo apt install fontconfig openjdk-17-jre -y
java -version
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
  https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
  https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update -y
sudo apt-get install jenkins -y
sudo systemctl enable jenkins 
sudo systemctl start jenkins 

#install docker
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg -y
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
sudo usermod -aG docker ubuntu
newgrp docker
```

[other-packages.sh](http://other-packages.sh)

```bash
#!/bin/bash
# install trivy
sudo apt-get install wget apt-transport-https gnupg lsb-release -y
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | gpg --dearmor | sudo tee /usr/share/keyrings/trivy.gpg > /dev/null
echo "deb [signed-by=/usr/share/keyrings/trivy.gpg] https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main" | sudo tee -a /etc/apt/sources.list.d/trivy.list
sudo apt-get update
sudo apt-get install trivy -y
#install terraform
sudo apt install wget -y
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform
#install Kubectl on Jenkins
sudo apt update
sudo apt install curl -y
curl -LO https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
kubectl version --client
#install Aws cli
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
sudo apt-get install unzip -y
unzip awscliv2.zip
sudo ./aws/install

#install helm.
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3
chmod 700 get_helm.sh
./get_helm.sh
```

Execute both scripts using the following command.

```bash
chmod 777 jenkins-docker.sh
chmod 777 other-packages.sh
./jenkins-docker.sh
./other-packages.sh
```

Verify the installation of all packages.

```bash
docker --version 
trivy --version
aws --version
terraform --version
helm version
kubectl version
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704391114087/6bc59ada-a1fe-483a-9d40-507832c96b79.png align="center")

Grant executable permissions for `docker.sock` and initiate the Docker container for SonarQube.

```bash
sudo chmod 777 /var/run/docker.sock
docker run -d --name sonar -p 9000:9000 sonarqube:lts-community
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704391255718/e834bf48-9556-408d-b3bf-f5a4b8abf534.png align="center")

Step 4: Establish a connection between Jenkins and SonarQube.

Copy the IP address of the EC2 instance and paste it into the browser.

```bash
<Ec2-ip:8080>
```

It will prompt for a password.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704391364829/57b6994b-6d69-44ab-8d62-2bce96ea290c.png align="center")

Retrieve the password and complete the installation.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704391431527/458cffc5-3cf1-42de-8487-ef943626eebc.png align="center")

Jenkins Dashboard.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704391597448/b727a991-dc5b-4417-890e-3fa62c5ca1b0.png align="center")

Now access SonarQube using:

```bash
<ec2-ip:9000>
```

It will ask for the default username and password. Enter "admin" in both.

The SonarQube dashboard will look like this.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704391732567/b2bb8098-3e9b-4df3-8deb-8e9a276706b4.png align="center")

`Step 5: Terraform Configuration and EKS Provisioning`

Now, navigate to Jenkins and add the Terraform plugin to facilitate the AWS EKS provisioning through the Pipeline Job.

Follow these steps:

1. Access the Jenkins dashboard –&gt; Manage Jenkins –&gt; Plugins.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704391856691/6721a36c-bb15-4fb3-8029-626720479635.png align="center")

1. In the Available Plugins section, search for "Terraform" and install it.
    

To determine the path to Terraform, you can use the following command in terminal(Ec2 SSH):

```bash
which terraform
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704392341624/d9cb902e-4472-4883-aeb6-c91393829580.png align="center")

Navigate back to the Jenkins dashboard and follow these steps to add Terraform to the tools:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704393304409/1dfc55b7-a662-44d2-a7e6-08f8088e0c23.png align="center")

Find the section for adding tools, and look for Terraform in the list.

Add Terraform, specifying the name and the path to the Terraform executable on your system (the path you obtained using the `which terraform` command).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704393465368/eee24766-5918-4519-bd2a-f68865bafd89.png align="center")

Modify the S3 bucket name in the [`backend.tf`](http://backend.tf/) file. If you have forked the Git repository, navigate to the `EKS_terraform` folder and make the necessary changes. Update the S3 bucket name according to your preferences or project requirements. This ensures that Terraform uses the correct backend configuration for storing its state files.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704393557846/ff6b61d4-cfb6-4ca0-9359-b7a66554c7df.png align="center")

Create a new Jenkins job for provisioning Amazon EKS. This job will be responsible for managing the deployment and configuration of your EKS cluster.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704393598256/5b5f214f-dec8-4baf-a782-fc3830becb8a.png align="center")

I aim to incorporate build parameters to facilitate the application and destruction processes during the build phase exclusively. Please include the following configuration inside the job, as depicted in the image below.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704393681616/2e03ae6a-b300-4306-99c6-94a5192235b7.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705337659130/2415cd86-e5c9-4b49-a98a-c5ac0f9f95c4.png align="center")

`Pipeline Code:`

```bash

pipeline{
    agent any
    stages {
        stage('Checkout from Git'){
            steps{
                git branch: 'main', url: 'https://github.com/patelajay745/Amazon-FE.git'
            }
        }
        stage('Terraform version'){
             steps{
                 sh 'terraform --version'
             }
        }
        stage('Terraform init'){
             steps{
                 dir('EKS') {
                      sh 'terraform init'
                   }
             }
        }
        stage('Terraform validate'){
             steps{
                 dir('EKS') {
                      sh 'terraform validate'
                   }
             }
        }
        stage('Terraform plan'){
             steps{
                 dir('EKS') {
                      sh 'terraform plan'
                   }
             }
        }
        stage('Terraform apply/destroy'){
             steps{
                 dir('EKS') {
                      sh 'terraform ${action} --auto-approve'
                   }
             }
        }
    }
}
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704393849170/bc184c65-fdc0-4f13-be46-f560b630eaf5.png align="center")

Click "Save," then proceed to "Build with Parameters." Choose "apply" and click "Build."

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704393922833/a3e71c2c-9bbb-47cb-a00c-2508e231464f.png align="center")

The EKS provisioning process will take some time. If everything is configured correctly, the status will resemble the image below.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704394505690/82569252-729c-4807-b177-467d2c4e65a6.png align="center")

Now check created resource in AWS console.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704394610871/f69f90f2-36cd-4a57-830f-fd968d0dddef.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704394681200/8f038f5a-be64-4b45-bc61-6b9d54741012.png align="center")

`Step 6: Plugin Installation and Setup (Java, Sonar, Node.js, OWASP, Docker)`

Navigate to the Jenkins dashboard.

Go to "Manage Jenkins" → "Plugins" → "Available Plugins."

Search for the following plugins:

1. Eclipse Temurin Installer
    
2. SonarQube Scanner
    
3. NodeJS
    
4. OWASP Dependency-Check
    
5. Docker
    
6. Docker Commons
    
7. Docker Pipeline
    
8. Docker API
    
9. Docker Build Step
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704395141654/34032543-3558-4ae2-b1ab-f39407912fe6.png align="center")
    

`Step 7: Configuration of Global tools.`

Navigate to "Manage Jenkins" → "Tools" → Install JDK(17) and Node.js(16) → Click on "Apply and Save."

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704395891381/3b5a360c-d39f-4e36-a45b-e5ea5d919a2f.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704396016291/71621c8d-6dce-4c64-86b2-34bd268a09c6.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704396041483/4bbbfe21-f074-4a52-b6d6-1b6d78895332.png align="center")

For SonarQube:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704396082301/cc1e89c4-8262-4f17-b52f-c0f87763d24a.png align="center")

For Owasp:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704396158808/40c0a4e1-62d0-480a-819b-04bbc2f7444c.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704396134970/9daf1ed4-051b-490c-b0e8-d11560e57e8b.png align="center")

For Docker:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704396216458/340d9d07-f71e-4549-92cf-d400da0f2bd6.png align="center")

Click on "Apply and Save."  
`Step 8: Configure Sonar Server in Manage Jenkins`

Retrieve the Public IP Address of your EC2 Instance. Since SonarQube operates on Port 9000, access it via &lt;Public IP&gt;:9000.

Visit your SonarQube Server, navigate to Administration → Security → Users, click on Tokens, update the token by assigning it a name, and then generate the token.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704396315103/4a1116bc-a7e5-4305-bacb-5d6f1993b0d7.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704396335772/745719cd-eabe-42e7-aa8c-7bb4c4f625b6.png align="center")

Enter name of token then click on "Generate"

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704396393093/064c4931-5572-42ca-b474-1e729c78ef50.png align="center")

Copy the token, then go to the Jenkins Dashboard → Manage Jenkins → Credentials → Add Secret Text. The entry should resemble this.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704396435741/303edea6-cfad-44c8-8880-45d5b883b952.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704396463361/834fca80-ef1b-4437-a074-c44483923672.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704396499531/ed498db8-a573-45f9-8193-5d377502ec0f.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704396558672/f87c9876-887e-40f3-be11-a145ff6a1eec.png align="center")

Now, Navigate to Dashboard → Manage Jenkins → System and Add like the below image.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704396695981/c8da8602-aa4b-4b59-89e9-cf0f0acb6333.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704396761540/6bc0a3d1-51cd-41d0-b20d-1aa38f0e84f4.png align="center")

Click on "Apply and Save."

In the Sonarqube Dashboard, also include a quality gate by navigating to Administration → Configuration → Webhooks.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704396813984/e4e5846d-8ea9-4155-92f6-089304a468aa.png align="center")

Click on "Create"

```bash
In URL Section:
<http://jenkins-public-ip:8080>/sonarqube-webhook/>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704396950706/5172c466-bf1a-43cc-9ccd-36e72b7c250f.png align="center")

Now, integrate Docker credentials into Jenkins for logging in and pushing the image:

Navigate to Manage Jenkins → Credentials → Global → Add Credential.

Provide DockerHub Username and Password under Global Credentials.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704397264681/85c617f6-688e-4f8e-982d-757453e8074d.png align="center")

`Step 9: Pipeline up to Docker`

Now, let's create a new job for our pipeline.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704397320815/5bc97e22-9e46-4cca-be65-65fbe557af00.png align="center")

Add Following script in pipeline section.

```bash
pipeline{
    agent any
    tools{
        jdk 'jdk17'
        nodejs 'node16'
    }
    environment {
        SCANNER_HOME=tool 'sonar-scanner'
    }
    stages {
        stage('clean workspace'){
            steps{
                cleanWs()
            }
        }
        stage('Checkout from Git'){
            steps{
                git branch: 'main', url: 'https://github.com/patelajay745/Amazon-FE.git'
            }
        }
        stage("Sonarqube Analysis "){
            steps{
                withSonarQubeEnv('sonar-server') {
                    sh ''' $SCANNER_HOME/bin/sonar-scanner -Dsonar.projectName=Amazon \
                    -Dsonar.projectKey=Amazon'''
                }
            }
        }
        stage("quality gate"){
           steps {
                script {
                    waitForQualityGate abortPipeline: false, credentialsId: 'Sonar-Token'
                }
            }
        }
        stage('Install Dependencies') {
            steps {
                sh "npm install"
            }
        }
        stage('OWASP FS SCAN') {
            steps {
                dependencyCheck additionalArguments: '--scan ./ --disableYarnAudit --disableNodeAudit', odcInstallation: 'DP-Check'
                dependencyCheckPublisher pattern: '**/dependency-check-report.xml'
            }
        }
         stage('TRIVY FS SCAN') {
            steps {
                sh "trivy fs . > trivyfs.txt"
            }
        }
        stage("Docker Build & Push"){
            steps{
                script{
                   withDockerRegistry(credentialsId: 'docker', toolName: 'docker'){
                       sh "docker build -t amazon ."
                       sh "docker tag amazon patelajay745/amazon:latest "
                       sh "docker push patelajay745/amazon:latest "
                    }
                }
            }
        }
        stage("TRIVY"){
            steps{
                sh "trivy image patelajay745/amazon:latest > trivyimage.txt"
            }
        }
        stage("deploy_docker"){
            steps{
                sh "docker stop amazon || true"  // Stop the container if it's running, ignore errors
                sh "docker rm amazon || true" 
                sh "docker run -d --name amazon -p 4000:3000 patelajay745/amazon:latest"
            }
        }
    }
}
```

Click on "Apply and Save."

Click on "Build Now"

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704399885778/482dce0a-50e7-46fb-bf9d-eb7af20d83b0.png align="center")

Now see the reports on SonarQube dashboard.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704399975543/fcde0614-ffbf-4ba7-901e-952fe8877004.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704400008275/e3bd0beb-16a9-4532-b902-328a7087a58b.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704400070893/d5b81823-1f79-4bea-9e14-d9891908184b.png align="center")

When you log in to Dockerhub, you will see a new image is created

`Step 10: Deployment on EKS`

SSH to Jenkins server and enter following command:

```bash
aws eks update-kubeconfig --name <CLUSTER NAME> --region <CLUSTER REGION>
aws eks update-kubeconfig --name EKS_CLOUD --region us-east-2
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704400121802/04b493ea-0565-40a6-a790-78065774082b.png align="center")

Get config file using following code.

```bash
cat ~/.kube/config
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704400190366/ed89216c-e99e-41c4-8530-b8c1296125a2.png align="center")

Copy it and save it in a document or another folder, naming it secret-file.txt.

Note: Create a file named secret-file.txt in your file explorer, store the configuration within it, and utilize it in the Kubernetes credential section.

Now install required plugin for kubernates in Jenkins.

Go to "Manage Jenkins" → "Plugins" → "Available Plugins."

Search for the following plugins:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704400238772/945e6783-b29a-49a8-9184-4175a4f86c20.png align="center")

Navigate to Manage Jenkins → Credentials → Global → Add Credential.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704400381566/88e967ad-bbba-47a8-b8c8-f3d5fbc0a7a2.png align="center")

Add another Stage into pipeline to deploy on the Kubernetes cluster.

```bash
stage('Deploy to Kubernetes') {
            steps {
                script {
                    dir('K8S') {
                        withKubeConfig(caCertificate: '', clusterName: '', contextName: '', credentialsId: 'k8s', namespace: '', restrictKubeConfigAccess: false, serverUrl: '') {
                            // Apply deployment and service YAML files
                            sh 'kubectl apply -f deployment.yml'
                            sh 'kubectl apply -f service.yml'

                            // Get the external IP or hostname of the service
                            def externalIP = sh(script: 'kubectl get svc amazon-service -o jsonpath="{.status.loadBalancer.ingress[0].hostname}"', returnStdout: true).trim()

                            // Print the URL in the Jenkins build log
                            echo "Service URL: http://${externalIP}/"
                        }
                    }
                }
            }
        }
```

Find Weburl from Jenkins Console output.

If you dont get url in Jenkins Console then you can run following command on terminal.

```bash
kubectl get svc amazon-service -o jsonpath={.status.loadBalancer.ingress[0].hostname}
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704401225885/b0ee34f2-b0fa-463f-9f3a-ef39e78d9fbf.png align="center")

Access on web browser.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704401107781/caa6f300-9433-45ff-9fc2-c024c872c1ca.png align="center")

Final pipeline.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704401285330/79b5f179-484c-4d22-97f2-8bafa2a0ffe0.png align="center")

`Step 11: Setup Monitoring on cluster`

Create new pipeline.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704401419553/8a2a5c3f-7206-4efa-91db-b9ad74269447.png align="center")

Pipeline code:

```bash
pipeline{
    agent any
    
    stages {
        stage('Setup') {
            steps {
                script {
                    // Add Helm repositories
                    sh 'helm repo add stable https://charts.helm.sh/stable'
                    sh 'helm repo add prometheus-community https://prometheus-community.github.io/helm-charts'
                }
            }
        }
        stage('Deploy Prometheus') {
            steps {
                script {
                    
                        withKubeConfig(caCertificate: '', clusterName: '', contextName: '', credentialsId: 'k8s', namespace: '', restrictKubeConfigAccess: false, serverUrl: '') {
                            
                           // Check if the namespace 'prometheus' exists
                                def namespaceExists = sh(script: 'kubectl get namespace prometheus', returnStatus: true) == 0
                    
                                // If the namespace doesn't exist, create it and install Prometheus using Helm
                                if (!namespaceExists) {
                                    sh 'kubectl create namespace prometheus'
                                    sh 'helm install stable prometheus-community/kube-prometheus-stack -n prometheus'
                                } else {
                                    echo 'Namespace prometheus already exists.'
                                }

                
                        }
                    
                }
            }
        }
        stage('Patch Services') {
            steps {
                script {
                    withKubeConfig(caCertificate: '', clusterName: '', contextName: '', credentialsId: 'k8s', namespace: '', restrictKubeConfigAccess: false, serverUrl: '') {
                        
                    // Patch Prometheus service to use ClusterIP
                    sh 'kubectl patch svc stable-kube-prometheus-sta-prometheus -n prometheus --type=json -p=\'[{"op":"replace","path":"/spec/type","value":"LoadBalancer"}]\'' 

                    // Patch Grafana service to use ClusterIP
                    sh 'kubectl patch svc stable-grafana -n prometheus --type=json -p=\'[{"op":"replace","path":"/spec/type","value":"LoadBalancer"}]\'' 
                     }
                }     
            }
        }

        stage('Get Service URLs') {
            steps {
                script {
                    withKubeConfig(caCertificate: '', clusterName: '', contextName: '', credentialsId: 'k8s', namespace: '', restrictKubeConfigAccess: false, serverUrl: '') {
                        
                    // Get the external IP or hostname of the service
                   def prometheus = sh(script: 'kubectl get svc stable-kube-prometheus-sta-prometheus -n prometheus -o jsonpath="{.status.loadBalancer.ingress[0].hostname}"', returnStdout: true).trim()

                     // Get the external IP or hostname of the service
                   def grafana = sh(script: 'kubectl get svc stable-grafana -n prometheus -o jsonpath="{.status.loadBalancer.ingress[0].hostname}"', returnStdout: true).trim()

                   
                    
                     echo "Service URL for prometheus: http://${prometheus}:9090"
                      echo "Service URL for grafana : http://${grafana}/"
                    }
                }
            }
        }
    }
        
    
}
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704403723479/2c1f4704-a815-4cb0-bfc3-2da289faeec7.png align="center")

Click on "Save". and "Build Now"

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704403771817/a0b0eba4-ebc0-4fd6-afb1-2e7ab7a7b27c.png align="center")

Find Weburl for grafana and premethus from Jenkins Console output.

If you dont get url in Jenkins Console then you can run following command on terminal.

```bash
kubectl get svc stable-kube-prometheus-sta-prometheus -n prometheus -o jsonpath="{.status.loadBalancer.ingress[0].hostname}"

#for grafana url 
kubectl get svc stable-grafana -n prometheus -o jsonpath="{.status.loadBalancer.ingress[0].hostname}"
```

To open promethus url in browser you need to attach ":9090/targets" at end of url.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704404059855/ddf083d6-eb2e-471f-8172-3becd126f0aa.png align="center")

To open grafana url you might need to wait for a while.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704404146431/4b4a2ace-74c1-4340-811c-0c80cbbb2126.png align="center")

`Enter user name "admin" and password "prom-operator"`

Click on "+" on right hand side and then click on import dashboard as shown in below screenshot.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704404465151/9b59615c-7b30-4a2e-bb19-5fc7f60a7de2.png align="center")

Enter "15661" and click on "Load"

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704404793978/60df81d4-a9f7-4b7f-a9bc-01eb31361be5.png align="center")

Then choose "prometheus" as source and Click on "Import"

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704404736432/ee99f77e-f572-4edc-9589-80850d461645.png align="center")

Here you go. Your dashboard is ready.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704404933475/715b7f00-d436-4ada-bf93-5ac1fa90681d.png align="center")

You can import dashboard as per your requirement from this site. [https://grafana.com/grafana/dashboards/](https://grafana.com/grafana/dashboards/)

`Step 12: Destroy EKS cluster when your done with project.`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704405040036/65f0c008-64a3-477e-a494-6ad940fa1cba.png align="center")

Don't Forget to Destroy Terraform created Ec2 instance for Jenkins server.

### **Conclusion**

Building an app like Amazon with Jenkins has been exciting! 🚀 Jenkins helps smoothly put together Amazon's virtual world, like arranging blocks. It makes sure everything works well, and we've added special guards, Grafana and Prometheus, to watch over the app. 🌐 This guide takes you through each step, making sure your Amazon-like app is safe and works great, from writing code to making it live. 🛠️💻 Let's celebrate this success and make your Amazon Clone even better!
