Project Release: Docker + AWS + Selenium + Terraform. Get it up and running.
Tags: docker, aws, selenium, terraform
Published: 2019-04-28 22:46:03

---
title: Project Release: Docker + AWS + Selenium + Terraform. Get it up and running.
published: true
description: Little side project to get Selenium Grid up and running.
tags: docker, aws, selenium, terraform
cover_image: https://blog.davidjeddy.com/wp-content/uploads/2019/04/DAST.png
---

<!-- wp:heading -->
<h2>A little background</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Recently at the office the team I am assigned to have been tasked with build a quick start tool chain for a project. This got me thinking, how easy could I start up a Selenium Grid to test a web application using some of my favorite tools? As I am also a large supporter of testing something in my brain said 'yep, do this'. So herein is how to use the project I put together using Docker, AWS, Selenium, and Terraform.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Part 1: Local Setup</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Before anything else please ensure the prerequisites are configured and functioning properly. Specifically the AWS CLI and Terraform. These two tools are core and must be functional locally. (Check with the vendor for setup instructions if needed.)</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1615} -->
<figure class="wp-block-image"><img src="https://blog.davidjeddy.com/wp-content/uploads/2019/05/dast_0.png" alt="05 - dast_0.png" class="wp-image-1615"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Once the configuration values are added initialize the Terraform configuration.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1616} -->
<figure class="wp-block-image"><img src="https://blog.davidjeddy.com/wp-content/uploads/2019/05/dast_0_1.png" alt="05 - dast_0_1.png" class="wp-image-1616"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>If everything goes as expected we should be returned to the terminal prompt with a successfully initialized Terraform project. With that completed let's go ahead and stand up the infrastructure.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1617} -->
<figure class="wp-block-image"><img src="https://blog.davidjeddy.com/wp-content/uploads/2019/05/dast_1.png" alt="05 - dast_1.png" class="wp-image-1617"/><figcaption><code>terraform plan</code> and <code>terraform apply</code> commands with suggested arguments.</figcaption></figure>
<!-- /wp:image -->

<!-- wp:image {"id":1618} -->
<figure class="wp-block-image"><img src="https://blog.davidjeddy.com/wp-content/uploads/2019/05/dast_1_1-1048x1440.png" alt="05 - dast_1_1.png" class="wp-image-1618"/><figcaption><code>terraform plan</code> resulting output.</figcaption></figure>
<!-- /wp:image -->

<!-- wp:image {"id":1619} -->
<figure class="wp-block-image"><img src="https://blog.davidjeddy.com/wp-content/uploads/2019/05/dast_1_2-1488x1440.png" alt="05 - dast_1_2.png" class="wp-image-1619"/><figcaption><code>terraform apply</code> resulting output.</figcaption></figure>
<!-- /wp:image -->

<!-- wp:heading -->
<h2>Part 2: Setting up Selenium Grid Service</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The project has two main parts. A Selenium Grid host and a Web App host. The next step will be to setup the Selenium Grid host. Lucky for us the Selenium team provides a basic <code>docker-compose.yml</code>. I took this and rolled it into the <code>./resources/selenium.sh</code> script. Ideally a software config tool would be used to transfer the config to the remote host but eh, I didn't feel like it. Anyways, each of the two hosts have two setup steps: copy the script via SCP; then execute the script by passing the command via SSH. </p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1621} -->
<figure class="wp-block-image"><img src="https://blog.davidjeddy.com/wp-content/uploads/2019/05/dast_2.png" alt="05 - dast_2.png" class="wp-image-1621"/><figcaption>The SCP and SSH command logic.</figcaption></figure>
<!-- /wp:image -->

<!-- wp:image {"id":1622} -->
<figure class="wp-block-image"><img src="https://blog.davidjeddy.com/wp-content/uploads/2019/05/dast_2_1.png" alt="05 - dast_2_1.png" class="wp-image-1622"/><figcaption>SCP copying the script onto the Selenium host.</figcaption></figure>
<!-- /wp:image -->

<!-- wp:image {"id":1623} -->
<figure class="wp-block-image"><img src="https://blog.davidjeddy.com/wp-content/uploads/2019/05/dast_2_2-1670x1440.png" alt="05 - dast_2_2.png" class="wp-image-1623"/><figcaption>The script execution ending.</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>If you are familiar with docker-compose the last few lines should look familiar to you as well. What we see here is the docker-compose.yml being processed and the services being started. As this point we have a fully functional Death Sta...er... Selenium Grid. If we visit the public DNS of the machine the Selenium Grid (as provided from the terraform apply output) the web console should load. (NOTE: Selenium Grid is NOT secure. It has NO access controls nor authentication. Security should be setup in front of any public Grids.)</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1631} -->
<figure class="wp-block-image"><img src="https://blog.davidjeddy.com/wp-content/uploads/2019/04/dast-selenium-grid-web-console.png" alt="" class="wp-image-1631"/><figcaption>Ignore the localhost address, this image is 'borrowed'.</figcaption></figure>
<!-- /wp:image -->

<!-- wp:heading -->
<h2>Part 3: Setting up Web App Service</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Now that the Grid is up and running time to start up the Web App. For this project I used the Java Spring Boot Pet Clinic web app. It seemed a logical choice since 1) many people are familiar with it and 2) writing a Selenium test case was similar to other tools I have used in the past. So lets get it going here.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1624} -->
<figure class="wp-block-image"><img src="https://blog.davidjeddy.com/wp-content/uploads/2019/05/dast_3.png" alt="05 - dast_3.png" class="wp-image-1624"/><figcaption>Commands are very similar to setting up the Selenium Grid</figcaption></figure>
<!-- /wp:image -->

<!-- wp:image {"id":1625} -->
<figure class="wp-block-image"><img src="https://blog.davidjeddy.com/wp-content/uploads/2019/05/dast_3_1.png" alt="05 - dast_3_1.png" class="wp-image-1625"/><figcaption>Looks familiar...</figcaption></figure>
<!-- /wp:image -->

<!-- wp:image {"id":1626} -->
<figure class="wp-block-image"><img src="https://blog.davidjeddy.com/wp-content/uploads/2019/05/dast_3_2.png" alt="05 - dast_3_2.png" class="wp-image-1626"/><figcaption>Up and running, nice!</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Unlike the Selenium setup this takes a bit longer due to the web app needing to compile and start up. After a some moments though visiting the public DNS should result in the Pet Clinic app loading in the browser.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1632} -->
<figure class="wp-block-image"><img src="https://blog.davidjeddy.com/wp-content/uploads/2019/04/spring-pet-clinic-home.png" alt="" class="wp-image-1632"/><figcaption>Look at them cute animals! :cuteness:</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Nice! Everything is up and running. So far, so good.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Part 4: Execute!</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>With everything up and running the only thing left is to trigger the Selenium browser test. Here is where things get a bit confusing. The test case/s logic is executed on the Web App instance (not a good idea in production systems) and connects to the Selenium Grid's hub instance. The Hub service receives the test request capabilities and routes it to an appropriate browser node. In our case Chrome on Linux. The execution command is a bit long in the tooth but it does not require use to hard any values manually. This is important for automation when integrating the tools into a CI / CD pipeline.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1627} -->
<figure class="wp-block-image"><img src="https://blog.davidjeddy.com/wp-content/uploads/2019/05/dast_4.png" alt="05 - dast_4.png" class="wp-image-1627"/><figcaption>Passing the execution command via SSH to the Web App.</figcaption></figure>
<!-- /wp:image -->

<!-- wp:image {"id":1628} -->
<figure class="wp-block-image"><img src="https://blog.davidjeddy.com/wp-content/uploads/2019/05/dast_4_1-1211x1440.png" alt="05 - dast_4_1.png" class="wp-image-1628"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Boom, goes the dynamite! A successfully executed browser test.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Selenium is a massively feature and even take screen captures of each step executed; tests can be written in Java, Javascript, Python, and many other languages. <a href="https://www.seleniumhq.org/docs/07_selenium_grid.jsp">Seriously, take a look at the docs</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Step 5: Taking it all down</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Like all good things this to must end. Since this is all put together with Terraform it can be taken down even more quickly than it took to start up. Excellent for temporary environment or proof of concepts.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":1629} -->
<figure class="wp-block-image"><img src="https://blog.davidjeddy.com/wp-content/uploads/2019/05/dast_5.png" alt="05 - dast_5.png" class="wp-image-1629"/></figure>
<!-- /wp:image -->

<!-- wp:image {"id":1630} -->
<figure class="wp-block-image"><img src="https://blog.davidjeddy.com/wp-content/uploads/2019/05/dast_5_1.png" alt="05 - dast_5_1.png" class="wp-image-1630"/></figure>
<!-- /wp:image -->

<!-- wp:heading -->
<h2>Bonus: Request / Response Diagram</h2>
<!-- /wp:heading -->

<!-- wp:image {"id":1611} -->
<figure class="wp-block-image"><img src="https://blog.davidjeddy.com/wp-content/uploads/2019/04/Docker-AWS-Selenium-Tefferaform.png" alt="" class="wp-image-1611"/><figcaption>Not to complicated when you see like this.</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Hope you found this as informative to read as it was for me to put it together. It shows just how flexible and capable automation tools can be to get things up and running as well as manageable with minimal of effort.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Resources</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>GitHub Repository: <a href="https://github.com/davidjeddy/docker_aws_selenium_terraform">https://github.com/davidjeddy/docker_aws_selenium_terraform</a></li></ul>
<!-- /wp:list -->