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