DEV Community

Cover image for Deploy JMeter on AWS using Terraform
NaveenKumar Namachivayam ⚡ for AWS Community Builders

Posted on • Originally published at qainsights.com

Deploy JMeter on AWS using Terraform

Maintaining JMeter infrastructure for performance testing, CI/CD integration with the enterprise pipeline, and managing are cumbersome tasks. By leveraging the Infrastructure as Code solution, Terraform is one of the excellent ways to build, manage, and deploy JMeter infrastructure quickly and efficiently. In this blog, we are going to dive deeper into spinning up an AWS infrastructure with JMeter using Terraform.

What is Infrastructure as Code?

Infrastructure as Code (IaC) helps to build, change, and version infrastructure safely and efficiently.

What is Terraform?

Terraform is an IaC tool from HashiCorp. It comes with three flavors: CLI, Cloud, and Enterprise. Terraform's mantra is: Write, Plan, and Apply.

Terraform is an open-source infrastructure as code software tool that provides a consistent CLI workflow to manage hundreds of cloud services. Terraform codifies cloud APIs into declarative configuration files.

This blog post will not enlighten you about Terraform. But it focuses on how to deploy the vanilla JMeter and JMeter plugins on AWS.

Prerequisites

The following are the prerequisites required to deploy JMeter on AWS using Terraform.

  • AWS Console access to create relevant IAM roles, access keys, and secrets
  • Terraform CLI

AWS Setup

Key Pair

To access EC2 instances, we need to create a key pair in AWS. To create a new key pair, follow the below steps.

  • Log into AWS console
  • Navigate to EC2 -> Key Pairs
  • Create a new key pair w/ RSA and Private Key format (PEM)
  • Save the private key to a file in a secure location

Creating a new key pair on AWS
Creating a new key pair on AWS

IAM User

To manage the AWS resources on Terraform, it is recommended to create a new IAM user with Access key credential type. To create a new IAM user, go to:

  • IAM Dashboard on AWS
  • Click Users -> Add users
  • Enter valid user name and select Access key - Programmatic access.
  • Click Next: Permissions
  • Select Attach existing policies directly
  • Check AdministratorAccess or AmazonEC2FullAccess
  • Click Next: Tags. Enter the appropriate tags.
  • Click Next: Review
  • Click Create user

Store the Access Key ID and secret access key in a secured location. We need this info to configure it into AWS CLI.

AWS CLI

Download the AWS CLI from https://aws.amazon.com/cli/

Based on your operating system, launch the AWS CLI program and validate the version.

aws --version

To configure AWS CLI, enter aws configure and press the enter key in the terminal. Enter the access key, secret, and other required details and configure it properly.

Terraform

To download Terraform, head to https://www.terraform.io/downloads and follow the instructions to download it for your operating system. In this demo, I am going to use Ubuntu 20.04 in WSL.

To validate the Terraform installation, enter terraform --version.

To install, auto complete terraform -install-autocomplete

To spin the EC2 instances for JMeter, make a new directory mkdir JMeter-AWS-Terraform. We are going to use this directory to write some basic HCL. Terraform uses a declarative language called HashiCorp Configuration Language, which tells Terraform how to manage the resources.

JMeter on AWS using Terraform

Terraform Write

Inside JMeter-AWS-Terraform , create a new file main.tf.

Copy and paste the below Terraform module into main.tf.

IMPORTANT: We are going to spin t2.small instance type in this demo which is NOT under FREE TIER.

module "jmeter" {
  source  = "QAInsights/jmeter/aws"
  version = "1.1.2"

  aws_ami           = "ami-001089eb624938d9f"
  aws_instance_type = "t2.small"
  aws_key_name      = "terraform"
  jmeter_version    = "5.4.3"
  jmeter_plugins    = ["jpgc-casutg"]
}

The above configuration leverages the Terraform module which I have published in the Terraform registry. It expects four inputs: ami, instance type, key name, and JMeter plugins.

JMeter Terraform Module

<!-- /wp:button -->

By default, it installs JMeter 5.4.3 and other variables.

To get started, enter terraform init

This will download the Terraform plugins, modules, and other dependencies.

Terraform Plan

The next step is to plan the resources. The output of the below command will help us to see the resources' that will get created or changed.

Enter terraform plan

Here is the partial output of plan command.

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # module.jmeter.module.jmeter_server.aws_instance.this will be created
  + resource "aws_instance" "this" {
      + ami                                  = "ami-001089eb624938d9f"  
      + arn                                  = (known after apply)      
      + associate_public_ip_address          = (known after apply)      
      + availability_zone                    = (known after apply)      
      + cpu_core_count                       = (known after apply)      
      + cpu_threads_per_core                 = (known after apply)      
      + disable_api_termination              = (known after apply)      
      + ebs_optimized                        = (known after apply)      
      + get_password_data                    = false


...
...
...

Plan: 1 to add, 0 to change, 0 to destroy.

Terraform Apply

The last step in provisioning the infrastructure on AWS is to send terraform apply

It will prompt you to review and enter yes to start provisioning. For automation purpose, you can use terraform apply --auto-approve.

Terraform will make changes to the AWS infrastructure based on the current state. As we are spinning up the new instances, after less than a minute, a new EC2 instance will be up and running.

Launch the EC2 Instances page, navigate to the Ohio region where you can see an EC2 instance is up and running.

Deploy JMeter on AWS using Terraform
Deploy JMeter on AWS using Terraform

Copy the public IP address of that instance to ssh into it.

ssh -i <pem-key> ec2-user@<public-IP-address>

JMeter has been installed in the home directory. To validate it, enter `jmeter -v`

JMeter 5.4.3 on AWS
JMeter 5.4.3 on AWS

The Terraform module also installs JMeter plugin jpgc-casutg. This can be configured in the main.tf file.

To validate, sudo cat /var/log/cloud-init-output.log to view the log.

Within a few minutes, you have an EC2 instance with Java, JMeter, and JMeter plugins for performance testing. Terraform allows us to configure all the parameters under the hood, e.g. AWS region, VPC, instance types, and more. This Terraform module uses a lot of default values for AWS. If you are looking for anything specific to configure, please let me know in the GitHub repo.

JMeter Terraform Module Repo

<!-- /wp:button -->

In this example, we have spun up a t2.small type. If you keep running, your AWS bill is going to hit the roof.

To destroy the resources, enter terraform destroy --auto-approve. CAUTION: This command is non-reversible.

Within a minute, EC2 instance will be deleted.

Conclusion

Integrating performance tests in the enterprise pipeline is crucial for shift-left and adopting DevOps practice. Managing the infrastructure for performance tests is time-consuming and error-prone. By leveraging IaC tools like Terraform, it is easy and quick to spin up the resources and destroy them once the need is done. In the next blog post, we are going to see how to deploy JMeter distributed load test infrastructure on AWS using Terraform.

Top comments (0)