Wednesday, 22 July 2020

Launch AWS EC2 instances as Jenkins Slaves using EC2 plugin

In Previous topic we have triggered Jenkins Job from the Github project, Now we shall check to launch EC2 instances as Jenkins slaves from the master Jenkins when there is a need.

The Scenario is , when we need to trigger more than one Jenkins job in different machine , we usually opt for Master and Slaves Jenkins scenario.What if those slaves need to be run in different servers to control the build load or sharing the load of master.

In that case we will be needing more than one server to trigger the Jenkins build, So,to launch Server (EC2 instance) as a Jenkins slave , we have a managed plug-in called "EC2-Plugin", lets see how we can make use of this.

Lets create a IAM Role with read access to create a EC2 instances when in need.
For this , search for IAM --> click on Roles



Click on Create Role

Select EC2 Role


Search for EC2FullAccess




Now , Lets attach this role to the Jenkins EC2 instance which is running already.


Attach the role like below



Now lets create s security group to the Slave EC2 instance we are going to launch .

Select the SSH and select Source as maser jenkins Security.
It means that the instance we are running now treating as master and Security group which is attached here is "Full", See the below



then "click on Security group".

Now , Lets go to Jenkins Dash board and install EC2 plugin.



Now the plug in installed

Now go to manage Jenkins-->Configure system-->




If you want to give full access as a your EC2 instance(master) to slave you can select jenkins as below



Then give a Access Key and Secret access key of your root account in the below page




But In this case , I don't want to give that root access to my EC2 instance which am going to launch as Slave.

here We need to select as below and enter Private key of Master EC2 instance because you are going to connect from Mater to Slave by using this Key.

Once you paste the private test the connection and it should come as "Success".



Now we can add AMIs 
 To get the AMI ID of master , we can get as below




Make sure that , AMI are in the same region , because AMIs are region specific.

Select Security group Name which we created above as sg_jenkins_slave for the slave.

Label for the Jenkins job: AWS-Slave  and , auto termination time give as 10 mins.


Give Tag Name for the Jenkins Slave for easy recognition , if you want give how many ec2 instances you would like to launch for the field : Initial Cap., Now am in need of only one so, am not entering any value and check on associate Public-IP for the same and Finally click on SAVE button .

Now Lets create a new job and check whether its is launching EC2 instance or not.






Add a simple shell script for Build and click on save.


Now Run this job , once its running it should create EC2 instance as a slave  and as per our setup it should terminate after 10 mins.



Now you can refresh EC2 instances page and can see Slave instances are launched.



See the Console output which we gave it in our shell script.



Wait for 10 mins , then slave instance should terminate automatically.
Check the below 



So we have successfully lunched Slave EC2 instance where we can run Jenkins job on it.



No comments:

Post a Comment