Creating and Managing CI/CD Pipelines with Jenkins

Table of contents
- Step 1: Setting Up the Master EC2 Instance
- Step 2: Setting Up the Slave EC2 Instances
- Step 3: Configuring Jenkins Master and Slave
- Add SSH Credentials in Jenkins
- Step 4: Updating Security Groups
- Step 5: Creating and Managing Jenkins CI/CD Pipelines
- Step 6: Testing and Validation
- Step 7: Scaling Jenkins with More Slaves

To create and manage CI/CD pipelines with Jenkins on an AWS EC2 instance using a master-slave architecture, you'll need to follow these steps. This guide covers setting up Jenkins, configuring a master-slave setup, and updating security groups to allow communication.
Prerequisites
AWS EC2 instance (Ubuntu-based)
Jenkins installed on EC2 master node
Slave EC2 instances set up with the required environment (Java, Jenkins agent)
Security Groups configured for Jenkins communication
Step 1: Setting Up the Master EC2 Instance
Launch an EC2 Instance (Ubuntu recommended for simplicity):
Choose Ubuntu AMI.
Select instance type (e.g., t2.medium).
Create or select an existing Security Group that allows:
HTTP (80)
HTTPS (443)
SSH (22) for admin access.
Connect to the EC2 instance:
SSH into the instance using your private key:
ssh -i "your-key.pem" ubuntu@<your-ec2-public-ip>
Install Jenkins on the EC2 master instance:
Update the system:
sudo apt update -y sudo apt upgrade -y
Install Java (Jenkins runs on Java):
sudo apt install openjdk-11-jdk -y
Install Jenkins:
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add - sudo sh -c 'echo deb http://pkg.jenkins.io/debian/ stable main > /etc/apt/sources.list.d/jenkins.list' sudo apt update sudo apt install jenkins -y
Start Jenkins:
sudo systemctl start jenkins
Enable Jenkins to start on boot:
sudo systemctl enable jenkins
Open Jenkins web interface:
Go to
http://<your-ec2-public-ip>:8080
in your browser.Retrieve Jenkins unlock key by running:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Enter this password to unlock Jenkins.
Install required plugins:
- Install plugins for GitHub, Git, Docker, and any other necessary tools from
Manage Jenkins
→Manage Plugins
.
- Install plugins for GitHub, Git, Docker, and any other necessary tools from
Add SSH Credentials (Private Key) for the Slave EC2 Instance
To add SSH credentials (private key) for the slave EC2 instance in Jenkins, follow these detailed steps:
Step 1: Prepare the SSH Private Key
Before configuring SSH access for the slave EC2 instance, you need to generate an SSH key pair (if you don’t have one already) or use an existing key pair.
1.1 Generate SSH Key Pair
To generate an SSH private and public key pair, use the
ssh-keygen
command on your local machine or on any Linux/Unix-based system. Follow these steps:Open a Terminal or Command Prompt on your local machine (Mac, Linux, or Windows with Git Bash/WSL).
Run the following command to generate the SSH key pair:
bashCopyEditssh-keygen -t rsa -b 2048 -f ~/.ssh/my-ec2-key
Breakdown of the command:
-t rsa
: Specifies the type of key to generate (RSA).-b 2048
: Specifies the number of bits in the key (2048 bits is recommended for RSA).-f ~/.ssh/my-ec2-key
: Specifies the file path and name to save the private key (my-ec2-key
), typically saved under the.ssh
directory.
Choose a location to save the key: After running the command, you'll be prompted to enter a file path to save the private key:
bashCopyEditGenerating public/private rsa key pair. Enter file in which to save the key (/home/user/.ssh/my-ec2-key):
Press Enter to save it in the default location (or specify a different path if preferred).
Enter a passphrase (optional): You will then be prompted to enter a passphrase for the key. This is optional but recommended for extra security. If you choose not to use a passphrase, simply press Enter.
Example output:
bashCopyEditEnter passphrase (empty for no passphrase):
Key pair generated: After confirming the passphrase (or leaving it empty), the SSH key pair will be generated. You'll have two files:
Private Key:
my-ec2-key
Public Key:
my-ec2-key.pub
1.2 View and Copy the Public Key
To view the public key, run the following command:
bashCopyEditcat ~/.ssh/my-ec2-key.pub
This will display the public key content, which you can then copy.
Step 2: Setting Up the Slave EC2 Instances
Launch additional EC2 instances (for slaves) in the same VPC or subnet as the master instance.
Use a similar Ubuntu instance.
Ensure security groups allow inbound SSH access from the master instance.
Install Jenkins slave on the EC2 instance:
SSH into the slave instance:
ssh -i "your-key.pem" ubuntu@<slave-ec2-public-ip>
Install Java (since Jenkins requires it):
sudo apt install openjdk-11-jdk -y
Install necessary dependencies like
git
or others for your specific projects.
Step 3: Configuring Jenkins Master and Slave
Add SSH Credentials in Jenkins
Login to Jenkins: Open your Jenkins dashboard in a web browser by going to
http://<your-jenkins-master-ip>:8080
.Navigate to Manage Jenkins:
- In the Jenkins dashboard, click on
Manage Jenkins
in the left-hand side menu.
- In the Jenkins dashboard, click on
Access Manage Credentials:
- Under the
Security
section, click onManage Credentials
.
- Under the
Choose the Scope:
In the
Manage Credentials
section, you will see a list of available domains.If you're adding credentials for the global scope (for all Jenkins jobs), click on
(global)
.
Add New Credentials:
Click on
Add Credentials
on the left-hand side.In the Kind dropdown, select
SSH Username with private key
.
Fill in the SSH Credentials Form:
Username: Enter the SSH username for your slave EC2 instance (typically
ubuntu
for Ubuntu instances).Private Key:
Select
Enter directly
in the Private Key section.Open your private key file from your master ec2 in a text editor (e.g.,
vim
), and copy the entire contents of the file.Paste the private key contents into the box labeled "Private Key".
Passphrase (optional): If your private key is encrypted with a passphrase, enter it here.
ID: Provide an identifier (e.g.,
ec2-slave-key
) for the credentials, so it can be easily referenced later.Description: Optionally, provide a description, such as
Private Key for connecting to EC2 slave
.
Save the Credentials:
- Click
OK
to save the credentials.
- Click
Configure Slave on Jenkins Master:
Go to
Manage Jenkins
→Manage Nodes
.Click on
New Node
, name it (e.g.,slave1
), and selectPermanent Agent
.Configure the following:
Remote root directory: Path on the slave EC2 instance (e.g.,
/home/ubuntu/jenkins
).Usage: Use this agent for everything.
Launch method: Select
Launch agent via SSH
.Host: Slave EC2's public IP address.
Credentials: select credentions which we create named as
ec2-slave-key
.
Configure Slave public key:
Navigate to .ssh folder
- vim /.ssh/authorise_keys
Open authorise_keys file in edit mode put the public key copied from master EC2’s. and save using esc and put wq! and click Enter.
Verify Slave Connection:
Jenkins will attempt to connect to the slave using SSH.
Once successful, the slave will show as connected and ready.
Step 4: Updating Security Groups
Update Security Group of Master EC2:
Ensure that the master instance’s security group allows inbound communication on the following ports from the slave instance's IP:
SSH (22): For agent connection
Jenkins port (8080): If you're accessing the Jenkins UI
Update Security Group of Slave EC2:
Ensure the slave instance’s security group allows inbound SSH (port 22) from the master EC2 instance’s IP.
Step 5: Creating and Managing Jenkins CI/CD Pipelines
Create a Jenkins Pipeline:
In Jenkins, go to
New Item
→ SelectPipeline
.Write the pipeline script (for example, for building a simple Node.js app):
pipeline { agent any stages { stage('Build') { steps { sh 'npm install' } } stage('Test') { steps { sh 'npm test' } } stage('Deploy') { steps { sh 'deploy.sh' } } } }
Run Jobs on the Slave Node:
You can assign specific stages or the entire pipeline to run on the slave node by specifying the node in the pipeline:
pipeline { agent none stages { stage('Build') { agent { label 'slave1' } steps { sh 'npm install' } } stage('Test') { steps { sh 'npm test' } } stage('Deploy') { agent { label 'slave1' } steps { sh 'deploy.sh' } } } }
Step 6: Testing and Validation
Test the setup:
Commit code to your repository, triggering the Jenkins pipeline.
Verify that the pipeline runs on the slave node and passes all stages.
Monitor Jenkins Jobs:
- Check the Jenkins dashboard for job status, logs, and build results.
Step 7: Scaling Jenkins with More Slaves
Add more slaves as needed by following the same process for configuring new EC2 instances and adding them as agents in Jenkins.
Manage Resource Allocation:
- You can dynamically scale the slave nodes by using EC2 Auto Scaling or CloudFormation for automated provisioning.
This is a basic setup for Jenkins CI/CD using AWS EC2 and security groups in a master-slave configuration. It ensures efficient task distribution and builds scalability into your CI/CD pipeline.
Subscribe to my newsletter
Read articles from Vikas Surve directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Vikas Surve
Vikas Surve
I am an 𝗠𝗦 𝗖𝗲𝗿𝘁𝗶𝗳𝗶𝗲𝗱 𝗗𝗲𝘃𝗢𝗽𝘀 𝗘𝗻𝗴𝗶𝗻𝗲𝗲𝗿 𝗘𝘅𝗽𝗲𝗿𝘁 and 𝗔𝘇𝘂𝗿𝗲 𝗔𝗱𝗺𝗶𝗻𝗶𝘀𝘁𝗿𝗮𝘁𝗼𝗿 𝗔𝘀𝘀𝗼𝗰𝗶𝗮𝘁𝗲 with over 𝟭𝟬 𝘆𝗲𝗮𝗿𝘀 𝗼𝗳 𝗲𝘅𝗽𝗲𝗿𝗶𝗲𝗻𝗰𝗲 in designing, implementing, and optimizing DevOps solutions. My expertise includes 𝗖𝗜/𝗖𝗗 𝗮𝘂𝘁𝗼𝗺𝗮𝘁𝗶𝗼𝗻 𝘂𝘀𝗶𝗻𝗴 𝗚𝗶𝘁𝗟𝗮𝗯, 𝗝𝗲𝗻𝗸𝗶𝗻𝘀, 𝗮𝗻𝗱 𝗔𝘇𝘂𝗿𝗲 𝗗𝗲𝘃𝗢𝗽𝘀, as well as 𝗖𝗼𝗻𝘁𝗮𝗶𝗻𝗲𝗿 𝗼𝗿𝗰𝗵𝗲𝘀𝘁𝗿𝗮𝘁𝗶𝗼𝗻 𝘄𝗶𝘁𝗵 𝗗𝗼𝗰𝗸𝗲𝗿 𝗮𝗻𝗱 𝗞𝘂𝗯𝗲𝗿𝗻𝗲𝘁𝗲𝘀. 🔹 𝗘𝘅𝗽𝗲𝗿𝘁 𝗶𝗻 𝗱𝗲𝘀𝗶𝗴𝗻𝗶𝗻𝗴 𝗮𝗻𝗱 𝗺𝗮𝗻𝗮𝗴𝗶𝗻𝗴 𝗲𝗻𝗱-𝘁𝗼-𝗲𝗻𝗱 𝗖𝗜/𝗖𝗗 𝗽𝗶𝗽𝗲𝗹𝗶𝗻𝗲𝘀 🔹 𝗛𝗮𝗻𝗱𝘀-𝗼𝗻 𝗲𝘅𝗽𝗲𝗿𝗶𝗲𝗻𝗰𝗲 𝘄𝗶𝘁𝗵 𝗔𝘇𝘂𝗿𝗲, 𝗞𝘂𝗯𝗲𝗿𝗻𝗲𝘁𝗲𝘀 (𝗔𝗞𝗦), 𝗮𝗻𝗱 𝗧𝗲𝗿𝗿𝗮𝗳𝗼𝗿𝗺 𝗳𝗼𝗿 𝘀𝗰𝗮𝗹𝗮𝗯𝗹𝗲 𝗱𝗲𝗽𝗹𝗼𝘆𝗺𝗲𝗻𝘁𝘀 🔹 𝗣𝗮𝘀𝘀𝗶𝗼𝗻𝗮𝘁𝗲 𝗮𝗯𝗼𝘂𝘁 𝗮𝘂𝘁𝗼𝗺𝗮𝘁𝗶𝗼𝗻, 𝘀𝗲𝗰𝘂𝗿𝗶𝘁𝘆, 𝗮𝗻𝗱 𝗰𝗹𝗼𝘂𝗱-𝗻𝗮𝘁𝗶𝘃𝗲 𝘁𝗲𝗰𝗵𝗻𝗼𝗹𝗼𝗴𝗶𝗲𝘀 🛠 𝗦𝗸𝗶𝗹𝗹𝘀 & 𝗧𝗼𝗼𝗹𝘀 ✅ 𝗗𝗲𝘃𝗢𝗽𝘀 & 𝗖𝗜/𝗖𝗗: Azure DevOps, GitLab, Jenkins ✅ 𝗖𝗹𝗼𝘂𝗱 & 𝗜𝗻𝗳𝗿𝗮𝘀𝘁𝗿𝘂𝗰𝘁𝘂𝗿𝗲: Azure, AWS ✅ 𝗜𝗻𝗳𝗿𝗮𝘀𝘁𝗿𝘂𝗰𝘁𝘂𝗿𝗲 𝗮𝘀 𝗖𝗼𝗱𝗲 (𝗜𝗮𝗖): Terraform, Bicep ✅ 𝗖𝗼𝗻𝘁𝗮𝗶𝗻𝗲𝗿𝘀 & 𝗢𝗿𝗰𝗵𝗲𝘀𝘁𝗿𝗮𝘁𝗶𝗼𝗻: Docker, Kubernetes (AKS) ✅ 𝗖𝗼𝗻𝗳𝗶𝗴 𝗠𝗮𝗻𝗮𝗴𝗲𝗺𝗲𝗻𝘁: PowerShell, Shell Scripting ✅ 𝗠𝗼𝗻𝗶𝘁𝗼𝗿𝗶𝗻𝗴 & 𝗢𝗯𝘀𝗲𝗿𝘃𝗮𝗯𝗶𝗹𝗶𝘁𝘆: Grafana, Prometheus, Azure Monitor ✅ 𝗦𝗲𝗰𝘂𝗿𝗶𝘁𝘆 & 𝗡𝗲𝘁𝘄𝗼𝗿𝗸𝗶𝗻𝗴: Load Balancers, Firewalls, ClusterIP ✅ 𝗢𝗽𝗲𝗿𝗮𝘁𝗶𝗻𝗴 𝗦𝘆𝘀𝘁𝗲𝗺𝘀: Linux, Mac 💡 𝗞𝗲𝘆 𝗦𝘁𝗿𝗲𝗻𝗴𝘁𝗵𝘀 ✔ 𝗖𝗹𝗼𝘂𝗱 𝗔𝗿𝗰𝗵𝗶𝘁𝗲𝗰𝘁𝘂𝗿𝗲 & 𝗔𝘂𝘁𝗼𝗺𝗮𝘁𝗶𝗼𝗻 – Designing and managing scalable cloud solutions ✔ 𝗖𝗜/𝗖𝗗 & 𝗗𝗲𝘃𝗢𝗽𝘀 𝗟𝗲𝗮𝗱𝗲𝗿𝘀𝗵𝗶𝗽 – Implementing robust and automated software delivery pipelines ✔ 𝗧𝗲𝗮𝗺 𝗟𝗲𝗮𝗱𝗲𝗿𝘀𝗵𝗶𝗽 & 𝗠𝗲𝗻𝘁𝗼𝗿𝘀𝗵𝗶𝗽 – Leading a 5-member team, fostering collaboration and growth ✔ 𝗦𝗲𝗰𝘂𝗿𝗶𝘁𝘆 & 𝗖𝗼𝗺𝗽𝗹𝗶𝗮𝗻𝗰𝗲 – Ensuring cloud security, compliance, and best practices ✔ 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 𝗦𝗼𝗹𝘃𝗶𝗻𝗴 & 𝗢𝗽𝘁𝗶𝗺𝗶𝘇𝗮𝘁𝗶𝗼𝗻 – Driving efficiency through automation and DevOps practices ✔ 𝗖𝗼𝗻𝘁𝗶𝗻𝘂𝗼𝘂𝘀 𝗟𝗲𝗮𝗿𝗻𝗶𝗻𝗴 & 𝗜𝗻𝗻𝗼𝘃𝗮𝘁𝗶𝗼𝗻 – Exploring emerging technologies and best practices