Getting started with Aerokube Selenoid on AWS EC2 Linux

Girish Nair
CodeX
Published in
9 min readJul 25, 2022

Selenoid is a powerful Golang implementation of the original Selenium hub code. It uses Docker to launch browsers.

You can read more about Selenoid here

In this article, we will primarily explore how can we install Selenoid in an Amazon EC2 Linux instance and set up a Selenium Grid in AWS for your cross-browser compatibility tests, let’s get started.

Step 1: Creating an AWS EC2 Linux Instance

Pre-Requisites: You will need an AWS account, you can navigate to the Signup page to create an account.

Post signup, login to the AWS Management Console and navigate to AWS EC2 which is a compute service that can be used to create virtual machines. They offer a wide range of options starting with Linux, Ubuntu, Windows, and macOS as well. In our case, we will be using a Linux-based instance. One thing to note is that EC2 instances are Regional services, and you can create multiple instances in a single Region or across Regions. You can check which Region you are currently selected based on the dropdown that is available on the console page.

AWS EC2 Dashboard

You can select a Region that is close to your geographical location and proceed with EC2 instance creation.

Click on the button “Launch Instance” that is available in the EC2 dashboard, you will be navigated to the below page.

Creating an EC2 instance using AMIs

For demo purposes, we will select any available “Amazon Linux 2 AMI” machine this will redirect us to the “Choose an Instance Type” page.

Choose Instance Type

In our case, we will go ahead and select a t2.medium instance type. Post your installation and hands-on you can terminate the t2.medium instance so that do you not incur too many costs. As these are on-demand instances you will be billed based on usage and generally, it won’t be that heavy on your pocket. We can also select a t2.micro instance type which comes under “Free tier eligible” but you might notice some slowness using this instance type as the no of vCPUs and Memory size is comparatively less than other instances.

Click on Configure Instance details button, in this page you can select the default options by default AWS will allocate you a VPC (Virtual Private Cloud), subnets, etc. You can click Add Storage to move to the next page.

On the “Add Storage” page you can keep the default storage value as 8GIB or maybe increase it slightly to 10 or 12 GiB. Since Selenoid will pull many docker images for Firefox, Chrome & Opera browsers this can take up quite a bit of space. So, it will be better if you adjust the storage accordingly for your instance type to avoid any space issues. In my case, I have kept it to 10GiB.

Next, you can add or skip the “Add Tags” page and move to the “Configure Security Group” page.

Security Group: This is the most important step that needs to be updated correctly for you to access your Selenoid Hub URL & Selenoid UI page.

You can create a new Security Group and Add the below-mentioned Rules as per the snapshot added. Note that I have edited the Image to remove My IP details, in your case if you select the “My IP” dropdown option available under Source, your IP details will be automatically populated. If you want your instance to be accessible from any IP you can also select Source: “Anywhere”

You will need to expose the following ports for Selenoid:

22 — SSH port which is used to connect to the EC2 instance
4444 — This is the Selenium HUB URL port
8080 — port on which you can access Selenoid UI

Configure Security Groups

You can optionally give a name and Description for your Security Group which will be helpful during the creation of any new instances in the future as you also have an option to select a previously created security group.

Finally, we are the last step of Instance creation, you will need to click on the button “Review and Launch” this will open the “Review” page you can just verify and confirm the details and options selected, post which you can click on the “Launch” button to launch the EC2 instance.

On clicking the Launch button, you will see the window for “Selecting or Creating a new key pair” as shown below.

Select or Create EC2 key pair

Since this is a fresh setup, you can go ahead and select the option “Create a new key pair” in the first dropdown. Provide some relevant “Key pair name” in the textbox and click on the Download Key Pair button. This will download a .pem file which can be used to SSH and login to your EC2 instance.

Download Key Pair and launch the instance

Click on the “Launch Instance” button, this will launch your AWS EC2 Linux instance. You will need to wait for a couple of minutes for the instance to be fully up and running.

EC2 Running State

Step 2: Connecting to created AWS EC2 instance

There are multiple ways to log in to an EC2 instance, in this article, we will use the downloaded .pem file to log in and access our EC2 instance.

  1. Open your command prompt window or mac terminal and change the directory with the command cd to where you have downloaded the .pem file. In my case, the .pem file is stored in the downloads folder, so I will just execute
cd Downloads

2. Next, for performing SSH to the instance, we can refer below command template

ssh -i fileName.pem ec2-user@ec2-public-ip

As my .pem file is named: Selenoid_Key.pem and my ec2-public-ip is 43.205.128.212 for me the command which I will need to execute should look something like as mentioned below. (Note: This will change based on your setup as your .pem fileName and public IP address will be different)

ssh -i Selenoid_Key.pem ec2-user@43.205.128.212

3. On entering the above command and hitting ENTER, a question prompt will be displayed to add the host to your known_hosts file. just type yes and hit Enter key. You should be logged in to your instance.

Post login to the EC2 instance, the first command that you need to execute is to update all the available existing dependencies and libraries in the machine. You can do it using the below command.

sudo yum update -y

Step 3: Installing Docker in AWS EC2 instance

Verify if Docker is already installed in your EC2 instance by just typing “docker” and pressing Enter Key, if you get a whole bunch of details like the snapshot below it implies that Docker is already installed in your EC2 instance. You can skip the Docker installation step and move ahead.

Else you might need to install Docker, using the below command:

sudo yum install docker -y

Post installation, you might need to start the Docker service, you can execute the below command to start the Docker service.

sudo systemctl start docker

You can verify Docker is running using the below command, you should be able to see Active (running)

sudo systemctl status docker
Docker Service Active

Step 4: Installing Selenoid and Selenoid UI

First, we will need to download Configuration Manager, here I am using version 1.8.1 for Linux releases

sudo wget “https://github.com/aerokube/cm/releases/download/1.8.1/cm_linux_amd64

Next, we will have to give execution permissions to the Configuration Manager binary that we have downloaded using the wget command.

sudo chmod +x cm_linux_amd64

Start Selenoid using the below command, this might few minutes as it will download all relevant selenoid images and different browser images Chrome, Firefox, and Opera.

sudo ./cm_linux_amd64 selenoid start –vnc

Add ec2-user to Docker group

usermod -aG docker ec2-user

Start Selenoid UI

./cm_linux_amd64 selenoid-ui start

Post, executing all the above commands you can verify if all the images have been pulled in successfully using the below command.

docker images
Selenoid Images Pulled

Along with aerokube/selenoid, aerokube/selenoid-ui, selenoid/ video-recorder images you should be able to see the following browser images: Firefox, Chrome, and Opera with different tags or versions.

Check all Running containers using the below command

docker ps
Selenoid Containers

Step 5: Accessing Selenoid UI from the browser

You can access the Selenoid UI from your browser, for this you will need to navigate to the URL: http://43.205.128.212:8080

Here the IP address: 43.205.128.212 is my Public IP for the EC2 instance that was created, in your case public IP will be different. In the EC2 Security Group, we have allowed traffic on port 8080 by adding the TCP Rule so we should be able to access Selenoid UI using the Public IP address and port 8080.

Selenoid UI

Step 6: Execution test scripts in Selenoid Grid

Finally, we are at the home stretch!! We need to use the Selenium Grid concept: DesiredCapabilities and RemoteWebDriver class wherein we need to pass in all the applicable capabilities and Selenoid grid hub URL details which will be used to connect and run Selenium WebDriver tests in the respective browser containers that are available in Selenoid.

Below is a DesiredCapabilities sample example for Java with Chrome version: 102.0 that can be referred from the Selenoid UI page. We will need to configure something similar in our test automation scripts during browser setup.

Below is a very simple way in which we can configure the details in our setup script, browser and version details are being passed from TestNG parameters. For now, I am just using TestNG BeforeMethod annotation to initialize and set all browser capabilities for Selenoid tests.

Since we have set capabilities for enableVNC and enableVideo as true, we should be able to view the running tests in Selenoid UI as well as get a video output file for all the test executions which is really helpful.

All the requests should be passed to our Selenoid Hub URL, which in our case will be http://43.205.128.212:4444/wd/hub (In your case, you can replace the IP address with your ec2-instance public IP address). Port 4444 is exposed in EC2 Security groups. From the hub, node requests will be redirected to appropriate browsers based on capabilities being passed.

Sample Test Class

View Test Scripts being executed in Selenoid

Selenoid UI — Execution

You can click on the respective browser sessions to view the scripts being executed. Post execution, the containers will be disposed of.

VNC for Tests

Videos will also be generated in the videos section which can be used for logging, collecting evidence or debugging purposes.

Post-hands-on, do make sure to terminate your running EC2 instance by going to Actions and selecting the Terminate option available under “Instance State” to avoid excessive billing for your account.

Hope this article has been helpful for you in setting up Selenoid and Selenoid UI in an AWS EC2 instance, if you have any questions or queries, do let me know.

Thanks for reading!! Cheers!!

--

--