Selenium script execution in AWS EC2 instance using selenium grid and docker containers
Tags: awsugncr, selenium, aws, docker
Published: 2023-10-18 16:24:01

> _**☀️ In this use case, we will see how to execute the selenium script execution inside AWS EC2 instances using Selenium grid and docker image.**_

**<u>_Flow Diagram:_</u>**


![flow diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7dvhhkjswgs2lnld9xv4.gif)



🦋 **<u>EC2 instance creation with preinstalled docker amazon Linux AMI</u>**

✏️ Go to AWS Management console, In AMI search for an 'docker' and select one of the Linux AMI where docker already been pre-installed as below.

![Linux AMI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iwoe010rpkzdrugl7ews.png)

✏️ Create a new key-pair in order to connect with EC2 instances. If you are using putty please make you are selecting the .ppk file

![key-pair](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gy0j8gb5ygbqirn77ek5.png)

✏️ In Network settings, make sure you add the inbound security group with port 4444 and 7900 as below

![Inbound security group](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jvzew4k5049gfiuw34nn.png)

✏️ Once you click create instances make sure your instance is up and running

![EC2 running state](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z8u3s8dhchobrhcjkffn.png)

✏️Click on connect option using SSH client as below

```
ssh -i "vj.pem" ec2-user@<ipv4 public dns>.compute-1.amazonaws.com
```
You can either use SSH Client via putty agent or windows powershell. Once you are connected you will get as below

![EC2 instance connected via SSH](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/chhuhgd4bw1rthdtlppk.png)

🦋 **_<u>How to install the docker in amazon EC2 instance if not installed</u>_**

🐳 Consider that if selected AMI doesn't contain any docker. You can install the docker by using below commands

```
sudo yum update
sudo yum install -y docker
sudo usermod -a -G docker ec2-user
sudo service docker start
sudo chkconfig docker on
```

![docker install](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qgofstlaejl0884gnj7i.gif)


🐳 You can check for docker version using below command

```
docker --version
```

![docker --version](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iqv2a05gw6mgpk5884g9.gif)

🦋**<u> How to pull the selenium standalone chrome image from docker hub</u>**

👨🏻‍💻 In order to run your webdriver tests remotely. You need to pull and install the latest standalone selenium chrome version as below

```
docker run -d -p 4444:4444 -p 7900:7900 selenium/standalone-chrome:latest
```
 
![selenium standalone chrome installation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b5afzgav0ah9ybcw71h4.png)

![standalone chrome](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sbzd6ni2mlhu8ftoriil.gif)


👨🏻‍💻 To check containers are up and running. Enter the below command

```
docker ps -a
```

![container with chrome](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/waug58jiwopd4xq82pdq.png)

👨🏻‍💻 Similarly, if you need install the latest standalone firefox version. You can use the below command

```
docker run -d -p 4444:4444 -p 7900:7900 selenium/standalone-firefox:latest
```

![selenium standalone firefox installation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t5ukcmkpidnvhqum63si.png)

👨🏻‍💻 Copy the IPV4 or public IPV4 address with colon 4444, open it in a new tab

![selenium grid with chrome](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rf4vli1pp1zr3kt7ke3w.png)

👨🏻‍💻 You can check how many images installed using below docker command

```
docker images
```

![docker images](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uxi9i6wf3b3zsf1pzafd.png)

👨🏻‍💻If you wish to install the docker compose to scale up the number of nodes. Below is the command

```
sudo curl -L https://github.com/docker/compose/releases/download/1.21.0/docker-compose-`uname -s`-`uname -m` | sudo tee /usr/local/bin/docker-compose > /dev/null
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version
```

🦋 <u>**_Another way - Creation of selenium grid with docker compose yml file_**</u>

✏️ Create the docker-compose yml file using below command

```
touch docker-compose.yml
```

✏️ Edit the newly created docker compose yml file in visual editor by pressing i in your keyboard. Enter the below yml file and quit using :wq!

```
version: "3"
services:
  chrome:
    image: selenium/node-chrome:4.14.1
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443
      - SE_NODE_MAX_INSTANCES=3
  edge:
    image: selenium/node-edge:4.14.1
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443

  firefox:
    image: selenium/node-firefox:4.14.1
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443

  selenium-hub:
    image: selenium/hub:4.14.1
    container_name: selenium-hub
    ports:
      - "4442:4442"
      - "4443:4443"
      - "4444:4444"
```

✏️ Enter the following command to make the docker up and running

```
docker-compose up -d
```

✏️ You will get below output once done with your installation


![installation of docker compose](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rnvxcbgo1h27igf48iqy.png)

✏️ You can check all the docker containers running by using the below command

```
docker ps -a
```

![container ids](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tbp6p4jjrdxk801g80ve.png)

🦋 **<u>Checking the selenium grid using EC2 IPV4 address and port 4444</u>**

⭐ Checking the selenium grid using IPV4 public ip address and port 4444

![selenium grid](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lvsshqophvx5h0oz68j0.png)

🦋 **<u>Configure your EC2 IPV4 instance address in your execution script as a hub</u>**

☀️ You can check the current execution and queue size in your selenium grid as below

![queue size](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/obmgkv6fnsvehwn4dfty.png)

 
![containers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wf3332xoij0i7ehndm0m.png)

☀️ You can increase your queue size using below command

```
docker compose -f docker-compose-v3.yml up --scale chrome=2
```

☀️ Below are the changes to be done in your driver configuration files

```
public void user_login_page() throws MalformedURLException {
 WebDriverManager.chromedriver().setup();
 DesiredCapabilities capabilities=new DesiredCapabilities();
 capabilities.setBrowserName("chrome");
 driver = new RemoteWebDriver(new 
 URL("http://52.66.250.166:4444/wd/hub"), capabilities);
 driver.get("https://selectorshub.com/");
}
```

☀️ You can tear down the container using below command

```
docker-compose down
```

☀️ Please make sure terminate your ubuntu instances created to avoid costs

![ec2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tnelf0adc2s23bk7l49u.png)


Thanks for being patient and followed me. Keep supporting 🙏

Clap👏 if you liked the blog

For more exercises — pls do follow me below ✅!

https://www.linkedin.com/in/vijayaraghavanvashudevan/

#aws #awscommunitybuilder #awsreskill #awsec2 #selenium #docker #ubuntu #selenium-grid #docker-compose #cross-broswer #awsugmdu #awsugncr #cloudnloud #awsugncrdelhi