Dec. 19th, 2018

How to Handle Dynamic AWS SigV4 in JMeter for API Testing

As a developer, you can store APIs on AWS on the Amazon API Gateway. When you need to load test these APIs, your testing script has to include the AWS Signature, which are its access credentials.  This blog post will explain how to add the SigV4 AWS signature to your Apache JMeter™ test in Groovy, so you can run API functional and load tests. Let’s get started.

 

What is SigV4?

 

Signature Version 4 is the current AWS signing protocol, and is used to authenticate information to HTTP AWS requests. AWS signatures, consisted of an access key ID and secret access key, are used for identifying who sent requests and for protecting data. You can learn how Signature Version 4 works here. Read more from this documentation on Signing AWS API Requests.

 

Creating Your JMeter Test

 

Our JMeter test will send an HTTP request to a AWS API. We will use a Groovy script in a JSR223 PreProcessor to generate the AWS Signature and then use a Regular HTTP Request to call the API.

 

Prerequisites

 

Before starting, you need to download the following JAR files and add them to JMeter:

 

Our Testing Script

 

1. Add a Thread Group

 

Add -> Threads -> Thread Group

 

2. Add User Defined Variables as a child element of the Thread Group.

 

Thread Group -> Config Element -> User Defined Variables

 

3. Complete the values for your User Defined Variables. Add the AWS access key, secret key, host, service name and region. You should obtain these from Amazon.

 

api testing with sigv4 singature

 

4. Add a JSR223 PreProcessor to your Thread Group .

 

Thread Group -> Add -> Pre Processors -> JSR223 PreProcessor

 

In this scenario, the PreProcessor will use Groovy Language.

 

5. Add code to the PreProcessor. This code is configured to create two JMeter variables with the AWS signature, which will be used to let you call you API in AWS. You can find the code here.

 

You can add the code as is, without having to change anything.

 

The code:

  • Gets the values from the user defined variables.
  • Gets the values from the HTTP Sampler we will configure in the next step.
  • Generates an AWS Date, which Amazon requires when calling the API.
  • Creates a JMeter variable with the date stored in it.
  • Calculates the AWS signature.
  • Creates a JMeter variable with an AWS authorization header.

 

These two variables can now be used to call the API on AWS and test it.

 

6. Add a HTTP Header Manager to the Test Plan

 

Test Plan -> Add -> Config Element -> HTTP Header Manager

 

The header is required by Amazon to call the APIs.

 

7. Add Header names: x-amz-date (for the AWS Date) with the value ${x_amz_date}, and Authorization (for the AWS signature) with the value ${aws_authorization}.

 

jmeter, apis, amazon, aws gateway

 

8.  Add an HTTP Request Sampler (as many as you want). These are for calling the API endpoints.

 

The server name has to be ${aws_host} and the path should be the same path of your AWS endpoint.

 

Specify the method and body according to your scenario.

 

functional api testing with aws signature

 

9. Consider adding a Cookie Manager, an HTTP Cache Manager and Timers in your script to improve the CPU.

 

Your script is ready be run!

 

10. Add a Listener and run your test.

 

This is what the full script looks like:

 

run tests on apis on aws with signature authorization

 

Running Your Test in BlazeMeter

 

That’s it! Now, you can also take your JMeter script in BlazeMeter and run it in the cloud or behind a firewall. You can scale your test, run from multiple locations, and get advanced reports for better collaboration. Try out BlazeMeter by putting your URL in the box below, to start testing now.

     
arrow Please enter a valid URL

Interested in writing for our Blog? Send us a pitch!