Create and configure AWS EC2 linux instance to run as Jenkins slave agent
There are situations where we need to run a Jenkins job on a remote machine e.g. EC2 instance on amazon web services. In this case we need to create a jenkins slave agent in the host machine that can run the job on the remote EC2 instance. We will create a slave agent to run ssh commands on aws from Jenkins on host machine in Part 2 of this post. In this post, we will create an AWS EC2 instance and prepare it so that we are able to run Jenkins slave agent on it.
Let us start by logging onto https://aws.amazon.com. To follow along, you will need to create a new aws account in case you don’t already have an account. You can opt for the free tier account for this example as we will create the instance available as part of the free-tier account. Once your account is created, select My Account -> AWS Management Console link from the menu in the top right corner in the home page. If you just created your aws account, you can log on with the root user. In case you have access to the aws account as IAM user, you can log in as IAM user but you should have the permissions required to create and prepare the EC2 instance which is being explained here.
In the home screen for AWS Management Console, just make a note of the region that has been selected or select the one which is nearer to your location to reduce network latency. For our example we are selecting Asia Pacific (Mumbai) ap-south-1.
Next we will goto Services->EC2 from the left top corner menu. Select EC2 from Compute section.Select Launch Instance from the Launch Instance drop down.
This will take us to the screen where we can select machine image. This image is a template containing settings for the machine such as operating system and applications.For our example we will select Ubuntu 18.04. Click on Select button to the right side of the image name.
In the next screen, we need to choose the instance type.The instance type you choose will depend on the machine capacity required. Here we can select the instance providing the required CPU, memory, storage and networking capacity. For our example we will select t2.micro below which is mentioned as “free tier available“. It is a general purpose machine providing a basic configuration which is sufficient for our example.
The next step is to configure the instance.We will keep it as default for our example.
In the next screen, you can select the storage required. Again the default storage is sufficient for our purpose.
In the next screen, you can define a tag or a key-value pair as per your requirement. As of now, we do not need to add anything here and will proceed to next screen by selecting Review and Launch.
In the next step, we are required to configure a security group. We can either use an existing one, or create a new one based on our requirement. For our example, we will create a new security group and provide ssh access which is the default configuration. It is a good practice to select the ip addresses from where this machine will be accessed via ssh. For now we will leave it as default, that means that this machine can be accessible via ssh from any ip address.
The next step is to review the instance details selected in previous steps and select Launch once reviewed.
Once the instance is launched we will be able to see it in the EC2 dashboard.To start the instance, select Actions->Instance State->Start as shown in the screen image.
We can find the public host name and the ip address once the instance has started.
Keep a note of the Public DNS (IPv4) value or the host name which we will need to connect to this instance.
Now let us access this machine from our host machine and check the connectivity.We can check connect via puTTY or any other tool being used to ssh into a remote machine.
For this we will need the private key which we have downloaded while creating the ec2 instance.We will use puTTY gen to convert the key into the format required by puTTY to connect to this instance.Once we have the new key in the format, required by us, we can mention the Public DNS
(IPv4), also called the host name that we have copied from ec2 instance details..
We can also mention the ip address instead of host name which is also available next to host name from ec2 instance details.
Jenkins requires JDK to be available on the instance where the linux slave node will be run.
If you were to run Jenkins slave node on Ubuntu instance without installing JDK, you would find error similar to the following error when you try to start the slave node.
java.io.IOException: Java not found on hudson.slaves.SlaveComputer@1c042b7. Install a Java 8 version on the Agent.
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Launch failed – cleaning up connection
[SSH] Connection closed.
Once you are able to connect via puTTY, you can install JDK from the command prompt.
We will install the default OpenJDK version 11 which is available with Ubuntu 18.04, but you can install the package of your choice. Run the following commands from the command prompt
$ sudo apt install default-jdk
The next command is to verify the installed jdk version.
$ javac -version
Now you are ready to start the slave node that will enable ssh onto this instance.
We will discuss how to setup the slave node and run jenkins job on the instance we have created in Part2 of this post.