AWS terraform automation scripts
[csit.git] / resources / tools / terraform / aws / main.tf
1 provider "aws" {
2   region = "eu-central-1"
3 }
4
5 variable "avail_zone" {
6   type = string
7   default = "eu-central-1a"
8 }
9 # Base VPC CIDRs
10 variable "vpc_cidr_mgmt" {
11   type = string
12   default = "192.168.0.0/24"
13 }
14 variable "vpc_cidr_b" {
15   type = string
16   default = "192.168.10.0/24"
17 }
18 variable "vpc_cidr_c" {
19   type = string
20   default = "200.0.0.0/24"
21 }
22 variable "vpc_cidr_d" {
23   type = string
24   default = "192.168.20.0/24"
25 }
26
27 # Trex Dummy CIDRs
28 variable "trex_dummy_cidr_port_0" {
29   type = string
30   default = "10.0.0.0/24"
31 }
32 variable "trex_dummy_cidr_port_1" {
33   type = string
34   default = "20.0.0.0/24"
35 }
36
37 # IPs
38 variable "a_gateway" {
39   type = string
40   default = "192.168.0.1"
41 }
42 variable "b_gateway" {
43   type = string
44   default = "192.168.10.1"
45 }
46 variable "c_gateway" {
47   type = string
48   default = "200.0.0.1"
49 }
50 variable "d_gateway" {
51   type = string
52   default = "192.168.20.1"
53 }
54 variable "tg_if1_ip" {
55   type = string
56   default = "192.168.10.254"
57 }
58 variable "tg_if2_ip" {
59   type = string
60   default = "192.168.20.254"
61 }
62 variable "dut1_if1_ip" {
63   type = string
64   default = "192.168.10.11"
65 }
66 variable "dut1_if2_ip" {
67   type = string
68   default = "200.0.0.101"
69 }
70 variable "dut2_if1_ip" {
71   type = string
72   default = "200.0.0.102"
73 }
74 variable "dut2_if2_ip" {
75   type = string
76   default = "192.168.20.11"
77 }
78 variable "tg_mgmt_ip" {
79   type = string
80   default = "192.168.0.10"
81 }
82 variable "dut1_mgmt_ip" {
83   type = string
84   default = "192.168.0.11"
85 }
86 variable "dut2_mgmt_ip" {
87   type = string
88   default = "192.168.0.12"
89 }
90
91 # Instance Type
92 variable "instance_type" {
93   type = string
94   default = "c5n.9xlarge"
95 }
96
97 resource "aws_vpc" "CSIT" {
98   cidr_block = var.vpc_cidr_mgmt
99 }
100
101 resource "aws_security_group" "CSIT" {
102   name        = "CSIT"
103   description = "Allow  inbound traffic"
104   vpc_id = aws_vpc.CSIT.id
105
106   ingress {
107     from_port = 22
108     to_port = 22
109     protocol = "tcp"
110     cidr_blocks = ["0.0.0.0/0"]
111   }
112
113   ingress {
114     from_port = 0
115     to_port = 0
116     protocol = -1
117     self = true
118   }
119
120   egress {
121     from_port       = 0
122     to_port         = 0
123     protocol        = "-1"
124     cidr_blocks     = ["0.0.0.0/0"]
125   }
126
127   depends_on = [aws_vpc.CSIT]
128 }
129
130 resource "aws_vpc_ipv4_cidr_block_association" "b" {
131   vpc_id     = aws_vpc.CSIT.id
132   cidr_block = var.vpc_cidr_b
133   depends_on = [aws_vpc.CSIT]
134 }
135 resource "aws_vpc_ipv4_cidr_block_association" "c" {
136   vpc_id     = aws_vpc.CSIT.id
137   cidr_block = var.vpc_cidr_c
138   depends_on = [aws_vpc.CSIT]
139 }
140 resource "aws_vpc_ipv4_cidr_block_association" "d" {
141   vpc_id     = aws_vpc.CSIT.id
142   cidr_block = var.vpc_cidr_d
143   depends_on = [aws_vpc.CSIT]
144 }
145
146 resource "aws_subnet" "mgmt" {
147   vpc_id = aws_vpc.CSIT.id
148   cidr_block = var.vpc_cidr_mgmt
149   availability_zone = var.avail_zone
150   depends_on = [aws_vpc.CSIT]
151 }
152
153 resource "aws_subnet" "b" {
154   vpc_id = aws_vpc.CSIT.id
155   cidr_block = var.vpc_cidr_b
156   availability_zone = var.avail_zone
157   depends_on = [aws_vpc.CSIT, aws_vpc_ipv4_cidr_block_association.b]
158 }
159
160 resource "aws_subnet" "c" {
161   vpc_id = aws_vpc.CSIT.id
162   cidr_block = var.vpc_cidr_c
163   availability_zone = var.avail_zone
164   depends_on = [aws_vpc.CSIT, aws_vpc_ipv4_cidr_block_association.c]
165 }
166
167 resource "aws_subnet" "d" {
168   vpc_id = aws_vpc.CSIT.id
169   cidr_block = var.vpc_cidr_d
170   availability_zone = var.avail_zone
171   depends_on = [aws_vpc.CSIT, aws_vpc_ipv4_cidr_block_association.d]
172 }
173
174 resource "aws_internet_gateway" "CSIT" {
175   vpc_id = aws_vpc.CSIT.id
176   depends_on = [aws_vpc.CSIT]
177 }
178
179 resource "aws_key_pair" "CSIT" {
180   key_name = "CSIT"
181   public_key = file("~/.ssh/id_rsa.pub")
182 }
183
184 data "aws_ami" "ubuntu" {
185   most_recent = true
186
187   filter {
188     name   = "name"
189     values = ["*hvm-ssd/ubuntu-bionic-18.04-amd64*"]
190   }
191
192   filter {
193     name   = "virtualization-type"
194     values = ["hvm"]
195   }
196
197   owners = ["099720109477"] # Canonical
198 }
199
200 resource "aws_placement_group" "CSIT" {
201   name     = "CSIT"
202   strategy = "cluster"
203 }
204
205 resource "aws_instance" "tg" {
206   ami           = data.aws_ami.ubuntu.id
207   instance_type = var.instance_type
208 #  cpu_threads_per_core = 1
209 #  cpu_core_count = 18
210   key_name = aws_key_pair.CSIT.key_name
211   associate_public_ip_address = true
212   subnet_id = aws_subnet.mgmt.id
213   private_ip = var.tg_mgmt_ip
214   vpc_security_group_ids = [aws_security_group.CSIT.id]
215   depends_on = [aws_vpc.CSIT, aws_placement_group.CSIT]
216   placement_group = aws_placement_group.CSIT.id
217   source_dest_check = false
218 }
219
220 resource "aws_instance" "dut1" {
221   ami           = data.aws_ami.ubuntu.id
222 #  cpu_threads_per_core = 1
223 #  cpu_core_count = 18
224   instance_type = var.instance_type
225   key_name = aws_key_pair.CSIT.key_name
226   associate_public_ip_address = true
227   subnet_id = aws_subnet.mgmt.id
228   private_ip = var.dut1_mgmt_ip
229   vpc_security_group_ids = [aws_security_group.CSIT.id]
230   depends_on = [aws_vpc.CSIT, aws_placement_group.CSIT]
231   placement_group = aws_placement_group.CSIT.id
232   source_dest_check = false
233 }
234
235 resource "aws_instance" "dut2" {
236   ami           = data.aws_ami.ubuntu.id
237 #  cpu_threads_per_core = 1
238 #  cpu_core_count = 18
239   instance_type = var.instance_type
240   key_name = aws_key_pair.CSIT.key_name
241   associate_public_ip_address = true
242   subnet_id = aws_subnet.mgmt.id
243   private_ip = var.dut2_mgmt_ip
244   vpc_security_group_ids = [aws_security_group.CSIT.id]
245   depends_on = [aws_vpc.CSIT, aws_placement_group.CSIT]
246   placement_group = aws_placement_group.CSIT.id
247   source_dest_check = false
248 }
249
250 resource "aws_route" "CSIT-igw" {
251   route_table_id = aws_vpc.CSIT.main_route_table_id
252   gateway_id = aws_internet_gateway.CSIT.id
253   destination_cidr_block = "0.0.0.0/0"
254   depends_on = [aws_vpc.CSIT, aws_internet_gateway.CSIT]
255 }
256 resource "aws_route" "dummy-trex-port-0" {
257   route_table_id = aws_vpc.CSIT.main_route_table_id
258   network_interface_id = aws_instance.tg.primary_network_interface_id
259   destination_cidr_block = var.trex_dummy_cidr_port_0
260   depends_on = [aws_vpc.CSIT, aws_instance.dut1]
261 }
262 resource "aws_route" "dummy-trex-port-1" {
263   route_table_id = aws_vpc.CSIT.main_route_table_id
264   network_interface_id = aws_instance.tg.primary_network_interface_id
265   destination_cidr_block = var.trex_dummy_cidr_port_1
266   depends_on = [aws_vpc.CSIT, aws_instance.dut2]
267 }
268
269 resource "null_resource" "deploy_tg" {
270   depends_on = [ aws_instance.tg ]
271   connection {
272     user = "ubuntu"
273     host = aws_instance.tg.public_ip
274     private_key = file("~/.ssh/id_rsa")
275   }
276   provisioner "ansible" {
277     plays {
278       playbook {
279         file_path = "../../testbed-setup/ansible/site_aws.yaml"
280         force_handlers = true
281       }
282       hosts = ["tg"]
283       extra_vars = {
284         ansible_python_interpreter = "/usr/bin/python3"
285         aws = true
286       }
287     }
288   }
289 }
290 resource "null_resource" "deploy_dut1" {
291   depends_on = [ aws_instance.dut1 ]
292   connection {
293     user = "ubuntu"
294     host = aws_instance.dut1.public_ip
295     private_key = file("~/.ssh/id_rsa")
296   }
297   provisioner "ansible" {
298     plays {
299       playbook {
300         file_path = "../../testbed-setup/ansible/site_aws.yaml"
301         force_handlers = true
302       }
303       hosts = ["sut"]
304       extra_vars = {
305         ansible_python_interpreter = "/usr/bin/python3"
306         aws = true
307       }
308     }
309   }
310 }
311 resource "null_resource" "deploy_dut2" {
312   depends_on = [ aws_instance.dut2 ]
313   connection {
314     user = "ubuntu"
315     host = aws_instance.dut2.public_ip
316     private_key = file("~/.ssh/id_rsa")
317   }
318   provisioner "ansible" {
319     plays {
320       playbook {
321         file_path = "../../testbed-setup/ansible/site_aws.yaml"
322         force_handlers = true
323       }
324       hosts = ["sut"]
325       extra_vars = {
326         ansible_python_interpreter = "/usr/bin/python3"
327         aws = true
328       }
329     }
330   }
331 }
332
333 resource "null_resource" "deploy_topology" {
334   depends_on = [ aws_instance.tg, aws_instance.dut1, aws_instance.dut2 ]
335   provisioner "ansible" {
336     plays {
337       playbook {
338         file_path = "../../testbed-setup/ansible/cloud_topology.yaml"
339       }
340       hosts = ["local"]
341       extra_vars = {
342         ansible_python_interpreter = "/usr/bin/python3"
343         cloud_topology = "aws"
344         tg_if1_mac = data.aws_network_interface.tg_if1.mac_address
345         tg_if2_mac = data.aws_network_interface.tg_if2.mac_address
346         dut1_if1_mac = data.aws_network_interface.dut1_if1.mac_address
347         dut1_if2_mac = data.aws_network_interface.dut1_if2.mac_address
348         dut2_if1_mac = data.aws_network_interface.dut2_if1.mac_address
349         dut2_if2_mac = data.aws_network_interface.dut2_if2.mac_address
350         tg_public_ip = aws_instance.tg.public_ip
351         dut1_public_ip = aws_instance.dut1.public_ip
352         dut2_public_ip = aws_instance.dut2.public_ip
353       }
354     }
355   }
356 }
357
358 output "dbg_tg" {
359   value = "TG IP: ${aws_instance.tg.public_ip}"
360 }
361
362 output "dbg_dut1" {
363   value = "DUT1 IP: ${aws_instance.dut1.public_ip}"
364 }
365
366 output "dbg_dut2" {
367   value = "DUT2 IP: ${aws_instance.dut2.public_ip}"
368 }