Infra: Fix AWS deployment
[csit.git] / terraform-ci-infra / 2n_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   = "${var.resources_name_prefix}_${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]
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.d.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]
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" "tg_if1" {
168   subnet_id         = aws_subnet.b.id
169   source_dest_check = false
170   private_ip        = var.tg_if1_ip
171   private_ips       = [var.tg_if1_ip]
172   security_groups   = [aws_security_group.CSITSG.id]
173   depends_on        = [aws_vpc.CSITVPC, aws_subnet.b]
174
175   attachment {
176     instance     = aws_instance.tg.id
177     device_index = 1
178   }
179
180   tags = {
181     "Environment" = var.environment_name
182   }
183 }
184
185 resource "aws_network_interface" "tg_if2" {
186   subnet_id         = aws_subnet.d.id
187   source_dest_check = false
188   private_ip        = var.tg_if2_ip
189   private_ips       = [var.tg_if2_ip]
190   security_groups   = [aws_security_group.CSITSG.id]
191   depends_on        = [aws_vpc.CSITVPC, aws_subnet.d]
192
193   attachment {
194     instance     = aws_instance.tg.id
195     device_index = 2
196   }
197
198   tags = {
199     "Environment" = var.environment_name
200   }
201 }
202
203 data "aws_network_interface" "dut1_if1" {
204   id = aws_network_interface.dut1_if1.id
205 }
206
207 data "aws_network_interface" "dut1_if2" {
208   id = aws_network_interface.dut1_if2.id
209 }
210
211 data "aws_network_interface" "tg_if1" {
212   id = aws_network_interface.tg_if1.id
213 }
214
215 data "aws_network_interface" "tg_if2" {
216   id = aws_network_interface.tg_if2.id
217 }
218
219 # Instances
220 resource "aws_instance" "tg" {
221   ami                         = var.ami_image
222   availability_zone           = var.avail_zone
223   instance_type               = var.instance_type
224   key_name                    = aws_key_pair.CSITKP.key_name
225   associate_public_ip_address = true
226   subnet_id                   = aws_subnet.mgmt.id
227   private_ip                  = var.tg_mgmt_ip
228   vpc_security_group_ids      = [aws_security_group.CSITSG.id]
229   placement_group             = aws_placement_group.CSITPG.id
230   source_dest_check           = false
231   depends_on                  = [aws_vpc.CSITVPC, aws_placement_group.CSITPG]
232   # host_id                   = "1"
233
234   root_block_device {
235     volume_size = 50
236   }
237
238   tags = {
239     "Name"        = "${var.resources_name_prefix}_${var.testbed_name}-tg"
240     "Environment" = var.environment_name
241   }
242 }
243
244 resource "aws_instance" "dut1" {
245   ami                         = var.ami_image
246   availability_zone           = var.avail_zone
247   instance_type               = var.instance_type
248   key_name                    = aws_key_pair.CSITKP.key_name
249   associate_public_ip_address = true
250   subnet_id                   = aws_subnet.mgmt.id
251   private_ip                  = var.dut1_mgmt_ip
252   vpc_security_group_ids      = [aws_security_group.CSITSG.id]
253   placement_group             = aws_placement_group.CSITPG.id
254   source_dest_check           = false
255   depends_on                  = [aws_vpc.CSITVPC, aws_placement_group.CSITPG]
256   # host_id                   = "2"
257
258   root_block_device {
259     volume_size = 50
260   }
261
262   tags = {
263     "Name"        = "${var.resources_name_prefix}_${var.testbed_name}-dut1"
264     "Environment" = var.environment_name
265   }
266 }
267
268 # Routes
269 resource "aws_route" "CSIT-igw" {
270   route_table_id         = aws_vpc.CSITVPC.main_route_table_id
271   gateway_id             = aws_internet_gateway.CSITGW.id
272   destination_cidr_block = "0.0.0.0/0"
273   depends_on             = [aws_vpc.CSITVPC, aws_internet_gateway.CSITGW]
274 }
275
276 resource "aws_route" "dummy-trex-port-0" {
277   route_table_id         = aws_vpc.CSITVPC.main_route_table_id
278   network_interface_id   = aws_instance.tg.primary_network_interface_id
279   destination_cidr_block = var.trex_dummy_cidr_port_0
280   depends_on             = [aws_vpc.CSITVPC, aws_instance.dut1]
281 }
282
283 resource "aws_route" "dummy-trex-port-1" {
284   route_table_id         = aws_vpc.CSITVPC.main_route_table_id
285   network_interface_id   = aws_instance.tg.primary_network_interface_id
286   destination_cidr_block = var.trex_dummy_cidr_port_1
287   depends_on             = [aws_vpc.CSITVPC, aws_instance.dut1]
288 }
289
290 # Deployment/Ansible
291 resource "null_resource" "deploy_tg" {
292   depends_on = [
293     aws_instance.tg,
294     aws_network_interface.tg_if1,
295     aws_network_interface.tg_if2
296   ]
297
298   connection {
299     user        = "ubuntu"
300     host        = aws_instance.tg.public_ip
301     private_key = tls_private_key.CSITTLS.private_key_pem
302   }
303
304   provisioner "remote-exec" {
305     inline = var.first_run_commands
306   }
307
308   provisioner "ansible" {
309     plays {
310       playbook {
311         file_path      = var.ansible_file_path
312         force_handlers = true
313       }
314       hosts      = ["tg_aws"]
315       extra_vars = {
316         ansible_ssh_pass           = var.ansible_provision_pwd
317         ansible_python_interpreter = var.ansible_python_executable
318         aws                        = true
319       }
320     }
321   }
322 }
323
324 resource "null_resource" "deploy_dut1" {
325   depends_on = [
326     aws_instance.dut1,
327     aws_network_interface.dut1_if1,
328     aws_network_interface.dut1_if2
329   ]
330
331   connection {
332     user        = "ubuntu"
333     host        = aws_instance.dut1.public_ip
334     private_key = tls_private_key.CSITTLS.private_key_pem
335   }
336
337   provisioner "remote-exec" {
338     inline = var.first_run_commands
339   }
340
341   provisioner "ansible" {
342     plays {
343       playbook {
344         file_path      = var.ansible_file_path
345         force_handlers = true
346       }
347       hosts      = ["sut_aws"]
348       extra_vars = {
349         ansible_ssh_pass           = var.ansible_provision_pwd
350         ansible_python_interpreter = var.ansible_python_executable
351         aws                        = true
352       }
353     }
354   }
355 }
356
357 resource "null_resource" "deploy_topology" {
358   depends_on = [ aws_instance.tg, aws_instance.dut1 ]
359
360   provisioner "ansible" {
361     plays {
362       playbook {
363         file_path = var.ansible_topology_path
364       }
365       hosts      = ["local"]
366       extra_vars = {
367         ansible_python_interpreter = var.ansible_python_executable
368         testbed_name               = var.testbed_name
369         cloud_topology             = var.topology_name
370         tg_if1_mac                 = data.aws_network_interface.tg_if1.mac_address
371         tg_if2_mac                 = data.aws_network_interface.tg_if2.mac_address
372         dut1_if1_mac               = data.aws_network_interface.dut1_if1.mac_address
373         dut1_if2_mac               = data.aws_network_interface.dut1_if2.mac_address
374         tg_public_ip               = aws_instance.tg.public_ip
375         dut1_public_ip             = aws_instance.dut1.public_ip
376         public_ip_list             = "${aws_instance.tg.public_ip},${aws_instance.dut1.public_ip}"
377       }
378     }
379   }
380 }