How to Set Up Jenkins on AWS: A Step-by-Step Guide

How to Set Up Jenkins on AWS: A Step-by-Step Guide

Jenkins is a popular open-source automation server that helps streamline your software development and deployment pipelines. In this guide, we’ll walk through setting up Jenkins on AWS, step by step.


Prerequisites

  1. AWS Account: Ensure you have access to the AWS Management Console.

  2. SSH Key Pair: Generate a key pair or use an existing one for EC2 instance access.

  3. Security Group Rules: Allow HTTP (port 8080) and SSH (port 22) in your security group.

  4. Basic AWS CLI Knowledge (Optional): If you want to automate steps.


Step 1: Launch an EC2 Instance

  1. Navigate to EC2 Dashboard:

    • Go to the AWS Management Console > EC2 > Instances > Launch Instances.
  2. Choose AMI:

    • Select Ubuntu Server 20.04 LTS (free-tier eligible).
  3. Instance Type:

    • Choose t2.micro (free-tier eligible).
  4. Configure Instance Details:

    • Leave default settings or customize as needed.

    • Ensure the instance is in a public subnet.

  5. Add Storage:

    • Default storage (8 GB) is sufficient for Jenkins.
  6. Configure Security Group:

    • Add the following inbound rules:

      • SSH: Port 22, Source: My IP.

      • Custom TCP Rule: Port 8080, Source: 0.0.0.0/0 (for Jenkins web access).

  7. Review and Launch:

    • Review your settings and click Launch.

    • Select your key pair or create a new one, then launch the instance.


Step 2: Connect to the EC2 Instance

  1. Open your terminal or SSH client.

  2. Run the following command to connect:

     ssh -i "your-key.pem" ubuntu@<EC2_PUBLIC_IP>
    

    Replace <EC2_PUBLIC_IP> with the public IP of your instance.


Step 3: Install Jenkins

  1. Update System Packages:

     sudo apt update && sudo apt upgrade -y
    
  2. Install Java (Required for Jenkins):

     sudo apt install -y openjdk-11-jdk
    
  3. Add Jenkins Repository:

     curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.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
    
  4. Install Jenkins:

     sudo apt update
     sudo apt install -y jenkins
    
  5. Start Jenkins Service:

     sudo systemctl start jenkins
     sudo systemctl enable jenkins
    

Step 4: Access Jenkins Web Interface

  1. Open a web browser and navigate to:

     http://<EC2_PUBLIC_IP>:8080
    
  2. Unlock Jenkins:

    • Find the initial admin password:

        sudo cat /var/lib/jenkins/secrets/initialAdminPassword
      
    • Copy the password and paste it into the web interface.

  3. Set Up Jenkins:

    • Install recommended plugins.

    • Create an admin user.

    • Configure instance URL (use the public IP).


Step 5: Secure Your Jenkins

  1. Enable Security:

    • Use IAM roles for EC2 to manage AWS resources securely.

    • Configure HTTPS using a reverse proxy (e.g., Nginx or Apache).

  2. Backup Configurations:

    • Use plugins like ThinBackup for periodic Jenkins backups.

Step 6: Install Essential Plugins

  1. Go to Manage Jenkins > Manage Plugins.

  2. Recommended plugins:

    • Git: For SCM integration.

    • Pipeline: To create Jenkins pipelines.

    • Docker: If you use Docker for builds.

    • Credentials: To manage secrets.


Step 7: Create Your First Job

  1. Click New Item in Jenkins.

  2. Enter a name, select Freestyle project, and configure the project settings.

  3. Add build steps, such as running shell commands or pulling from a Git repository.


Step 8: Automate Jenkins with AWS (Optional)

  • IAM Role for EC2 Instance: Attach an IAM role with necessary permissions to access AWS resources.

  • S3 for Backup: Configure backups to S3 using AWS CLI or plugins.


Conclusion

You now have a fully functional Jenkins instance running on AWS, ready to automate your CI/CD pipelines. Jenkins provides extensive customization with plugins and can integrate seamlessly with tools like Docker, Kubernetes, and AWS services.

Start building pipelines and automate your workflows for enhanced productivity!