Updated: Jul 17, 2020
This tutorial walks you through the process of deploying Jenkins an open-source Continuous Integration server. You will launch an EC2 instance, install Jenkins on that instance, and configure Jenkins to automatically spin up Jenkins build slave instances if build abilities need to be augmented on the instance.
NOTE:We will be using the Mumbai region in AWS for demonstration purposes,users can select regions based on their geography presence and this tutorial is not meant for production environments.
Set up Prerequisites:
Access to AWS account.
Create an IAM user account which has admin privileges or privileges with full access to IAM,EC2,S3,VPC services etc with access type enabled for both programmatic access and AWS management console access.( NOTE:Don’t use root credentials for any of your learning or production related activities as best practice)
STEP 1: Assuming,users have access to their AWS accounts and login using their IAM user credentials.
STEP 2: Go to AWS Console->Click Services->Click EC2 under Compute->Click Launch Instance->Select Amazon Linux 2 AMI (HVM)->64-bit->Click Next
Select t2.micro General Purpose Instance->Click Next:Configure Instance Details
You can keep all the default values as it is and ensure you are selecting default VPC as it has public subnets which allows traffic from outside world under Network field and if you have your own VPC select that VPC->Click Next :Add Storage
( Refer https://www.raghavendrakambhampati.com/post/how-to-build-a-custom-vpc-with-private-public-subnets-route-table-internet-gateway-security-gr if you want to create a new VPC with Public and Private Subnets )
Change the default volume size from 8GiB to 30GiB and keep all other values as default->Click Next: Add Tags
Add Tags to your EC2 instance to label your AWS resource and easily recognize your machine for running your commands using Key,Value details ->Click Next: Configure Security Group
Select Create a new security group->Add the Security group name and description and under type select SSH & Custom TCP as network protocol and ensure you are selecting source as My IP as it takes your current machine IP and allows incoming traffic to your EC2 instance and also add Port 8080 for the Jenkins build server to get connected..Don’t select source as anywhere 0.0.0.0/0 as this will give access to open network and allows open traffic to flow which is a potential threat to your organization.NOTE:The source IP can change when you change your network connectivity from wifi or any other network .So to ensure you have access to this instance go to the security group and click on My IP to capture your latest IP of the source machine->Click Review and Launch.
You can preview all the configuration details you have entered and Click Launch->It will ask you to select any key pair.If you have existing select the existing key pair,if not select Create a new key pair option->Enter the name of keypair->Click on Download key pair->and then Click Launch instances.
You will get a new dashboard with your instance ID details.Click on the instance id it will point to your EC2 instances page with status first as pending and then to running within a few seconds after which you can access your instance .
Once your instance is up and running, select the instance you created ->copy your Public DNS and paste into your putty or mac terminal and login with your keypairs.
NOTE:Once you download your keypair ,you will have keypair in the format of .pem and use putty based ssh-keygen to convert your .pem file to .ppk file and use this key for logging into instances using putty .If you are using mac terminal,simply run the below commands in terminal.
chmod 400 **/path/to/your/key/jenkinskey.pem ssh -i **/path/to/your/key/jenkinskey.pem ec2-user@ec2-**UNIQUE SET OF NUMBERS**.compute-1.amazonaws.com(Public DNS Name of your AWS Ubuntu EC2 Instance)
STEP 3: SSH to the AWS EC2 Linux Instance and run the below commands to Install & Configure Jenkins
Before doing the steps make sure you have java1.8 rather than default java1.7 ship with Amazon AMI:
[ec2-user@ip-10-0-1-48 ~]$ sudo yum install java-1.8.0 [ec2-user@ip-10-0-1-48 ~]$ java -version openjdk version "1.8.0_242" OpenJDK Runtime Environment (build 1.8.0_242-b08) OpenJDK 64-Bit Server VM (build 25.242-b08, mixed mode)
Add the Jenkins repo using the following command:
[ec2-user@ip-10-0-1-48 ~]$ sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo --2020-04-15 10:46:02-- http://pkg.jenkins-ci.org/redhat/jenkins.repo Resolving pkg.jenkins-ci.org (pkg.jenkins-ci.org)... 126.96.36.199 Connecting to pkg.jenkins-ci.org (pkg.jenkins-ci.org)|188.8.131.52|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 71 Saving to: '/etc/yum.repos.d/jenkins.repo' 100%[=========================================================================================================================================>] 71 --.-K/s in 0s 2020-04-15 10:46:03 (11.7 MB/s) - '/etc/yum.repos.d/jenkins.repo' saved [71/71]
Import a key file from Jenkins-CI to enable installation from the package:
[ec2-user@ip-10-0-1-48 ~]$ sudo rpm --import http://pkg.jenkins-ci.org/redhat/jenkins-ci.org.key
[ec2-user@ip-10-0-1-48 ~]$ sudo yum install jenkins -y Failed to set locale, defaulting to C Loaded plugins: extras_suggestions, langpacks, priorities, update-motd jenkins | 2.9 kB 00:00:00 jenkins/primary_db | 147 kB 00:00:00 Resolving Dependencies --> Running transaction check ---> Package jenkins.noarch 0:2.231-1.1 will be installed --> Finished Dependency Resolution Dependencies Resolved =================================================================================================================================================================================== Package Arch Version Repository Size =================================================================================================================================================================================== Installing: jenkins noarch 2.231-1.1 jenkins 63 M Transaction Summary =================================================================================================================================================================================== Install 1 Package Total download size: 63 M Installed size: 63 M Downloading packages: jenkins-2.231-1.1.noarch.rpm | 63 MB 00:00:29 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : jenkins-2.231-1.1.noarch 1/1 Verifying : jenkins-2.231-1.1.noarch 1/1 Installed: jenkins.noarch 0:2.231-1.1 Complete!
Start Jenkins as a service:
[ec2-user@ip-10-0-1-48 ~]$ sudo service jenkins start Starting jenkins (via systemctl): [ OK ]
[ec2-user@ip-10-0-1-48 ~]$ sudo systemctl start jenkins.service
[ec2-user@ip-10-0-1-48 ~]$ sudo systemctl enable jenkins.service jenkins.service is not a native service, redirecting to /sbin/chkconfig. Executing /sbin/chkconfig jenkins on
[ec2-user@ip-10-0-1-48 ~]$ sudo systemctl status jenkins ● jenkins.service - LSB: Jenkins Automation Server Loaded: loaded (/etc/rc.d/init.d/jenkins; bad; vendor preset: disabled) Active: active (running) since Wed 2020-04-15 10:48:28 UTC; 1min 12s ago Docs: man:systemd-sysv-generator(8) CGroup: /system.slice/jenkins.service └─3812 /etc/alternatives/java -Dcom.sun.akuma.Daemon=daemonized -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -jar /usr/lib/jenkins/jenkins.war --logfile=... Apr 15 10:48:27 ip-10-0-1-48.ap-south-1.compute.internal systemd: Starting LSB: Jenkins Automation Server... Apr 15 10:48:27 ip-10-0-1-48.ap-south-1.compute.internal runuser: pam_unix(runuser:session): session opened for user jenkins by (uid=0) Apr 15 10:48:28 ip-10-0-1-48.ap-south-1.compute.internal jenkins: Starting Jenkins [ OK ] Apr 15 10:48:28 ip-10-0-1-48.ap-south-1.compute.internal systemd: Started LSB: Jenkins Automation Server.
STEP 4: Configure Jenkins
Connect to http://<Public DNS of EC2>:8080 from your favourite browser to connect to Jenkins Dashboard and you will see the below screen for the first time.
As prompted, enter the password found in /var/lib/jenkins/secrets/initialAdminPassword.Use the following command to display this password->Click Continue.
[ec2-user@ip-10-0-1-48 ~]$ sudo cat /var/lib/jenkins/secrets/initialAdminPassword *******************
The Jenkins installation script directs you to the Customize Jenkins page->Click Install suggested plugins.
Once the installation is complete, it will ask us to create First Admin user->enter Credentials->click Save Credentials, and then click Start Using Jenkins.
On the left-hand side, click Manage Jenkins, and then click Manage Plugins->Click on the Available tab, and then enter Amazon EC2 plugin at the top right->Select the checkbox next to Amazon EC2 plugin, and then click Install without restart
Once the installation is done, click Go back to the top page->On the left-hand side, click Manage Jenkins, and then click Manage Nodes & Clouds->on the left-hand side, click configure clouds to configure AWS credentials for Jenkins to automatically spin up Jenkins build slave instances as needed.
Click on Add a new cloud->Select Amazon EC2
Enter Name ->Click on Add under Amazon EC2 Credentials-> A New screen will pop up -> Select AWS Credentials under Kind Field.
Enter only AWS Access Key ID and Secret Access Key details ->Click Add.Once the keys are added->Select the Region->Upload AWS EC2 private key .
Click the AWS EC2 Credentials which were filled above ->Click Test Connection and Save.