Streamlining CI/CD: A Complete Guide to Installing Jenkins on AWS EC2

Architecture of OpenAI


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

  1. Log in to AWS: Access the AWS Management Console.
  2. Navigate to EC2 Dashboard:

    • In the Services menu, select EC2 under Compute.
  3. 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.
    • Configure Storage: The default storage configuration is typically sufficient.
    • Launch: Review all settings and click Launch Instance.
  4. 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

  1. 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
Enter fullscreen mode

Exit fullscreen mode

  1. Retrieve Public IP:

    • In the EC2 Dashboard, select your instance.
    • Note the Public IPv4 address listed in the instance details.
  2. Establish SSH Connection:

    • Use the following command, replacing /path/to/your-key-pair.pem with the path to your key pair file and ec2-user@your-public-ip with the appropriate username and IP address:
ssh -i /path/to/your-key-pair.pem ec2-user@your-public-ip
Enter fullscreen mode

Exit fullscreen mode

ssh -i ~/.ssh/jenkins-key.pem ec2-user@54.123.45.67
Enter fullscreen mode

Exit fullscreen mode



Step 3 — Install Java

Jenkins requires Java to run. Follow these steps to install Java 17:

  1. Update Packages:
sudo yum update -y
Enter fullscreen mode

Exit fullscreen mode

  1. Install Java 17:
sudo yum install java-17-amazon-corretto -y
Enter fullscreen mode

Exit fullscreen mode

  1. Verify Installation:
java -version
Enter fullscreen mode

Exit fullscreen mode

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)
Enter fullscreen mode

Exit fullscreen mode



Step 4 — Install Jenkins

  1. Add Jenkins Repository:
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
Enter fullscreen mode

Exit fullscreen mode

  1. Import Jenkins GPG Key:
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
Enter fullscreen mode

Exit fullscreen mode

  1. Install Jenkins:
sudo yum install jenkins -y
Enter fullscreen mode

Exit fullscreen mode

  1. Enable Jenkins Service:
sudo systemctl enable jenkins
Enter fullscreen mode

Exit fullscreen mode

  1. Start Jenkins Service:
sudo systemctl start jenkins
Enter fullscreen mode

Exit fullscreen mode

  1. Check Jenkins Status:
sudo systemctl status jenkins
Enter fullscreen mode

Exit fullscreen mode

  • 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
Enter fullscreen mode

Exit fullscreen mode

Replace <public-ip-address> with your EC2 instance’s public IP.

  • Retrieve the initial admin password:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Enter fullscreen mode

Exit fullscreen mode

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!"
Enter fullscreen mode

Exit fullscreen mode

  • 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

By stp2y

Leave a Reply

Your email address will not be published. Required fields are marked *

No widgets found. Go to Widget page and add the widget in Offcanvas Sidebar Widget Area.