Introduction

In the previous blog, we discussed what JMeter is, when to use it, how to set up a JMeter on a machine (server), and much more. However, setting up the server from scratch every time is not practical. Additionally, there are server resource limitations. For example, if we need to test a large load, the server’s resources, such as memory and vCPUs, must be high; otherwise, the test won’t run effectively. To address this issue, I have come up with a better and simpler solution: using AWS ECS. This approach is also much more straightforward than setting up JMeter on EC2.

Why use AWS ECS?

Amazon Elastic Container Service (ECS) provides a scalable and efficient way to manage Docker containers, making it ideal for running JMeter load tests. ECS handles the orchestration for JMeter, making it easy to scale your load-testing infrastructure up or down based on your needs without worrying about the underlying server resources.

Benefits of using AWS ECS for JMeter

  1. Scalability: Easily scale the number of load generators by deploying multiple containers.
  2. Efficiency: Automate the deployment process and reduce setup time.
  3. Cost-Effective: Pay only for the resources you use, avoiding the cost of over-provisioning.
  4. Integration: Seamlessly integrates with other AWS services like S3 for storing test plans and results.

High-Level Steps

  1. Create a Docker Image: Build a Docker image that includes JMeter and AWS CLI.
  2. Upload Test Plans to S3: Once you include JMeter and AWS CLI and build the docker image, store your JMeter load test plans in an S3 bucket.
  3. Set Up AWS ECS: Create an Elastic Container Service (ECS) cluster and task definition to run the Docker container.
  4. Run the Load Test: Execute the ECS task to run the JMeter test, download the load testing plan from S3, and upload the results back to S3.

Prerequisites

  1. Knowledge of Docker and AWS Elastic Container Service (ECS)
  2. Knowledge of AWS Elastic Container Repository
  3. Knowledge of ECS Cluster
  4. Knowledge of ECS Task Definition

Step 01: Create a Docker Image

Below are the Sample Docker Image and ENTRYPOINT script,

Dockerfile

FROM openjdk:11

# Install JMeter
RUN wget https://dlcdn.apache.org//jmeter/binaries/apache-jmeter-5.6.3.tgz && 
    tar -xzf apache-jmeter-5.6.3.tgz && \
    mv apache-jmeter-5.6.3 /opt/jmeter && \
    rm apache-jmeter-5.6.3.tgz

# Install AWS CLI
RUN curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" && \
    unzip awscliv2.zip && \
    ./aws/install && \
    rm -rf awscliv2.zip aws

# Add JMeter to PATH
ENV PATH $PATH:/opt/jmeter/bin

# Copy entrypoint script into the container
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

# Set the entrypoint
ENTRYPOINT ["/bin/bash", "./entrypoint.sh"]

entrypoint.sh

#!/bin/bash
set -e

# Define variables
S3_BUCKET=jmeter-load-testing
TEST_PLAN_PATH=testing_plans/cloudzenia.jmx
RESULT_FILE=result-$(date +%Y%m%d%H%M%S).jtlRESULT_S3_PATH=testing_results/

# Download the JMeter test plan from S3
echo "Downloading test plan from S3..."
aws s3 cp s3://$S3_BUCKET/$TEST_PLAN_PATH /opt/jmeter/test_plan.jmx

# Run JMeter test
echo "Running JMeter test..."
jmeter -n -t /opt/jmeter/test_plan.jmx -l /opt/jmeter/$RESULT_FILE

# Upload the results back to S3
echo "Uploading results to S3..."
aws s3 cp /opt/jmeter/$RESULT_FILE s3://$S3_BUCKET/$RESULT_S3_PATH

echo "Test execution completed."

Note: The variables defined in the following script, such as bucket name and path, are based on my configuration; please update them to reflect yours.

Step 02: Build Docker Images and push it to AWS ECR

Place them in the same directory as the above file and begin creating the image. After that, send the image to AWS ECR. You can make use of the following command for building the image. 

docker build -t <image_name> .

Note: After building your image locally, please go to your AWS ECR repo and open push commands. Then, follow those commands to push your image into the ECR.

How to Push a Docker Image to Amazon ECR? – GeeksforGeeks

Step 03: Create an IAM Role for accessing the S3 bucket

We need this role to download and upload files from respective buckets.

After running the container, it will begin executing the script and attempt to access the AWS S3 bucket. If the task does not have the requisite permissions, the script will not be executed properly, so go to the IAM interface, establish one role and attach the policies listed below to it.

After creating the role, attach it to the task definition if not attached. However, while creating the task definition, it will, by default, create the “ecsTaskExecutation” role. You need to attach the S3 access policy.

Step 04: Link Image to Task definition

Go to the ECS console and establish a task definition using the image we created in the previous steps for TD.

If you cannot create the resources indicated in this blog, here is a blog that can assist you.

Step-05: Create an S3 bucket and Upload the JMeter Test Plan

In a previous JMeter blog, I showed you how to establish a JMeter Load testing plan. If you don’t have a JMeter load testing plan, please make one and upload it to an S3 bucket.

The below image shows the folder structure of the S3 bucket. I have uploaded my test plan to the testing_plan folder, and my load test result will be uploaded to the testing_result folder after the task execution is completed.

Step-06: Run the ECS Task

We don’t need ECS service because running the task continually will cause it to become excited, but we only want to execute the process once. Therefore, we will use a standalone task.

Check out the video below to see how the setup works.

Conclusion

You can easily automate and scale your JMeter load-testing infrastructure using AWS ECS. This method eliminates the need to set up servers from scratch and ensures you have the necessary resources to run your tests effectively.

For more details and information on AWS, cloud technologies, cloud computing, JMeter, load testing, and security, visit the CloudZenia website.

Sept 06, 2024