c0da7a487eebef0b065d6b580a80f3fa2585353f
[csit.git] / terraform-ci-infra / 2n_aws_c5n / 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 = "192.168.20.11"
53 }
54 variable "tg_mgmt_ip" {
55   type = string
56   default = "192.168.0.10"
57 }
58 variable "dut1_mgmt_ip" {
59   type = string
60   default = "192.168.0.11"
61 }
62
63 # Instance Type
64 variable "instance_type" {
65   type = string
66   default = "c5n.2xlarge"
67 }
68
69 resource "aws_vpc" "CSIT" {
70   cidr_block = var.vpc_cidr_mgmt
71 }
72
73 resource "aws_security_group" "CSIT" {
74   name        = "CSIT"
75   description = "Allow inbound traffic"
76   vpc_id = aws_vpc.CSIT.id
77
78   ingress {
79     from_port = 22
80     to_port = 22
81     protocol = "tcp"
82     cidr_blocks = ["0.0.0.0/0"]
83   }
84
85   ingress {
86     from_port = 0
87     to_port = 0
88     protocol = -1
89     self = true
90   }
91
92   egress {
93     from_port = 0
94     to_port = 0
95     protocol = "-1"
96     cidr_blocks = ["0.0.0.0/0"]
97   }
98
99   depends_on = [aws_vpc.CSIT]
100 }
101
102 resource "aws_vpc_ipv4_cidr_block_association" "b" {
103   vpc_id = aws_vpc.CSIT.id
104   cidr_block = var.vpc_cidr_b
105   depends_on = [aws_vpc.CSIT]
106 }
107 resource "aws_vpc_ipv4_cidr_block_association" "c" {
108   vpc_id = aws_vpc.CSIT.id
109   cidr_block = var.vpc_cidr_c
110   depends_on = [aws_vpc.CSIT]
111 }
112 resource "aws_vpc_ipv4_cidr_block_association" "d" {
113   vpc_id = aws_vpc.CSIT.id
114   cidr_block = var.vpc_cidr_d
115   depends_on = [aws_vpc.CSIT]
116 }
117
118 resource "aws_subnet" "mgmt" {
119   vpc_id = aws_vpc.CSIT.id
120   cidr_block = var.vpc_cidr_mgmt
121   availability_zone = var.avail_zone
122   depends_on = [aws_vpc.CSIT]
123 }
124
125 resource "aws_subnet" "b" {
126   vpc_id = aws_vpc.CSIT.id
127   cidr_block = var.vpc_cidr_b
128   availability_zone = var.avail_zone
129   depends_on = [aws_vpc.CSIT, aws_vpc_ipv4_cidr_block_association.b]
130 }
131
132 resource "aws_subnet" "c" {
133   vpc_id = aws_vpc.CSIT.id
134   cidr_block = var.vpc_cidr_c
135   availability_zone = var.avail_zone
136   depends_on = [aws_vpc.CSIT, aws_vpc_ipv4_cidr_block_association.c]
137 }
138
139 resource "aws_subnet" "d" {
140   vpc_id = aws_vpc.CSIT.id
141   cidr_block = var.vpc_cidr_d
142   availability_zone = var.avail_zone
143   depends_on = [aws_vpc.CSIT, aws_vpc_ipv4_cidr_block_association.d]
144 }
145
146 resource "aws_internet_gateway" "CSIT" {
147   vpc_id = aws_vpc.CSIT.id
148   depends_on = [aws_vpc.CSIT]
149 }
150
151 resource "aws_key_pair" "CSIT" {
152   key_name = "CSIT"
153   public_key = file("~/.ssh/id_rsa.pub")
154 }
155
156 data "aws_ami" "ubuntu" {
157   most_recent = true
158
159   filter {
160     name = "name"
161     values = ["*hvm-ssd/ubuntu-bionic-18.04-amd64*"]
162   }
163
164   filter {
165     name = "virtualization-type"
166     values = ["hvm"]
167   }
168
169   owners = ["099720109477"] # Canonical
170 }
171
172 resource "aws_placement_group" "CSIT" {
173   name = "CSIT"
174   strategy = "cluster"
175 }
176
177 resource "aws_instance" "tg" {
178   ami = data.aws_ami.ubuntu.id
179   instance_type = var.instance_type
180 #  cpu_threads_per_core = 1
181 #  cpu_core_count = 18
182   key_name = aws_key_pair.CSIT.key_name
183   associate_public_ip_address = true
184   subnet_id = aws_subnet.mgmt.id
185   root_block_device {
186     volume_size = 50
187   }
188   private_ip = var.tg_mgmt_ip
189   vpc_security_group_ids = [aws_security_group.CSIT.id]
190   depends_on = [aws_vpc.CSIT, aws_placement_group.CSIT]
191   placement_group = aws_placement_group.CSIT.id
192   source_dest_check = false
193 }
194
195 resource "aws_instance" "dut1" {
196   ami = data.aws_ami.ubuntu.id
197 #  cpu_threads_per_core = 1
198 #  cpu_core_count = 18
199   instance_type = var.instance_type
200   key_name = aws_key_pair.CSIT.key_name
201   associate_public_ip_address = true
202   subnet_id = aws_subnet.mgmt.id
203   root_block_device {
204     volume_size = 50
205   }
206   private_ip = var.dut1_mgmt_ip
207   vpc_security_group_ids = [aws_security_group.CSIT.id]
208   depends_on = [aws_vpc.CSIT, aws_placement_group.CSIT]
209   placement_group = aws_placement_group.CSIT.id
210   source_dest_check = false
211 }
212
213 resource "aws_route" "CSIT-igw" {
214   route_table_id = aws_vpc.CSIT.main_route_table_id
215   gateway_id = aws_internet_gateway.CSIT.id
216   destination_cidr_block = "0.0.0.0/0"
217   depends_on = [aws_vpc.CSIT, aws_internet_gateway.CSIT]
218 }
219 resource "aws_route" "dummy-trex-port-0" {
220   route_table_id = aws_vpc.CSIT.main_route_table_id
221   network_interface_id = aws_instance.tg.primary_network_interface_id
222   destination_cidr_block = var.trex_dummy_cidr_port_0
223   depends_on = [aws_vpc.CSIT, aws_instance.dut1]
224 }
225 resource "aws_route" "dummy-trex-port-1" {
226   route_table_id = aws_vpc.CSIT.main_route_table_id
227   network_interface_id = aws_instance.tg.primary_network_interface_id
228   destination_cidr_block = var.trex_dummy_cidr_port_1
229   depends_on = [aws_vpc.CSIT, aws_instance.dut1]
230 }
231
232 resource "null_resource" "deploy_tg" {
233   depends_on = [ aws_instance.tg ]
234   connection {
235     user = "ubuntu"
236     host = aws_instance.tg.public_ip
237     private_key = file("~/.ssh/id_rsa")
238   }
239   provisioner "ansible" {
240     plays {
241       playbook {
242         file_path = "../../testbed-setup/ansible/site_aws.yaml"
243         force_handlers = true
244       }
245       hosts = ["tg"]
246       extra_vars = {
247         ansible_python_interpreter = "/usr/bin/python3"
248         aws = true
249       }
250     }
251   }
252 }
253 resource "null_resource" "deploy_dut1" {
254   depends_on = [ aws_instance.dut1 ]
255   connection {
256     user = "ubuntu"
257     host = aws_instance.dut1.public_ip
258     private_key = file("~/.ssh/id_rsa")
259   }
260   provisioner "ansible" {
261     plays {
262       playbook {
263         file_path = "../../testbed-setup/ansible/site_aws.yaml"
264         force_handlers = true
265       }
266       hosts = ["sut"]
267       extra_vars = {
268         ansible_python_interpreter = "/usr/bin/python3"
269         aws = true
270       }
271     }
272   }
273 }
274
275 resource "null_resource" "deploy_topology" {
276   depends_on = [ aws_instance.tg, aws_instance.dut1 ]
277   provisioner "ansible" {
278     plays {
279       playbook {
280         file_path = "../../testbed-setup/ansible/cloud_topology.yaml"
281       }
282       hosts = ["local"]
283       extra_vars = {
284         ansible_python_interpreter = "/usr/bin/python3"
285         cloud_topology = "2n_aws_c5n"
286         tg_if1_mac = data.aws_network_interface.tg_if1.mac_address
287         tg_if2_mac = data.aws_network_interface.tg_if2.mac_address
288         dut1_if1_mac = data.aws_network_interface.dut1_if1.mac_address
289         dut1_if2_mac = data.aws_network_interface.dut1_if2.mac_address
290         tg_public_ip = aws_instance.tg.public_ip
291         dut1_public_ip = aws_instance.dut1.public_ip
292       }
293     }
294   }
295 }
296
297 output "dbg_tg" {
298   value = "TG IP: ${aws_instance.tg.public_ip}"
299 }
300
301 output "dbg_dut1" {
302   value = "DUT1 IP: ${aws_instance.dut1.public_ip}"
303 }
304