-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathmain.tf
472 lines (419 loc) · 11.7 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
terraform {
required_providers {
outscale = {
source = "outscale-dev/outscale"
version = "0.5.4"
}
}
required_version = ">= 1.2.0"
}
provider "outscale" {}
resource "outscale_keypair" "keypair01" {
keypair_name = "hackathon"
}
# SG common for all VMs
resource "outscale_security_group" "hackathon_common" {
security_group_name = "hackathon-common"
}
resource "outscale_security_group_rule" "hackathon_ssh" {
flow = "Inbound"
security_group_id = outscale_security_group.hackathon_common.id
rules {
from_port_range = "22"
to_port_range = "22"
ip_protocol = "tcp"
ip_ranges = ["0.0.0.0/0"]
}
}
resource "outscale_security_group_rule" "hackathon_vscode" {
flow = "Inbound"
security_group_id = outscale_security_group.hackathon_common.id
rules {
from_port_range = "3000"
to_port_range = "3000"
ip_protocol = "tcp"
ip_ranges = ["0.0.0.0/0"]
}
}
# SG common for all VMs
resource "outscale_security_group" "hackathon_web" {
security_group_name = "hackathon-web"
}
resource "outscale_security_group_rule" "hackathon_web" {
flow = "Inbound"
security_group_id = outscale_security_group.hackathon_web.id
rules {
from_port_range = "8000"
to_port_range = "8000"
ip_protocol = "tcp"
ip_ranges = ["0.0.0.0/0"]
}
}
# SG MongoDB
resource "outscale_security_group" "hackathon_mongodb" {
security_group_name = "hackathon-mongodb"
}
# SG MondoDB rule
resource "outscale_security_group_rule" "hackathon_mongodb" {
flow = "Inbound"
security_group_id = outscale_security_group.hackathon_mongodb.id
rules {
from_port_range = "27017"
to_port_range = "27017"
ip_protocol = "tcp"
ip_ranges = ["0.0.0.0/0"]
}
}
# SG MondoDB rule
resource "outscale_security_group_rule" "hackathon_mongo_express" {
flow = "Inbound"
security_group_id = outscale_security_group.hackathon_mongodb.id
rules {
from_port_range = "8081"
to_port_range = "8081"
ip_protocol = "tcp"
ip_ranges = ["0.0.0.0/0"]
}
}
# SG Postgres
resource "outscale_security_group" "hackathon_postgre" {
security_group_name = "hackathon-postgres"
}
# SG Postgres rule
resource "outscale_security_group_rule" "hackathon_postgre" {
flow = "Inbound"
security_group_id = outscale_security_group.hackathon_postgre.id
rules {
from_port_range = "5432"
to_port_range = "5432"
ip_protocol = "tcp"
ip_ranges = ["0.0.0.0/0"]
}
}
# SG Postgres rule
resource "outscale_security_group_rule" "hackathon_adminer" {
flow = "Inbound"
security_group_id = outscale_security_group.hackathon_postgre.id
rules {
from_port_range = "8080"
to_port_range = "8080"
ip_protocol = "tcp"
ip_ranges = ["0.0.0.0/0"]
}
}
########### VM with Database 1 ###########
# Note: vm_type defines the resources of VM:
# v5 - CPU generation. Possible values: [1..5], where 5 is the last generation
# c4 - the number of virtual cores (=4)
# r8 - The number of GB RAM (=8)
# p1 - performance. Possible values: [1,2,3], where 1 is highest
resource "outscale_vm" "hackathon_db1" {
image_id = "ami-bb490c7e"
vm_type = "tinav5.c4r8p1"
keypair_name = "${outscale_keypair.keypair01.keypair_name}"
security_group_ids = [outscale_security_group.hackathon_common.security_group_id, outscale_security_group.hackathon_postgre.security_group_id, outscale_security_group.hackathon_mongodb.security_group_id]
tags {
key = "name"
value = "hackathon_db1"
}
block_device_mappings {
device_name = "/dev/sdb"
bsu {
volume_size = 115
volume_type = "io1"
iops = 150
delete_on_vm_deletion = true
}
}
# Create SSH connection script
provisioner "local-exec" {
command = "echo 'ssh -o StrictHostKeyChecking=no -i ~/.ssh/hackathon.rsa outscale@${outscale_vm.hackathon_db1.public_ip}' > db1_connect.sh"
}
# Save IP to local script file
provisioner "local-exec" {
command = "echo '${outscale_vm.hackathon_db1.public_ip} db1' >> hosts"
}
# Change script attributes
provisioner "local-exec" {
command = "chmod +x db1_connect.sh"
}
# Copy init script to VM
provisioner "file" {
source = "db1/init.sh"
destination = "/home/outscale/init.sh"
connection {
type = "ssh"
user = "outscale"
private_key = "${outscale_keypair.keypair01.private_key}"
host = self.public_ip
}
}
# Copy docker-compose.yml to vm
provisioner "file" {
source = "db1/docker-compose.yml"
destination = "/home/outscale/docker-compose.yml"
connection {
type = "ssh"
user = "outscale"
private_key = "${outscale_keypair.keypair01.private_key}"
host = self.public_ip
}
}
# Copy DB init script to VM
provisioner "file" {
source = "db1/db_init.sql"
destination = "/home/outscale/db_init.sql"
connection {
type = "ssh"
user = "outscale"
private_key = "${outscale_keypair.keypair01.private_key}"
host = self.public_ip
}
}
# Run init script in VM
provisioner "remote-exec" {
inline = [
"chmod +x /home/outscale/init.sh",
"/home/outscale/init.sh",
]
connection {
type = "ssh"
user = "outscale"
private_key = "${outscale_keypair.keypair01.private_key}"
host = self.public_ip
}
}
# Clean on destroy
provisioner "local-exec" {
when = destroy
command = "rm -f db1_connect.sh"
}
}
########### VM with Microservice1 ###########
resource "outscale_vm" "hackathon_ms1" {
image_id = "ami-bb490c7e"
vm_type = "tinav5.c4r8p1"
keypair_name = "${outscale_keypair.keypair01.keypair_name}"
security_group_ids = [outscale_security_group.hackathon_common.security_group_id,outscale_security_group.hackathon_web.security_group_id]
tags {
key = "name"
value = "hackathon_ms1"
}
block_device_mappings {
device_name = "/dev/sdb"
bsu {
volume_size = 115
volume_type = "io1"
iops = 150
delete_on_vm_deletion = true
}
}
# Create SSH connection script
provisioner "local-exec" {
command = "echo 'ssh -o StrictHostKeyChecking=no -i ~/.ssh/hackathon.rsa outscale@${outscale_vm.hackathon_ms1.public_ip}' > ms1_connect.sh"
}
# Save IP to file
provisioner "local-exec" {
command = "echo '${outscale_vm.hackathon_ms1.public_ip} ms1' >> hosts"
}
# Change script attributes
provisioner "local-exec" {
command = "chmod +x ms1_connect.sh"
}
# Pack VSCode
provisioner "local-exec" {
command = "zip ms1/vscode.zip ms1/vscode/*"
}
# Copy VSCode file to VM
provisioner "file" {
source = "ms1/vscode.zip"
destination = "/home/outscale/vscode.zip"
connection {
type = "ssh"
user = "outscale"
private_key = "${outscale_keypair.keypair01.private_key}"
host = self.public_ip
}
}
# Copy init script to VM
provisioner "file" {
source = "ms1/init.sh"
destination = "/home/outscale/init.sh"
connection {
type = "ssh"
user = "outscale"
private_key = "${outscale_keypair.keypair01.private_key}"
host = self.public_ip
}
}
# Copy hosts to VM
provisioner "file" {
source = "hosts"
destination = "/home/outscale/hosts"
connection {
type = "ssh"
user = "outscale"
private_key = "${outscale_keypair.keypair01.private_key}"
host = self.public_ip
}
}
# Copy ms1 to VM
provisioner "file" {
source = "ms1/src/app.py"
destination = "/home/outscale/app.py"
connection {
type = "ssh"
user = "outscale"
private_key = "${outscale_keypair.keypair01.private_key}"
host = self.public_ip
}
}
# Run init script in VM
provisioner "remote-exec" {
inline = [
"chmod +x /home/outscale/init.sh",
"/home/outscale/init.sh",
]
connection {
type = "ssh"
user = "outscale"
private_key = "${outscale_keypair.keypair01.private_key}"
host = self.public_ip
}
}
# Clean on destroy
provisioner "local-exec" {
when = destroy
command = "rm -f ms1_connect.sh"
}
}
########### VM with App1 ###########
resource "outscale_vm" "hackathon_app1" {
image_id = "ami-bb490c7e"
vm_type = "tinav5.c4r8p1"
keypair_name = "${outscale_keypair.keypair01.keypair_name}"
security_group_ids = [outscale_security_group.hackathon_common.security_group_id]
tags {
key = "name"
value = "hackathon_app1"
}
block_device_mappings {
device_name = "/dev/sdb"
bsu {
volume_size = 115
volume_type = "io1"
iops = 150
delete_on_vm_deletion = true
}
}
# Extract ssh key to local file in ~/.ssh
provisioner "local-exec" {
command = <<EOT
cat <<EOF > ~/.ssh/hackathon.rsa
${outscale_keypair.keypair01.private_key}
EOF
EOT
}
# Change ssh key file attributes
provisioner "local-exec" {
command = "chmod 600 ~/.ssh/hackathon.rsa"
}
# Create SSH connection script
provisioner "local-exec" {
command = "echo 'ssh -o StrictHostKeyChecking=no -i ~/.ssh/hackathon.rsa outscale@${outscale_vm.hackathon_app1.public_ip}' > app1_connect.sh"
}
# Save IP to file
provisioner "local-exec" {
command = "echo '${outscale_vm.hackathon_app1.public_ip} app1' >> hosts"
}
# Change script attributes
provisioner "local-exec" {
command = "chmod +x app1_connect.sh"
}
# Pack VSCode
provisioner "local-exec" {
command = "zip app1/vscode.zip app1/vscode/*"
}
# Copy VSCode file to VM
provisioner "file" {
source = "app1/vscode.zip"
destination = "/home/outscale/vscode.zip"
connection {
type = "ssh"
user = "outscale"
private_key = "${outscale_keypair.keypair01.private_key}"
host = self.public_ip
}
}
# Pack src
provisioner "local-exec" {
command = "zip app1/src.zip app1/src/*"
}
# Copy src.zip file to VM
provisioner "file" {
source = "app1/src.zip"
destination = "/home/outscale/src.zip"
connection {
type = "ssh"
user = "outscale"
private_key = "${outscale_keypair.keypair01.private_key}"
host = self.public_ip
}
}
# Copy init script to VM
provisioner "file" {
source = "app1/init.sh"
destination = "/home/outscale/init.sh"
connection {
type = "ssh"
user = "outscale"
private_key = "${outscale_keypair.keypair01.private_key}"
host = self.public_ip
}
}
# Copy media_load script to VM
provisioner "file" {
source = "app1/.media_load.sh"
destination = "/home/outscale/.media_load.sh"
connection {
type = "ssh"
user = "outscale"
private_key = "${outscale_keypair.keypair01.private_key}"
host = self.public_ip
}
}
# Copy hosts to VM
provisioner "file" {
source = "hosts"
destination = "/home/outscale/hosts"
connection {
type = "ssh"
user = "outscale"
private_key = "${outscale_keypair.keypair01.private_key}"
host = self.public_ip
}
}
# Run init script in VM
provisioner "remote-exec" {
inline = [
"chmod +x /home/outscale/.media_load.sh",
"chmod +x /home/outscale/init.sh",
"/home/outscale/init.sh",
]
connection {
type = "ssh"
user = "outscale"
private_key = "${outscale_keypair.keypair01.private_key}"
host = self.public_ip
}
}
# Clean on destroy
provisioner "local-exec" {
when = destroy
command = "rm -f ~/.ssh/hackathon.rsa"
}
provisioner "local-exec" {
when = destroy
command = "rm -f app1_connect.sh"
}
}