Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Provisioning Example for Distributed Execution Using IaC - Terraform/AWS/EC2 #1933

Merged
merged 5 commits into from
Nov 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Running your Locust tests
configuration
running-locust-distributed
running-locust-docker
running-cloud-integration
running-without-web-ui


Expand Down
103 changes: 103 additions & 0 deletions docs/running-cloud-integration.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
.. _running-cloud-integration:

======================================================
Running Locust Distributed with IaC on IaaS (AWS/EC2)
======================================================

1. AWS Authentication
=====================

```bash
export AWS_ACCESS_KEY_ID=AIAXXXXXXXXXXXXXXXXX
export AWS_SECRET_ACCESS_KEY=T9HyXXXXXXXXXXXXXXXXXXXXXXXXXXXX
```

2. Configure your provisioning
==============================

- Don't forget to provide the correct subnet name in the variable file
- Define location and file of your locust plan script
- Define the number of nodes to create

**variables.tf**

```bash
variable "node_size" {
description = "Size of total nodes"
default = 2
}

variable "loadtest_dir_source" {
default = "plan/"
}

variable "locust_plan_filename" {
default = "basic.py"
}

variable "subnet_name" {
default = "subnet-prd-a"
description = "Subnet name"
}

```

---

3. Execute Terraform
=======================

```bash
cd examples/terraform/aws
terraform init
terraform apply --auto-approve
```

---

4. Access UI
============


Click on the link below to access the UI:

Result exemple:

```bash
Apply complete! Resources: 14 added, 0 changed, 0 destroyed.

Outputs:

dashboard_url = "http://3.237.255.123"
leader_private_ip = "10.17.5.119"
leader_public_ip = "3.237.255.123"
nodes_private_ip = [
"10.17.5.167",
"10.17.5.39",
]
nodes_public_ip = [
"3.235.45.218",
"100.24.124.0",
]
```

![locust-home](https://github.com/marcosborges/terraform-aws-loadtest-distribuited/raw/v0.4.0/assets/locust-home.png)

---

5. Cleanup
==========

```bash
terraform destroy --auto-approve
```

---

6. More information
===================

- [Terraform aws-get-started >> install-terraform-on-linux](https://learn.hashicorp.com/tutorials/terraform/install-cli?in=terraform/aws-get-started#install-terraform-on-linux)

- [Terraform module aws loadtest distribuited](https://registry.terraform.io/modules/marcosborges/loadtest-distribuited/aws/latest)

93 changes: 93 additions & 0 deletions examples/terraform/aws/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@

## 1. AWS Authentication

```bash
export AWS_ACCESS_KEY_ID=AIAXXXXXXXXXXXXXXXXX
export AWS_SECRET_ACCESS_KEY=T9HyXXXXXXXXXXXXXXXXXXXXXXXXXXXX
```

## 2. Configure your provisioning

- Don't forget to provide the correct subnet name in the variable file
- Define location and file of your locust plan script
- Define the number of nodes to create

**variables.tf**

```bash
variable "node_size" {
description = "Size of total nodes"
default = 2
}

variable "loadtest_dir_source" {
default = "plan/"
}

variable "locust_plan_filename" {
default = "basic.py"
}

variable "subnet_name" {
default = "subnet-prd-a"
description = "Subnet name"
}

```



---

## 3. Execute Terraform

```bash
cd examples/terraform/aws
terraform init
terraform apply --auto-approve
```

---

## 4. Access UI

Click on the link below to access the UI:

Result exemple:

```bash
Apply complete! Resources: 14 added, 0 changed, 0 destroyed.

Outputs:

dashboard_url = "http://3.237.255.123"
leader_private_ip = "10.17.5.119"
leader_public_ip = "3.237.255.123"
nodes_private_ip = [
"10.17.5.167",
"10.17.5.39",
]
nodes_public_ip = [
"3.235.45.218",
"100.24.124.0",
]
```

![locust-home](https://github.com/marcosborges/terraform-aws-loadtest-distribuited/raw/v0.4.0/assets/locust-home.png)

---

## 5. Cleanup

```bash
terraform destroy --auto-approve
```

---

## 6. More information

- [Terraform aws-get-started >> install-terraform-on-linux](https://learn.hashicorp.com/tutorials/terraform/install-cli?in=terraform/aws-get-started#install-terraform-on-linux)

- [Terraform module aws loadtest distribuited](https://registry.terraform.io/modules/marcosborges/loadtest-distribuited/aws/latest)

6 changes: 6 additions & 0 deletions examples/terraform/aws/data_subnet.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
data "aws_subnet" "current" {
filter {
name = "tag:Name"
values = [var.subnet_name]
}
}
32 changes: 32 additions & 0 deletions examples/terraform/aws/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module "loadtest" {

# https://registry.terraform.io/modules/marcosborges/loadtest-distribuited/aws/latest
source = "marcosborges/loadtest-distribuited/aws"

name = "provision-name"
nodes_size = var.node_size
executor = "locust"
loadtest_dir_source = var.loadtest_dir_source

# LEADER ENTRYPOINT
loadtest_entrypoint = <<-EOT
nohup locust \
-f ${var.locust_plan_filename} \
--web-port=8080 \
--expect-workers=${var.node_size} \
--master > locust-leader.out 2>&1 &
EOT

# NODES ENTRYPOINT
node_custom_entrypoint = <<-EOT
nohup locust \
-f ${var.locust_plan_filename} \
--worker \
--master-host={LEADER_IP} > locust-worker.out 2>&1 &
EOT

subnet_id = data.aws_subnet.current.id
locust_plan_filename = var.locust_plan_filename
ssh_export_pem = false

}
24 changes: 24 additions & 0 deletions examples/terraform/aws/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
output "leader_public_ip" {
value = module.loadtest.leader_public_ip
description = "The public IP address of the leader server instance."
}

output "leader_private_ip" {
value = module.loadtest.leader_private_ip
description = "The private IP address of the leader server instance."
}

output "nodes_public_ip" {
value = module.loadtest.nodes_public_ip
description = "The public IP address of the nodes instances."
}

output "nodes_private_ip" {
value = module.loadtest.nodes_private_ip
description = "The private IP address of the nodes instances."
}

output "dashboard_url" {
value = "http://${coalesce(module.loadtest.leader_public_ip, module.loadtest.leader_private_ip)}"
description = "The URL of the Locust UI."
}
21 changes: 21 additions & 0 deletions examples/terraform/aws/plan/basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import time
from locust import HttpUser, task, between


class Quickstart(HttpUser):
wait_time = between(1, 5)

@task
def google(self):
self.client.request_name = "google"
self.client.get("https://google.com/")

@task
def microsoft(self):
self.client.request_name = "microsoft"
self.client.get("https://microsoft.com/")

@task
def facebook(self):
self.client.request_name = "facebook"
self.client.get("https://facebook.com/")
3 changes: 3 additions & 0 deletions examples/terraform/aws/provisioner.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
provider "aws" {
region = "us-east-1"
}
17 changes: 17 additions & 0 deletions examples/terraform/aws/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
variable "node_size" {
description = "Size of total nodes"
default = 2
}

variable "loadtest_dir_source" {
default = "plan/"
}

variable "locust_plan_filename" {
default = "basic.py"
}

variable "subnet_name" {
default = "subnet-prd-a"
description = "Subnet name"
}