-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
181 lines (140 loc) · 3.69 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
provider "google" {
project = var.gcp-project
region = var.region
zone = var.zone
}
data "google_compute_image" "ubuntu-2004-lts" {
family = "ubuntu-pro-2004-lts"
project = "ubuntu-os-pro-cloud"
}
resource "google_compute_firewall" "default" {
name = "web-firewall"
network = google_compute_network.network.self_link
allow {
protocol = "icmp"
}
allow {
protocol = "tcp"
ports = ["22", "80", "5557"]
}
source_ranges = ["0.0.0.0/0"]
target_tags = ["web"]
}
data "cloudinit_config" "cloudinit" {
gzip = false
base64_encode = false
part {
content_type = "text/cloud-config"
content = <<EOT
apt:
sources:
docker.list:
source: deb [arch=amd64] https://download.docker.com/linux/ubuntu $RELEASE stable
keyid: 9DC858229FC7DD38854AE2D88D81803C0EBFCD88
packages:
- docker-ce
- docker-ce-cli
- containerd.io
write_files:
- path: /locust/locustfile.py
content: |
from locust import HttpUser, task
class HelloWorldUser(HttpUser):
@task
def hello_world(self):
self.client.get("/")
EOT
}
part {
content_type = "text/x-shellscript"
filename = "startup.sh"
content = <<EOT
#!/usr/bin/env bash
set -eux
docker run -d --restart=always -p 80:8089 -p 5557:5557 -v /locust:/mnt/locust locustio/locust -f /mnt/locust/locustfile.py --master
EOT
}
}
data "cloudinit_config" "cloudinit-worker" {
gzip = false
base64_encode = false
part {
content_type = "text/cloud-config"
content = <<EOT
apt:
sources:
docker.list:
source: deb [arch=amd64] https://download.docker.com/linux/ubuntu $RELEASE stable
keyid: 9DC858229FC7DD38854AE2D88D81803C0EBFCD88
packages:
- docker-ce
- docker-ce-cli
- containerd.io
write_files:
- path: /locust/locustfile.py
content: |
from locust import HttpUser, task
class HelloWorldUser(HttpUser):
@task
def hello_world(self):
self.client.get("/")
EOT
}
part {
content_type = "text/x-shellscript"
filename = "startup.sh"
content = <<EOT
#!/usr/bin/env bash
set -eux
docker run -d --restart=always -p 80:8089 -v /locust:/mnt/locust locustio/locust -f /mnt/locust/locustfile.py --worker --master-host ${local.private-ip}
EOT
}
}
resource "google_compute_network" "network" {
name = "${var.instance-name}-network"
auto_create_subnetworks = true
}
resource "google_compute_instance" "vm_instance" {
name = "${var.instance-name}-leader"
machine_type = var.instance-type
tags = ["web"]
allow_stopping_for_update = true
metadata = {
user-data = data.cloudinit_config.cloudinit.rendered
}
boot_disk {
initialize_params {
image = data.google_compute_image.ubuntu-2004-lts.self_link
}
}
network_interface {
network = google_compute_network.network.self_link
access_config {
}
}
}
resource "google_compute_instance" "worker" {
count = max(ceil(var.requests-per-second / local.rps-per-worker), 1)
name = "${var.instance-name}-worker-${count.index}"
machine_type = var.instance-type
tags = ["web"]
allow_stopping_for_update = true
metadata = {
user-data = data.cloudinit_config.cloudinit-worker.rendered
}
boot_disk {
initialize_params {
image = data.google_compute_image.ubuntu-2004-lts.self_link
}
}
network_interface {
network = google_compute_network.network.self_link
access_config {
}
}
}
locals {
public-ip = google_compute_instance.vm_instance.network_interface[0].access_config[0].nat_ip
private-ip = google_compute_instance.vm_instance.network_interface[0].network_ip
rps-per-worker = 80
}