Table of Contents
Introduction
If you are working on AWS projects, one of the most important practices you’ll come across is CI/CD (Continuous Integration and Continuous Delivery). And when it comes to CI/CD, Jenkins is one of the most popular open-source automation tools used by developers and DevOps engineers worldwide.
In this guide, you’ll learn how to:
- Install Jenkins on AWS (Amazon Linux 2023 / Ubuntu 24.04).
- Connect Jenkins with your AWS account.
- Create basic Jenkins pipelines for both frontend (static website) and backend (Node.js API) projects.
- Follow best practices for Jenkins on AWS.
By the end, you’ll have a clear idea of how Jenkins can help you automate builds and deployments for your AWS projects.
What is Jenkins?
Jenkins is an open-source automation server that helps you automate repetitive tasks like:
- Building code
- Running tests
- Deploying applications
It comes with 1,800+ plugins, including integrations for AWS, GitHub, Docker, Kubernetes, and more.
Why Jenkins for AWS?
- Flexibility: Deploy apps to EC2, S3, ECS, EKS, or Lambda.
- Community Support: Huge community and ready-to-use pipelines.
- Cost: Free and open-source, you only pay for the AWS infrastructure.
Setting Up Jenkins on AWS Projects
1. Installing Jenkins on AWS EC2
The easiest way to start is by running Jenkins on an EC2 instance.
Step 1 – Launch an EC2 instance
- Choose Amazon Linux 2023 or Ubuntu 24.04 LTS.
- Instance type:
t2.micro(Free Tier) for testing. - Allow ports 22 (SSH) and 8080 (Jenkins UI) in the Security Group.
Step 2 – Install Jenkins on Amazon Linux 2023
sudo dnf update -y
sudo dnf install java-17-amazon-corretto -y
sudo curl -fsSL https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key | sudo tee /etc/pki/rpm-gpg/RPM-GPG-KEY-jenkins
sudo curl -fsSL https://pkg.jenkins.io/redhat-stable/jenkins.repo | sudo tee /etc/yum.repos.d/jenkins.repo
sudo dnf install jenkins -y
sudo systemctl enable jenkins
sudo systemctl start jenkins
Step 2 – Install Jenkins on Ubuntu 24.04 LTS
sudo apt update -y
sudo apt upgrade -y
sudo apt install openjdk-17-jdk -y
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
/usr/share/keyrings/jenkins-keyring.asc > /dev/null
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 update -y
sudo apt install jenkins -y
sudo systemctl enable jenkins
sudo systemctl start jenkins
Step 3 – Access Jenkins
- Visit
http://<EC2-Public-IP>:8080 - Enter the initial admin password from:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Install recommended plugins and create your first admin user.
2. Configuring Jenkins
- Go to Manage Jenkins → Plugins → Available Plugins.
- Install:
- Pipeline (for Jenkinsfile-based CI/CD).
- Git (to pull code).
- AWS SDK/CLI plugins (for AWS integrations).
Connecting Jenkins with AWS
To allow Jenkins to deploy to AWS, you need AWS credentials.
Option 1 – Using IAM User
- Create an IAM User in AWS with
AdministratorAccess(for testing). - Download Access Key + Secret Key.
- In Jenkins → Manage Jenkins → Credentials, add:
- AWS Access Key
- AWS Secret Key
Option 2 – Using IAM Role (Best Practice)
If Jenkins runs inside an EC2 instance:
- Attach an IAM Role with required permissions.
- Jenkins will automatically use the instance role for AWS access.
👉 For production, use least privilege policies (e.g., AmazonS3FullAccess only if deploying to S3).
Creating a Simple Jenkins Pipeline for AWS
Depending on your project type, your Jenkins pipeline will look different. Let’s cover both frontend and backend scenarios.
Scenario 1 – Frontend App (React/Angular/Vue → S3 Hosting)
If your project is a frontend app, the build process generates static files (index.html, CSS, JS). These can be deployed to S3 as a static website.
Create an S3 Bucket
aws s3 mb s3://my-jenkins-frontend-bucket
aws s3 website s3://my-jenkins-frontend-bucket/ --index-document index.html
Jenkinsfile Example
pipeline {
agent any
environment {
AWS_DEFAULT_REGION = 'ap-southeast-1'
S3_BUCKET = 'my-jenkins-frontend-bucket'
}
stages {
stage('Checkout Code') {
steps {
git branch: 'main', url: 'https://github.com/username/react-demo-app.git'
}
}
stage('Build') {
steps {
sh 'npm install'
sh 'npm run build'
}
}
stage('Deploy to S3') {
steps {
sh 'aws s3 sync ./build s3://$S3_BUCKET --delete'
}
}
}
}
👉 This will build the frontend app and upload the static files to S3.
Scenario 2 – Backend App (Node.js API → EC2 Deployment)
If your project is a backend API (Express, NestJS, etc.), you’ll typically deploy it to an EC2 instance or Elastic Beanstalk.
Jenkinsfile Example (Deploy to EC2 via SSH)
pipeline {
agent any
stages {
stage('Checkout Code') {
steps {
git branch: 'main', url: 'https://github.com/username/node-api.git'
}
}
stage('Install & Test') {
steps {
sh 'npm install'
sh 'npm test'
}
}
stage('Deploy to EC2') {
steps {
sh '''
ssh -o StrictHostKeyChecking=no ec2-user@<EC2-PUBLIC-IP> << 'EOF'
cd /home/ec2-user/node-api
git pull origin main
npm install --production
pm2 restart app || pm2 start server.js --name app
EOF
'''
}
}
}
}
👉 In this case, Jenkins connects to EC2 via SSH, pulls the latest code, installs dependencies, and restarts the app with PM2.
Best Practices for Jenkins on AWS
- Use IAM Roles instead of hardcoding AWS credentials.
- Run Jenkins on EKS for scalability.
- Enable HTTPS for Jenkins with an SSL certificate.
- Use Jenkins agents for parallel builds.
- Monitor with CloudWatch to capture logs and alerts.
Common Pitfalls & Troubleshooting
- Permission denied: Check IAM policies.
- EC2 firewall issues: Open port
8080in Security Groups. - Plugin failures: Update Jenkins to the latest LTS version.
- Slow builds: Use Jenkins agents or EC2 Auto Scaling.
Jenkins vs AWS Native CI/CD (CodePipeline, CodeBuild)
- Jenkins → Highly customizable, open-source, lots of plugins.
- AWS CodePipeline/CodeBuild → Fully managed, no server maintenance.
- Hybrid approach → Use Jenkins for orchestration, AWS services for builds/deployments.
Conclusion
Jenkins is a powerful tool to automate your AWS workflows. In this guide, you learned how to:
- Install Jenkins on an EC2 instance (Amazon Linux 2023 or Ubuntu 24.04).
- Connect Jenkins with AWS using IAM.
- Build pipelines for both frontend (S3) and backend (EC2/Elastic Beanstalk) applications.
Once you master the basics, you can extend Jenkins to deploy apps to ECS, EKS, Lambda, or integrate advanced strategies like Blue/Green or Canary deployments.
