Logo

dev-resources.site

for different kinds of informations.

Load Testing using K6 in AWS and Terraform

Published at
7/14/2023
Categories
testing
aws
grafana
k6
Author
Benson Michael
Categories
4 categories in total
testing
open
aws
open
grafana
open
k6
open
Load Testing using K6 in AWS and Terraform

Summary:

This article talks about how you can deploy your K6 tests in AWS using Terraform and execute them from Gitlab pipeline.

Tools:

  1. K6 load testing framework.
  2. Grafana.
  3. InfluxDB.

Steps:

  1. Build an EC2 instance to run k6 using the following code:
provider "aws" {
  region  = "us-west-2"
  version = "~> 2.8"
}

data "template_file" "user_data" {
  template = file("${path.module}/_templates/k6_data.sh")
}

# The EC2 instance
resource "aws_instance" "k6_instance" {

  ami           = var.ami
  instance_type = var.instance_type
  key_name      = var.key_pair
  user_data     = data.template_file.user_data.rendered
  vpc_security_group_ids = ["${aws_security_group.default.id}"]
  subnet_id = var.key_pair

}

In the parent folder create a file named k6_data.sh under templates folder and add the script to install k6. This will run after the instance is started.

wget https://bintray.com/loadimpact/rpm/rpm -O bintray-loadimpact-rpm.repo
sudo mv bintray-loadimpact-rpm.repo /etc/yum.repos.d/
sudo yum install -y k6

In the similar way we need to create instances for Grafana and influx DB to run and add user data configs which will be initiated once the EC2 instances are up.

For Grafana:

wget https://dl.grafana.com/oss/release/grafana-7.1.0-1.x86_64.rpm
sudo yum install -y grafana-7.1.0-1.x86_64.rpm
sudo service grafana-server start
sudo /sbin/chkconfig --add grafana-server

For Influx DB:

wget https://dl.influxdata.com/influxdb/releases/influxdb-1.7.6.x86_64.rpm
sudo yum localinstall -y influxdb-1.7.6.x86_64.rpm
sudo systemctl enable influxdb.service

In Influx DB after installing the service we need to create data directories, Users and a database.

This should be done after starting the service using systemctl

influx -execute "create user admin with password 'mongoose123' with all privileges"
influx -execute "create user itguy with password 'itguy123' "
influx -execute "create database k6LoadTestDB"
influx -execute "grant ALL on k6LoadTestDB to grafana"

In order for Grafana to run generate reports from the data in Influx DB during test execution we need to configure Grafana to listen to the port in the Load Balancer.

Hope this article helps !!!!

Featured ones: