188b095b553d2a5067d48252a048fcd8ff2016ae
[csit.git] / fdio.infra.terraform / 3n_aws_c5n / deploy / main.tf
1 data "vault_aws_access_credentials" "creds" {
2   backend = "${var.vault-name}-path"
3   role    = "${var.vault-name}-role"
4 }
5
6 resource "aws_vpc" "CSITVPC" {
7   assign_generated_ipv6_cidr_block = true
8   enable_dns_hostnames             = false
9   enable_dns_support               = true
10   cidr_block                       = var.vpc_cidr_mgmt
11   instance_tenancy                 = "default"
12
13   tags = {
14     "Name"        = "${var.resources_name_prefix}_${var.testbed_name}-vpc"
15     "Environment" = var.environment_name
16   }
17 }
18
19 resource "aws_security_group" "CSITSG" {
20   depends_on                       = [
21     aws_vpc.CSITVPC
22   ]
23   description                      = "Allow inbound traffic"
24   name                             = "${var.resources_name_prefix}_${var.testbed_name}-sg"
25   revoke_rules_on_delete           = false
26   vpc_id                           = aws_vpc.CSITVPC.id
27
28   ingress {
29     from_port        = 22
30     to_port          = 22
31     protocol         = "tcp"
32     cidr_blocks      = ["0.0.0.0/0"]
33   }
34
35   ingress {
36     from_port        = 22
37     to_port          = 22
38     protocol         = "tcp"
39     ipv6_cidr_blocks = ["::/0"]
40   }
41
42   ingress {
43     from_port        = 0
44     to_port          = 0
45     protocol         = -1
46     self             = true
47     ipv6_cidr_blocks = ["::/0"]
48   }
49
50   egress {
51     from_port        = 0
52     to_port          = 0
53     protocol         = "-1"
54     cidr_blocks      = ["0.0.0.0/0"]
55   }
56
57   egress {
58     from_port        = 0
59     to_port          = 0
60     protocol         = "-1"
61     ipv6_cidr_blocks = ["::/0"]
62   }
63
64   tags = {
65     "Name"        = "${var.resources_name_prefix}_${var.testbed_name}-sg"
66     "Environment" = var.environment_name
67   }
68 }
69
70 resource "aws_vpc_ipv4_cidr_block_association" "b" {
71   depends_on = [
72     aws_vpc.CSITVPC
73   ]
74   cidr_block = var.vpc_cidr_b
75   vpc_id     = aws_vpc.CSITVPC.id
76 }
77
78 resource "aws_vpc_ipv4_cidr_block_association" "c" {
79   depends_on = [
80     aws_vpc.CSITVPC
81   ]
82   cidr_block = var.vpc_cidr_c
83   vpc_id     = aws_vpc.CSITVPC.id
84 }
85
86 resource "aws_vpc_ipv4_cidr_block_association" "d" {
87   depends_on = [
88     aws_vpc.CSITVPC
89   ]
90   cidr_block = var.vpc_cidr_d
91   vpc_id     = aws_vpc.CSITVPC.id
92 }
93
94 # Subnets
95 resource "aws_subnet" "mgmt" {
96   availability_zone               = var.avail_zone
97   assign_ipv6_address_on_creation = false
98   cidr_block                      = var.vpc_cidr_mgmt
99   depends_on                      = [
100     aws_vpc.CSITVPC
101   ]
102   ipv6_cidr_block                 = cidrsubnet(aws_vpc.CSITVPC.ipv6_cidr_block, 8, 1)
103   map_public_ip_on_launch         = false
104   vpc_id                          = aws_vpc.CSITVPC.id
105
106   tags = {
107     "Environment" = var.environment_name
108   }
109 }
110
111 resource "aws_subnet" "b" {
112   availability_zone               = var.avail_zone
113   assign_ipv6_address_on_creation = false
114   cidr_block                      = var.vpc_cidr_b
115   depends_on                      = [
116     aws_vpc.CSITVPC,
117     aws_vpc_ipv4_cidr_block_association.b
118   ]
119   ipv6_cidr_block                 = cidrsubnet(aws_vpc.CSITVPC.ipv6_cidr_block, 8, 2)
120   map_public_ip_on_launch         = false
121   vpc_id                          = aws_vpc.CSITVPC.id
122
123   tags = {
124     "Environment" = var.environment_name
125   }
126 }
127
128 resource "aws_subnet" "c" {
129   availability_zone               = var.avail_zone
130   assign_ipv6_address_on_creation = false
131   cidr_block                      = var.vpc_cidr_c
132   depends_on                      = [
133     aws_vpc.CSITVPC,
134     aws_vpc_ipv4_cidr_block_association.c
135   ]
136   ipv6_cidr_block                 = cidrsubnet(aws_vpc.CSITVPC.ipv6_cidr_block, 8, 3)
137   map_public_ip_on_launch         = false
138   vpc_id                          = aws_vpc.CSITVPC.id
139
140   tags = {
141     "Environment" = var.environment_name
142   }
143 }
144
145 resource "aws_subnet" "d" {
146   availability_zone               = var.avail_zone
147   assign_ipv6_address_on_creation = false
148   cidr_block                      = var.vpc_cidr_d
149   depends_on                      = [
150     aws_vpc.CSITVPC,
151     aws_vpc_ipv4_cidr_block_association.d
152   ]
153   ipv6_cidr_block                 = cidrsubnet(aws_vpc.CSITVPC.ipv6_cidr_block, 8, 4)
154   map_public_ip_on_launch         = false
155   vpc_id                          = aws_vpc.CSITVPC.id
156
157   tags = {
158     "Environment" = var.environment_name
159   }
160 }
161
162 resource "aws_internet_gateway" "CSITGW" {
163   depends_on = [
164     aws_vpc.CSITVPC
165   ]
166   vpc_id     = aws_vpc.CSITVPC.id
167
168   tags = {
169     "Environment" = var.environment_name
170   }
171 }
172
173 # SSH keypair
174 # Temporary key for provisioning only
175 resource "tls_private_key" "CSITTLS" {
176   algorithm   = "RSA"
177   ecdsa_curve = "P521"
178   rsa_bits    = 4096
179 }
180
181 resource "aws_key_pair" "CSITKP" {
182   key_name   = "${var.resources_name_prefix}_${var.testbed_name}-key"
183   public_key = "${tls_private_key.CSITTLS.public_key_openssh}"
184 }
185
186 resource "aws_placement_group" "CSITPG" {
187   name     = "${var.resources_name_prefix}_${var.testbed_name}-pg"
188   strategy = "cluster"
189 }
190
191 # NICs
192 resource "aws_network_interface" "dut1_if1" {
193   depends_on        = [
194     aws_vpc.CSITVPC,
195     aws_subnet.b,
196     aws_instance.dut1
197   ]
198   private_ip        = var.dut1_if1_ip
199   private_ips       = [var.dut1_if1_ip]
200   security_groups   = [aws_security_group.CSITSG.id]
201   source_dest_check = false
202   subnet_id         = aws_subnet.b.id
203
204   attachment {
205     instance     = aws_instance.dut1.id
206     device_index = 1
207   }
208
209   tags = {
210     "Environment" = var.environment_name
211   }
212 }
213
214 resource "aws_network_interface" "dut1_if2" {
215   depends_on        = [
216     aws_vpc.CSITVPC,
217     aws_subnet.c,
218     aws_instance.dut1
219   ]
220   private_ip        = var.dut1_if2_ip
221   private_ips       = [var.dut1_if2_ip]
222   security_groups   = [aws_security_group.CSITSG.id]
223   source_dest_check = false
224   subnet_id         = aws_subnet.c.id
225
226   attachment {
227     instance     = aws_instance.dut1.id
228     device_index = 2
229   }
230
231   tags = {
232     "Environment" = var.environment_name
233   }
234 }
235
236 resource "aws_network_interface" "dut2_if1" {
237   depends_on        = [
238     aws_vpc.CSITVPC,
239     aws_subnet.c,
240     aws_instance.dut2
241   ]
242   private_ip        = var.dut2_if1_ip
243   private_ips       = [var.dut2_if1_ip]
244   security_groups   = [aws_security_group.CSITSG.id]
245   source_dest_check = false
246   subnet_id         = aws_subnet.c.id
247
248   attachment {
249     instance     = aws_instance.dut2.id
250     device_index = 1
251   }
252
253   tags = {
254     "Environment" = var.environment_name
255   }
256 }
257
258 resource "aws_network_interface" "dut2_if2" {
259   depends_on        = [
260     aws_vpc.CSITVPC,
261     aws_subnet.d,
262     aws_instance.dut2
263   ]
264   private_ip        = var.dut2_if2_ip
265   private_ips       = [var.dut2_if2_ip]
266   security_groups   = [aws_security_group.CSITSG.id]
267   source_dest_check = false
268   subnet_id         = aws_subnet.d.id
269
270   attachment {
271     instance     = aws_instance.dut2.id
272     device_index = 2
273   }
274
275   tags = {
276     "Environment" = var.environment_name
277   }
278 }
279
280 resource "aws_network_interface" "tg_if1" {
281   depends_on        = [
282     aws_vpc.CSITVPC,
283     aws_subnet.b,
284     aws_instance.tg
285   ]
286   private_ip        = var.tg_if1_ip
287   private_ips       = [var.tg_if1_ip]
288   security_groups   = [aws_security_group.CSITSG.id]
289   source_dest_check = false
290   subnet_id         = aws_subnet.b.id
291
292   attachment {
293     instance     = aws_instance.tg.id
294     device_index = 1
295   }
296
297   tags = {
298     "Environment" = var.environment_name
299   }
300 }
301
302 resource "aws_network_interface" "tg_if2" {
303   depends_on        = [
304     aws_vpc.CSITVPC,
305     aws_subnet.d,
306     aws_instance.tg
307   ]
308   private_ip        = var.tg_if2_ip
309   private_ips       = [var.tg_if2_ip]
310   security_groups   = [aws_security_group.CSITSG.id]
311   source_dest_check = false
312   subnet_id         = aws_subnet.d.id
313
314   attachment {
315     instance     = aws_instance.tg.id
316     device_index = 2
317   }
318
319   tags = {
320     "Environment" = var.environment_name
321   }
322 }
323
324 data "aws_network_interface" "dut1_if1" {
325   id = aws_network_interface.dut1_if1.id
326 }
327
328 data "aws_network_interface" "dut1_if2" {
329   id = aws_network_interface.dut1_if2.id
330 }
331
332 data "aws_network_interface" "dut2_if1" {
333   id = aws_network_interface.dut2_if1.id
334 }
335
336 data "aws_network_interface" "dut2_if2" {
337   id = aws_network_interface.dut2_if2.id
338 }
339
340 data "aws_network_interface" "tg_if1" {
341   id = aws_network_interface.tg_if1.id
342 }
343
344 data "aws_network_interface" "tg_if2" {
345   id = aws_network_interface.tg_if2.id
346 }
347
348 # Instances
349 resource "aws_instance" "tg" {
350   depends_on                           = [
351     aws_vpc.CSITVPC,
352     aws_placement_group.CSITPG,
353     aws_security_group.CSITSG
354   ]
355   ami                                  = var.ami_image_tg
356   availability_zone                    = var.avail_zone
357   associate_public_ip_address          = true
358   instance_initiated_shutdown_behavior = var.instance_initiated_shutdown_behavior
359   instance_type                        = var.instance_type
360   key_name                             = aws_key_pair.CSITKP.key_name
361   placement_group                      = aws_placement_group.CSITPG.id
362   private_ip                           = var.tg_mgmt_ip
363   source_dest_check                    = false
364   subnet_id                            = aws_subnet.mgmt.id
365   vpc_security_group_ids               = [aws_security_group.CSITSG.id]
366   # host_id                            = "1"
367
368 #  root_block_device {
369 #    volume_size = 50
370 #  }
371
372   tags = {
373     "Name"        = "${var.resources_name_prefix}_${var.testbed_name}-tg"
374     "Environment" = var.environment_name
375   }
376 }
377
378 resource "aws_instance" "dut1" {
379   depends_on = [
380     aws_vpc.CSITVPC,
381     aws_placement_group.CSITPG,
382     aws_instance.tg
383   ]
384   ami                                  = var.ami_image_sut
385   availability_zone                    = var.avail_zone
386   associate_public_ip_address          = true
387   instance_initiated_shutdown_behavior = var.instance_initiated_shutdown_behavior
388   instance_type                        = var.instance_type
389   key_name                             = aws_key_pair.CSITKP.key_name
390   placement_group                      = aws_placement_group.CSITPG.id
391   private_ip                           = var.dut1_mgmt_ip
392   source_dest_check                    = false
393   subnet_id                            = aws_subnet.mgmt.id
394   vpc_security_group_ids               = [aws_security_group.CSITSG.id]
395   # host_id                            = "2"
396
397 #  root_block_device {
398 #    volume_size = 50
399 #  }
400
401   tags = {
402     "Name"        = "${var.resources_name_prefix}_${var.testbed_name}-dut1"
403     "Environment" = var.environment_name
404   }
405 }
406
407 resource "aws_instance" "dut2" {
408   depends_on = [
409     aws_vpc.CSITVPC,
410     aws_placement_group.CSITPG,
411     aws_instance.tg,
412     aws_instance.dut1
413   ]
414   ami                                  = var.ami_image_sut
415   availability_zone                    = var.avail_zone
416   associate_public_ip_address          = true
417   instance_initiated_shutdown_behavior = var.instance_initiated_shutdown_behavior
418   instance_type                        = var.instance_type
419   key_name                             = aws_key_pair.CSITKP.key_name
420   placement_group                      = aws_placement_group.CSITPG.id
421   private_ip                           = var.dut2_mgmt_ip
422   source_dest_check                    = false
423   subnet_id                            = aws_subnet.mgmt.id
424   vpc_security_group_ids               = [aws_security_group.CSITSG.id]
425   # host_id                            = "3"
426
427 #  root_block_device {
428 #    volume_size = 50
429 #  }
430
431   tags = {
432     "Name"        = "${var.resources_name_prefix}_${var.testbed_name}-dut2"
433     "Environment" = var.environment_name
434   }
435 }
436
437 # Routes
438 resource "aws_route" "CSIT-igw" {
439   depends_on             = [
440     aws_vpc.CSITVPC,
441     aws_internet_gateway.CSITGW
442   ]
443   destination_cidr_block      = "0.0.0.0/0"
444   destination_ipv6_cidr_block = "::/0"
445   gateway_id                  = aws_internet_gateway.CSITGW.id
446   route_table_id              = aws_vpc.CSITVPC.main_route_table_id
447 }
448
449 resource "aws_route" "dummy-trex-port-0" {
450   depends_on             = [
451     aws_vpc.CSITVPC,
452     aws_instance.dut1
453   ]
454   destination_cidr_block = var.trex_dummy_cidr_port_0
455   network_interface_id   = aws_instance.tg.primary_network_interface_id
456   route_table_id         = aws_vpc.CSITVPC.main_route_table_id
457 }
458
459 resource "aws_route" "dummy-trex-port-1" {
460   depends_on             = [
461     aws_vpc.CSITVPC,
462     aws_instance.dut1
463   ]
464   destination_cidr_block = var.trex_dummy_cidr_port_1
465   network_interface_id   = aws_instance.tg.primary_network_interface_id
466   route_table_id         = aws_vpc.CSITVPC.main_route_table_id
467 }
468
469 # Deployment/Ansible
470 resource "null_resource" "deploy_tg" {
471   depends_on = [
472     aws_instance.tg,
473     aws_network_interface.tg_if1,
474     aws_network_interface.tg_if2,
475     aws_instance.dut1,
476     aws_network_interface.dut1_if1,
477     aws_network_interface.dut1_if2,
478     aws_instance.dut2,
479     aws_network_interface.dut2_if1,
480     aws_network_interface.dut2_if2
481   ]
482
483   connection {
484     user        = "ubuntu"
485     host        = aws_instance.tg.public_ip
486     private_key = tls_private_key.CSITTLS.private_key_pem
487   }
488
489   provisioner "remote-exec" {
490     inline = var.first_run_commands
491   }
492
493 #  provisioner "ansible" {
494 #    plays {
495 #      playbook {
496 #        file_path      = var.ansible_file_path
497 #        force_handlers = true
498 #      }
499 #      hosts = ["tg_aws"]
500 #      extra_vars = {
501 #        ansible_ssh_pass           = var.ansible_provision_pwd
502 #        ansible_python_interpreter = var.ansible_python_executable
503 #        aws                        = true
504 #      }
505 #    }
506 #  }
507 #
508 #  provisioner "remote-exec" {
509 #    on_failure = continue
510 #    inline     = ["sudo reboot"]
511 #  }
512 }
513
514 resource "null_resource" "deploy_dut1" {
515   depends_on = [
516     aws_instance.tg,
517     aws_network_interface.tg_if1,
518     aws_network_interface.tg_if2,
519     aws_instance.dut1,
520     aws_network_interface.dut1_if1,
521     aws_network_interface.dut1_if2,
522     aws_instance.dut2,
523     aws_network_interface.dut2_if1,
524     aws_network_interface.dut2_if2
525   ]
526
527   connection {
528     user        = "ubuntu"
529     host        = aws_instance.dut1.public_ip
530     private_key = tls_private_key.CSITTLS.private_key_pem
531   }
532
533   provisioner "remote-exec" {
534     inline = var.first_run_commands
535   }
536
537 #  provisioner "ansible" {
538 #    plays {
539 #      playbook {
540 #        file_path      = var.ansible_file_path
541 #        force_handlers = true
542 #      }
543 #      hosts = ["sut_aws"]
544 #      extra_vars = {
545 #        ansible_ssh_pass           = var.ansible_provision_pwd
546 #        ansible_python_interpreter = var.ansible_python_executable
547 #        aws                        = true
548 #      }
549 #    }
550 #  }
551 #
552 #  provisioner "remote-exec" {
553 #    on_failure = continue
554 #    inline     = ["sudo reboot"]
555 #  }
556 }
557
558 resource "null_resource" "deploy_dut2" {
559   depends_on = [
560     aws_instance.tg,
561     aws_network_interface.tg_if1,
562     aws_network_interface.tg_if2,
563     aws_instance.dut1,
564     aws_network_interface.dut1_if1,
565     aws_network_interface.dut1_if2,
566     aws_instance.dut2,
567     aws_network_interface.dut2_if1,
568     aws_network_interface.dut2_if2
569   ]
570
571   connection {
572     user        = "ubuntu"
573     host        = aws_instance.dut2.public_ip
574     private_key = tls_private_key.CSITTLS.private_key_pem
575   }
576
577   provisioner "remote-exec" {
578     inline = var.first_run_commands
579   }
580
581 #  provisioner "ansible" {
582 #    plays {
583 #      playbook {
584 #        file_path      = var.ansible_file_path
585 #        force_handlers = true
586 #      }
587 #      hosts = ["sut_aws"]
588 #      extra_vars = {
589 #        ansible_ssh_pass           = var.ansible_provision_pwd
590 #        ansible_python_interpreter = var.ansible_python_executable
591 #        aws                        = true
592 #      }
593 #    }
594 #  }
595 #
596 #  provisioner "remote-exec" {
597 #    on_failure = continue
598 #    inline     = ["sudo reboot"]
599 #  }
600 }
601
602 resource "null_resource" "deploy_topology" {
603   depends_on = [
604     aws_instance.tg,
605     aws_instance.dut1,
606     aws_instance.dut2
607   ]
608
609   provisioner "ansible" {
610     plays {
611       playbook {
612         file_path = var.ansible_topology_path
613       }
614       hosts = ["local"]
615       extra_vars = {
616         ansible_python_interpreter = var.ansible_python_executable
617         testbed_name               = var.testbed_name
618         cloud_topology             = var.topology_name
619         tg_if1_mac                 = data.aws_network_interface.tg_if1.mac_address
620         tg_if2_mac                 = data.aws_network_interface.tg_if2.mac_address
621         dut1_if1_mac               = data.aws_network_interface.dut1_if1.mac_address
622         dut1_if2_mac               = data.aws_network_interface.dut1_if2.mac_address
623         dut2_if1_mac               = data.aws_network_interface.dut2_if1.mac_address
624         dut2_if2_mac               = data.aws_network_interface.dut2_if2.mac_address
625         tg_public_ip               = aws_instance.tg.public_ip
626         dut1_public_ip             = aws_instance.dut1.public_ip
627         dut2_public_ip             = aws_instance.dut2.public_ip
628         public_ip_list             = "${aws_instance.tg.public_ip},${aws_instance.dut1.public_ip},${aws_instance.dut2.public_ip}"
629       }
630     }
631   }
632 }