Setting up Jenkins on an AWS EC2 instance allows you to automate your software development processes efficiently. This guide provides a detailed, step-by-step approach to launching an EC2 instance, installing Jenkins, and configuring it for your CI/CD needs.
Table of Contents:
- Prerequisites
- Step 1: Launch an EC2 Instance
- Step 2: Connect to Your EC2 Instance via SSH
- Step 3: Install Java
- Step 4: Install Jenkins
- Step 5: Configure Security Groups
- Step 6: Access Jenkins Web Interface
- Step 7: Create a Jenkins Job
- Conclusion
Prerequisites
- AWS Account: Ensure you have an active AWS account. If not, sign up here.
- Basic Knowledge: Familiarity with AWS services and command-line operations is beneficial.
Step 1 — Launch an EC2 Instance
- Log in to AWS: Access the AWS Management Console.
-
Navigate to EC2 Dashboard:
- In the Services menu, select EC2 under Compute.
-
Launch Instance:
- Click on Launch Instance.
- Name and Tags: Assign a name to your instance (e.g., “Jenkins-Server”).
- Application and OS Images (Amazon Machine Image): Choose Amazon Linux 2 AMI (HVM), SSD Volume Type.
- Instance Type: Select t2.micro (eligible for free tier).
-
Key Pair (Login):
- Create a new key pair or select an existing one.
- If creating new, download the
.pem
file and store it securely; you’ll need it to access your instance.
-
Network Settings:
- Create a new security group with the following inbound rules:
- SSH (port 22): Allows secure shell access.
- HTTP (port 80): Enables web traffic.
- Custom TCP (port 8080): Required for Jenkins access.
- Create a new security group with the following inbound rules:
- Configure Storage: The default storage configuration is typically sufficient.
- Launch: Review all settings and click Launch Instance.
-
Verify Instance Status:
- Return to the EC2 Dashboard.
- Ensure your instance’s Instance State is running.
Step 2 — Connect to Your EC2 Instance via SSH
-
Set Key Permissions:
- Open your terminal.
- Modify the permissions of your key pair file to ensure it’s not publicly viewable:
chmod 400 /path/to/your-key-pair.pem
-
Retrieve Public IP:
- In the EC2 Dashboard, select your instance.
- Note the Public IPv4 address listed in the instance details.
-
Establish SSH Connection:
- Use the following command, replacing
/path/to/your-key-pair.pem
with the path to your key pair file andec2-user@your-public-ip
with the appropriate username and IP address:
- Use the following command, replacing
ssh -i /path/to/your-key-pair.pem ec2-user@your-public-ip
ssh -i ~/.ssh/jenkins-key.pem ec2-user@54.123.45.67
Step 3 — Install Java
Jenkins requires Java to run. Follow these steps to install Java 17:
- Update Packages:
sudo yum update -y
- Install Java 17:
sudo yum install java-17-amazon-corretto -y
- Verify Installation:
java -version
openjdk version "17.0.x" 2023-xx-xx
OpenJDK Runtime Environment Corretto-17.0.x.x.x (build 17.0.x+xx)
OpenJDK 64-Bit Server VM Corretto-17.0.x.x.x (build 17.0.x+xx, mixed mode)
Step 4 — Install Jenkins
- Add Jenkins Repository:
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
- Import Jenkins GPG Key:
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
- Install Jenkins:
sudo yum install jenkins -y
- Enable Jenkins Service:
sudo systemctl enable jenkins
- Start Jenkins Service:
sudo systemctl start jenkins
- Check Jenkins Status:
sudo systemctl status jenkins
- Ensure the output indicates that Jenkins is active (running).
Step 5 — Access Jenkins Web Interface
- Open a browser and navigate to:
http://<public-ip-address>:8080
Replace <public-ip-address>
with your EC2 instance’s public IP.
- Retrieve the initial admin password:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Copy the password and paste it into the Jenkins setup page.
Install suggested plugins and create your admin user.
Step 6 — Create a Jenkins Job
- From the Jenkins dashboard, click New Item.
- Name the job (e.g., “Hello-World”) and select Freestyle Project.
- Scroll down to the Build Environment section and check “Delete workspace before build starts.”
- Under Build Steps, click Add build step > Execute Shell, and enter:
echo "Hello, World!"
- Save and click Build Now.
- Check the console output to see “Hello, World!” printed.
Conclusion
Congratulations! You’ve successfully set up Jenkins on an AWS EC2 instance, accessed its web interface, and created a simple Jenkins job. Jenkins is a versatile tool that supports complex CI/CD workflows, making it invaluable for software development teams.
Feel free to explore plugins and additional configurations to tailor Jenkins to your project’s needs.
Source link
lol