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