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