Skip to content

arumullayaswanth/Terraform-jenkins-tomcat-deployment-project

Repository files navigation

DevOps Pipeline Setup with Terraform, Jenkins (Master-Slave), and Tomcat on AWS EC2

Deploy a web application using Jenkins to a Tomcat server. Everything is provisioned and configured using Terraform.


🔵 DevOps Pipeline Setup with Terraform, Jenkins (Master-Slave), and Tomcat on AWS EC2

Step 1: Launch EC2 and Install Terraform

  1. Launch an EC2 instance.(Name:Terraform)
  2. Connect to the EC2 instance via SSH.

Step 2: Grant Permissions to Terraform

  1. Navigate to IAM (Identity and Access Management).
  2. Go to Users → Click Create User.
  3. Set User Name as terraform.
  4. Click NextSet PermissionsPermission Options.
  5. Select Attach Policies Directly → Choose Administrator Access.
  6. Click NextCreate User.
  7. Open the terraform user profile.
  8. Go to Security CredentialsAccess KeyCreate Access Key.
  9. Select Use CaseCLI.
  10. Confirm by selecting "I understand the recommendation and want to proceed".
  11. Click NextCreate Access Key.
  12. Download the .csv file.

Step 3: Connect to Terraform EC2 Instance and Switch to Superuser

ssh -i <your-key.pem> ec2-user@<terraform-ec2-public-ip>
sudo -i

Step 4: Configure AWS CLI on EC2

aws configure

Provide the required values:

  • aws_access_key_id = YOUR_ACCESS_KEY
  • aws_secret_access_key = YOUR_SECRET_KEY
  • region = us-east-1
  • output = table

Verify configuration:

aws configure list
aws sts get-caller-identity

Step 5: Install Terraform on EC2

Create a script:

vim terraform.sh

Add the following content:

# Step 1: Install Required Packages
sudo yum install -y yum-utils shadow-utils

# Step 2: Add the HashiCorp Repository
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo

# Step 3: Install Terraform
sudo yum -y install terraform
terraform -version

Run the script:

sh terraform.sh

Step 6: Install Jenkins on EC2

Create a script:

vim Jenkins.sh

Add the following content:

# Install required packages
yum install git java-1.8.0-openjdk maven -y

# Add Jenkins repository
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key

# Install Java and Jenkins
sudo yum install java-17-amazon-corretto -y
yum install jenkins -y
update-alternatives --config java
#2

# Start Jenkins service
systemctl start jenkins.service
systemctl status jenkins.service

Run the script:

sh Jenkins.sh

Step 7: Retrieve Jenkins Initial Admin Password

cat /var/lib/jenkins/secrets/initialAdminPassword

Copy the password for the next step.


Step 8: Access Jenkins UI

  1. Copy the public IP address of your EC2 instance.
  2. Open a browser and enter:
    http://<terraform Public-IP>:8080
    
  3. Paste the initial admin password.
  4. Install suggested plugins.
  5. Create the first admin user:
    • Username :terraform
    • Password :terraform
    • Full Name
    • Email
  6. Click Save and ContinueSave and FinishStart using Jenkins.

Step 9: Configure Terraform Credentials in Jenkins

  1. Open Jenkins DashboardManage Jenkins.
  2. Navigate to CredentialsSystemGlobal Credentials (unrestricted).
  3. Click Add Credentials:
    • Kind: Select Secret Text
    • Secret: Enter your AWS Access Key(****************)
    • ID: accesskey
    • Description: Enter a meaningful description
  4. Click Save.
  5. Add another credential:
    • Kind: Select Secret Text
    • Secret: Enter your AWS Secret Key(******************)
    • ID: secretkey
    • Description: Enter a meaningful description
  6. Click Save.

Step 10: Create a Jenkins Pipeline Job for Terraform

  1. Navigate to Jenkins DashboardNew Item.
  2. Enter Name: terraform-project.
  3. Select Pipeline → Click OK.
  4. Under Pipeline Configuration:
    • This project is parameterizedAdd ParameterChoice Parameter
    • Name: action
    • Choices: apply and destroy
  5. Add the following pipeline script:
  6. Pipeline Script:
//Automating Infrastructure with Jenkins: Running Terraform Scripts using Jenkins Pipeline
pipeline {
   agent any

   environment {
       AWS_ACCESS_KEY_ID     = credentials('accesskey')
       AWS_SECRET_ACCESS_KEY = credentials('secretkey')
   }
   
   stages {
       stage('checkout') {
           steps {
               git 'https://github.com/arumullayaswanth/jenkins-tomcat-deployment-project.git'
           }
       }
       stage('init') {
           steps {
               sh 'terraform init'
           }
       }
       stage('validate') {
           steps {
               sh 'terraform validate'                
           }
       }
       stage('plan') {
           steps {
               sh 'terraform plan'
           }
       }
       stage('action') {
           steps {
               sh 'terraform $action --auto-approve'
           }
       }
   }
}
  1. Click Save.

Step 11: Build with Parameters

  1. Open Jenkins Dashboard → Select terraform-project.
  2. Click Build with Parameters.
  3. Choose action → Select apply.
  4. Click Build.

Step 12: Verify Terraform Deployment

  1. SSH into your Terraform EC2 instance.
  2. Run the following commands:
    cd /var/lib/jenkins/workspace/terraform-project
    ll
  3. List Terraform state:
    terraform state list

AWS infrastructure setup is completed now


Step 13–26: Jenkins Master & Slave Configuration, Tomcat Setup, WAR Deployment


Step 13: Connect to Jenkins-Master EC2

sudo -i
hostnamectl set-hostname Jenkins-Master
sudo -i

Step 14: Install Jenkins on Master

14.1. Create a script:

vim Jenkins.sh

14.2. Add the following content:

# Install required packages
yum install git java-1.8.0-openjdk maven -y

# Add Jenkins repository
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key

# Install Java and Jenkins
sudo yum install java-17-amazon-corretto -y
yum install jenkins -y
update-alternatives --config java

# Start Jenkins service
systemctl start jenkins.service
systemctl status jenkins.service

14.3. Run the script:

sh Jenkins.sh

14.4.Retrieve Jenkins Initial Admin Password:

cat /var/lib/jenkins/secrets/initialAdminPassword

Copy the password for the next step.

14.5. Access Jenkins UI:

  1. Copy the public IP address of your EC2 instance.
  2. Open a browser and enter:
    http://<Public-IP>:8080
    
  3. Paste the initial admin password.
  4. Install suggested plugins.
  5. Create the first admin user:
    • Username :jenkins
    • Password :jenkins
    • Full Name
    • Email
  6. Click Save and ContinueSave and FinishStart using Jenkins.

Step 15: Connect to Jenkins-Slave EC2 and Configure Jenkins Slave (Agent)

sudo -i
hostnamectl set-hostname Jenkins-Slave
sudo -i

Script:

vim jenkins-slave.sh

Content:

#STEP-1: INSTALLING GIT JAVA-1.8.0 MAVEN 
yum install git java-1.8.0-openjdk maven -y

#STEP-2: DOWNLOAD JAVA11 AND JENKINS
sudo yum install java-17-amazon-corretto -y
update-alternatives --config java
# *+ 2   /usr/lib/jvm/java-17-amazon-corretto.x86_64/bin/java(select this)
java -version

Run:

sh jenkins-slave.sh

Step 16: Install Tomcat in Jenkins-Slave


🔵 Install Apache Tomcat in Jenkins - 1

🔴 Install Apache Tomcat in Jenkins - 2

Prerequisites

  • Jenkins is installed and running.
  • Java 17 (Amazon Corretto) is installed.
  • A Linux-based OS (Amazon Linux, CentOS, or Ubuntu).

Step 16.1: Download and Extract Apache Tomcat:

wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.98/bin/apache-tomcat-9.0.98.tar.gz
tar -zxvf apache-tomcat-9.0.98.tar.gz

Step 16.2: Configure Tomcat Users: Edit the tomcat-users.xml file to add admin credentials.

sed -i '55  a\<role rolename="manager-gui"/>' apache-tomcat-9.0.98/conf/tomcat-users.xml
sed -i '56  a\<role rolename="manager-script"/>' apache-tomcat-9.0.98/conf/tomcat-users.xml
sed -i '57  a\<user username="tomcat" password="523182" roles="manager-gui, manager-script"/>' apache-tomcat-9.0.98/conf/tomcat-users.xml

add

  <role rolename="manager-gui"/>
  <role rolename="manager-script"/>
  <user username="tomcat" password="523182" roles="manager-gui, manager-script"/>
</tomcat-users>

Step 16.3: Modify Context.xml To allow remote access to Tomcat Manager:

sed -i '21d' apache-tomcat-9.0.98/webapps/manager/META-INF/context.xml
sed -i '22d' apache-tomcat-9.0.98/webapps/manager/META-INF/context.xml

Step 16.4: Start Tomcat:

sh apache-tomcat-9.0.98/bin/startup.sh

Step 16.5: Verify Installation: Access Tomcat in the browser:

http://<your-server-ip>:8080

Log in using the configured username (tomcat) and password (523182).

Step 16.6: Integrate Tomcat with Jenkins:

  1. Open Jenkins.
  2. Go to Manage Jenkins > Plugins > Available Plugins.
  3. Install Deploy to Container Plugin.> Go back to the top page

Step-17: Configure Tomcat Credentials in Jenkins Masters ( Jenkins Dashboard)

Step 17.1: Open Jenkins Dashboard:

  1. Log in to Jenkins.
  2. Click on Manage Jenkins.
  3. Navigate to Credentials > System > Global credentials (unrestricted).

Step 17.2: Add Tomcat Credentials:

  1. Click Add Credentials.
  2. Enter the following details:
    • Username: tomcat
    • Password: 523182
  3. Click Create.

Step 17.3: Copy Tomcat Credential ID:

  1. Go back to Credentials.
  2. Find the newly created Tomcat credentials.
  3. Copy the Credential ID for later use in Jenkins jobs.

Your Apache Tomcat server is now installed and linked to Jenkins! 🚀

Step 18: connection betten Jenkins Master & Jenkins Slave Configuration

Step 18.1: Open Jenkins Dashboard:

  1. Log in to Jenkins.
  2. Click on Manage Jenkins.
  3. nodes > Nwe Nodes > nodes name (jenkins-slave) > type : select Permanent Agent > creatr
  4. Number of executors : 3
  5. Remote root directory :/tmp
  6. Lavels : jenkins-slave
  7. Usage : Only build jobs with label expressions matching this node
  8. Launch method : Launch agents via SSH host : 18.212.69.14 Jenkins Slave Public IPv4 address Credentials : + Add--->jenkins--->click-->Add Credentials
    -->Kind:SSH Username with private key
    -->Username: ec2-user
    -->private key-->enter directly-->key(paste keypair my-Key pair.pem)
    ---->Add

    Credentials : select ec2-user
  9. Host Key Verification Strategy: Non verifying Verification Strategy
  10. Availability : Keep this agent online as much as possible
  11. SAVE

Step 19: Deploy WAR via Jenkins Pipeline

  1. Navigate to Jenkins DashboardNew Item.
  2. Enter Name: jenkins--project.
  3. Select Pipeline → Click OK.
  4. Pipeline Script:
pipeline {
    agent {
        label 'jenkins-slave'
    }
    stages {
        stage('checkout') {
            steps {
                git "https://github.com/arumullayaswanth/jenkins-java-project.git"
            }
        }
        stage("Build"){
            steps {
                sh "mvn compile"
            }
        }
        stage("Test"){
            steps {
               sh "mvn test"
            }
        }
        stage("Artifact"){
            steps {
                sh "mvn clean package"
            }
        }
        stage("Deploy") {
            steps {
                deploy adapters:[
                    tomcat9(
                        credentialsId: "8eacad04-e5e7-462e-a155-8fc36b9e1c52",   //Credential ID
                        path: " ",
                        url:"http://54.237.197.4:8080/" //jenkins-slave Public IPv4 address
                    )
                ],
                contextPath:"Netfilx",
                war:"target/*.war"
            }
        }
    }
}

Step 19: Build with Parameters

  1. Open Jenkins Dashboard → Select jenkins-project.
  2. Click Build.

Step 20: Verify WAR Deployment

cd /tmp/workspace/jenkins-project/target
ll

Step 21: Access Tomcat in Browser

  • URL: http://<Tomcat-IP>:8080
  • Login: tomcat / 523182
  • Refresh to see deployed app

DevOps Infrastructure with Terraform + Jenkins + Tomcat is now ready!

About

End-to-End CI/CD Deployment on AWS with Jenkins & Terraform

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published