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 |
- Launch an EC2 instance.(Name:Terraform)
- Connect to the EC2 instance via SSH.
- Navigate to IAM (Identity and Access Management).
- Go to Users → Click Create User.
- Set User Name as
terraform
. - Click Next → Set Permissions → Permission Options.
- Select Attach Policies Directly → Choose Administrator Access.
- Click Next → Create User.
- Open the terraform user profile.
- Go to Security Credentials → Access Key → Create Access Key.
- Select Use Case → CLI.
- Confirm by selecting "I understand the recommendation and want to proceed".
- Click Next → Create Access Key.
- Download the .csv file.
ssh -i <your-key.pem> ec2-user@<terraform-ec2-public-ip>
sudo -i
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
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
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
cat /var/lib/jenkins/secrets/initialAdminPassword
Copy the password for the next step.
- Copy the public IP address of your EC2 instance.
- Open a browser and enter:
http://<terraform Public-IP>:8080
- Paste the initial admin password.
- Install suggested plugins.
- Create the first admin user:
- Username :terraform
- Password :terraform
- Full Name
- Click Save and Continue → Save and Finish → Start using Jenkins.
- Open Jenkins Dashboard → Manage Jenkins.
- Navigate to Credentials → System → Global Credentials (unrestricted).
- Click Add Credentials:
- Kind: Select Secret Text
- Secret: Enter your AWS Access Key(****************)
- ID:
accesskey
- Description: Enter a meaningful description
- Click Save.
- Add another credential:
- Kind: Select Secret Text
- Secret: Enter your AWS Secret Key(******************)
- ID:
secretkey
- Description: Enter a meaningful description
- Click Save.
- Navigate to Jenkins Dashboard → New Item.
- Enter Name:
terraform-project
. - Select Pipeline → Click OK.
- Under Pipeline Configuration:
- This project is parameterized → Add Parameter → Choice Parameter
- Name:
action
- Choices:
apply
anddestroy
- Add the following pipeline script:
- 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'
}
}
}
}
- Click Save.
- Open Jenkins Dashboard → Select terraform-project.
- Click Build with Parameters.
- Choose action → Select
apply
. - Click Build.
- SSH into your Terraform EC2 instance.
- Run the following commands:
cd /var/lib/jenkins/workspace/terraform-project ll
- List Terraform state:
terraform state list
sudo -i
hostnamectl set-hostname Jenkins-Master
sudo -i
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:
- Copy the public IP address of your EC2 instance.
- Open a browser and enter:
http://<Public-IP>:8080
- Paste the initial admin password.
- Install suggested plugins.
- Create the first admin user:
- Username :jenkins
- Password :jenkins
- Full Name
- Click Save and Continue → Save and Finish → Start using Jenkins.
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
![]() 🔵 Install Apache Tomcat in Jenkins - 1 |
![]() 🔴 Install Apache Tomcat in Jenkins - 2 |
- 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:
- Open Jenkins.
- Go to Manage Jenkins > Plugins > Available Plugins.
- Install Deploy to Container Plugin.> Go back to the top page
Step 17.1: Open Jenkins Dashboard:
- Log in to Jenkins.
- Click on
Manage Jenkins
. - Navigate to
Credentials
>System
>Global credentials (unrestricted)
.
Step 17.2: Add Tomcat Credentials:
- Click
Add Credentials
. - Enter the following details:
- Username:
tomcat
- Password:
523182
- Username:
- Click
Create
.
Step 17.3: Copy Tomcat Credential ID:
- Go back to
Credentials
. - Find the newly created Tomcat credentials.
- Copy the Credential ID for later use in Jenkins jobs.
Step 18.1: Open Jenkins Dashboard:
- Log in to Jenkins.
- Click on
Manage Jenkins
. - nodes > Nwe Nodes > nodes name (jenkins-slave) > type : select Permanent Agent >
creatr
- Number of executors :
3
- Remote root directory :
/tmp
- Lavels :
jenkins-slave
- Usage :
Only build jobs with label expressions matching this node
- 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 : selectec2-user
- Host Key Verification Strategy:
Non verifying Verification Strategy
- Availability :
Keep this agent online as much as possible
- SAVE
- Navigate to Jenkins Dashboard → New Item.
- Enter Name:
jenkins--project
. - Select Pipeline → Click OK.
- 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"
}
}
}
}
- Open Jenkins Dashboard → Select jenkins-project.
- Click Build.
cd /tmp/workspace/jenkins-project/target
ll
- URL:
http://<Tomcat-IP>:8080
- Login:
tomcat
/523182
- Refresh to see deployed app
✅ DevOps Infrastructure with Terraform + Jenkins + Tomcat is now ready!