How to Deploy Jenkins Pipeline on AWS EC2: A Beginner's Guide

Getting Started with AWS EC2, Docker, and Jenkins

Creating an AWS Free Tier Account

To embark on your AWS journey, the initial step is to create a free tier account. After successfully setting up your account, log in to the AWS console to begin exploring the plethora of services offered.

Launching an EC2 Instance

Navigate to the EC2 service and select an Amazon Linux CLI instance, specifically the t2.micro type, which usually suffices for most basic needs without necessitating updates. Proceed with launching the instance and downloading the required files. Linux or MacOS users should obtain the .pem file, while Windows users should acquire the .ppt file (if using PowerShell or CMD).

# Example move commands:
# Adjust the paths according to your setup
mv /mnt/d/NDownloads/docker-server.pem ~/.ssh/
chmod 400 ~/.ssh/docker-server.pem

SSH Into Your EC2 Instance

With the files in place, SSH into your EC2 instance using the following command:

# SSH into EC2 instance
ssh -i ~/.ssh/docker-server.pem ec2-user@${your_public_ec2_address}

Installing Docker

Install Docker on your EC2 instance to facilitate containerization of your applications:

# Install Docker
sudo yum install docker

# Check Docker installation
docker --version

# Start Docker daemon
sudo service docker start

# Check if Docker process is running
ps aux | grep docker

# Grant permission to use Docker commands without sudo
sudo usermod -aG docker $USER

# Exit to apply changes
exit

# SSH back into EC2 instance
ssh -i ~/.ssh/docker-server.pem ec2-user@${your_public_ec2_address}

Running Docker Images

Now, let's run a Docker image on your EC2 instance. Whether it's your own project or one pulled from Docker Hub, ensure your security group's inbound rules allow access to port 3000.

# Log in to Docker
docker login

# Run Docker image
docker run -p 3000:3000 -d ${docker-username}/${docker-image}

Access your application via the provided EC2 address and port number (e.g., http://${your_public_ec2_address}:3000).

Integrating Jenkins

Integrate Jenkins into your setup to streamline your development pipeline. Configure inbound rules to include port 22 for Jenkins server access and open port 3001 as well. Ensure Docker login is executed on your CLI where AWS EC2 is located. Below is a basic example of a Jenkins file:

#!/usr/bin/env groovy

pipeline {
    agent any
environment{
docker_Image= "add ur docker hub image here"
}
    stages {
        stage('test') {
            steps {
                script {
                    echo "Testing the application..."
                }
            }
        }
        stage('build') {
            steps {
                script {
                    echo "Building the application..."
                }
            }
        }
        stage('deploy') {
            steps {
                script {
                    echo "Deploying the application..."
                           def dockerCmd ="docker run -p 3001:3001 -d ${docker_Image}"
                    sshagent(['ec2-server']){
 sh "ssh -o StrictHostKeyChecking=no ec2-user@{your_public_ec2_address} ${dockerCmd} "
                    }
                }
            }
        }
    }
}

With these steps completed, you're all set to efficiently manage your applications on AWS using Docker and Jenkins. Happy deploying!