-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
381 lines (365 loc) · 10.2 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
data "nsxt_policy_tier0_gateway" "lab_t0" {
display_name = "LAB-T0"
}
data "nsxt_policy_transport_zone" "overlay_tz" {
display_name = "nsx-overlay-transportzone"
}
resource "nsxt_policy_segment" "segment" {
display_name = "${var.customer}-Segment_${var.gateway_ip}-${var.subnet_length}"
description = "Terraform provisioned Segment"
connectivity_path = data.nsxt_policy_tier0_gateway.lab_t0.path
transport_zone_path = data.nsxt_policy_transport_zone.overlay_tz.path
subnet {
cidr = "${var.gateway_ip}/${var.subnet_length}"
}
}
data "nsxt_policy_segment_realization" "segment" {
path = nsxt_policy_segment.segment.path
}
data "vsphere_datacenter" "dc" {
name = "LAB"
}
data "vsphere_datastore" "datastore" {
name = "Datastore-iSCSI-1"
datacenter_id = data.vsphere_datacenter.dc.id
}
data "vsphere_compute_cluster" "cluster" {
name = "LAB-Cluster"
datacenter_id = data.vsphere_datacenter.dc.id
}
data "vsphere_network" "network" {
name = data.nsxt_policy_segment_realization.segment.network_name
datacenter_id = data.vsphere_datacenter.dc.id
}
data "vsphere_content_library" "library" {
name = "Images-Library"
}
data "vsphere_content_library_item" "wp_app_template" {
name = "wp-app-template"
library_id = data.vsphere_content_library.library.id
type = "VM-TEMPLATE"
}
data "vsphere_content_library_item" "wp_db_template" {
name = "wp-db-template"
library_id = data.vsphere_content_library.library.id
type = "VM-TEMPLATE"
}
resource "vsphere_folder" "folder" {
path = var.customer
type = "vm"
datacenter_id = "${data.vsphere_datacenter.dc.id}"
}
resource "vsphere_virtual_machine" "wordpress_app" {
name = "${var.customer}-app-0${count.index}"
count= length(var.application_vm_ip)
resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
datastore_id = data.vsphere_datastore.datastore.id
folder = vsphere_folder.folder.path
num_cpus = 1
memory = 2048
guest_id = "centos8_64Guest"
firmware = "efi"
network_interface {
network_id = data.vsphere_network.network.id
adapter_type = "vmxnet3"
}
disk {
label = "Hard disk 1"
size = 16
}
clone {
template_uuid = data.vsphere_content_library_item.wp_app_template.id
customize {
linux_options {
host_name = "wordpress-app"
domain = "lab.mimran.pro"
}
network_interface {
ipv4_address = var.application_vm_ip[count.index]
ipv4_netmask = var.subnet_length
}
ipv4_gateway = var.gateway_ip
dns_server_list = [var.dns_server]
}
}
vapp {
properties = {
database_ip = var.database_vm_ip
}
}
}
resource "vsphere_virtual_machine" "wordpress_db" {
name = "${var.customer}-db"
resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
datastore_id = data.vsphere_datastore.datastore.id
folder = vsphere_folder.folder.path
num_cpus = 1
memory = 2048
guest_id = "centos8_64Guest"
firmware = "efi"
network_interface {
network_id = data.vsphere_network.network.id
adapter_type = "vmxnet3"
}
disk {
label = "Hard disk 1"
size = 16
}
clone {
template_uuid = data.vsphere_content_library_item.wp_db_template.id
customize {
linux_options {
host_name = "wordpress-db"
domain = "lab.mimran.pro"
}
network_interface {
ipv4_address = var.database_vm_ip
ipv4_netmask = var.subnet_length
}
ipv4_gateway = var.gateway_ip
dns_server_list = [var.dns_server]
}
}
}
resource "nsxt_vm_tags" "app_tags" {
count = length(vsphere_virtual_machine.wordpress_app)
instance_id = vsphere_virtual_machine.wordpress_app[count.index].id
tag {
scope = "customer"
tag = var.customer
}
tag {
scope = "application"
tag = "wordpress"
}
tag {
scope = "tier"
tag = "app"
}
}
resource "nsxt_vm_tags" "db_tags" {
instance_id = vsphere_virtual_machine.wordpress_db.id
tag {
scope = "customer"
tag = var.customer
}
tag {
scope = "application"
tag = "wordpress"
}
tag {
scope = "tier"
tag = "db"
}
}
resource "nsxt_policy_group" "wordpress" {
display_name = "${var.customer}_wordpress"
description = "Terraform provisioned Group"
criteria {
condition {
key = "Tag"
member_type = "VirtualMachine"
operator = "EQUALS"
value = "application|wordpress"
}
}
}
resource "nsxt_policy_group" "wordpress_app_tier" {
display_name = "${var.customer}_wordpress_app_tier"
description = "Terraform provisioned Group"
criteria {
condition {
key = "Tag"
member_type = "VirtualMachine"
operator = "EQUALS"
value = "application|wordpress"
}
condition {
key = "Tag"
member_type = "VirtualMachine"
operator = "EQUALS"
value = "tier|app"
}
}
}
resource "nsxt_policy_group" "wordpress_db_tier" {
display_name = "${var.customer}_wordpress_db_tier"
description = "Terraform provisioned Group"
criteria {
condition {
key = "Tag"
member_type = "VirtualMachine"
operator = "EQUALS"
value = "application|wordpress"
}
condition {
key = "Tag"
member_type = "VirtualMachine"
operator = "EQUALS"
value = "tier|db"
}
}
}
data "nsxt_policy_service" "http_service" {
display_name = "HTTP"
}
data "nsxt_policy_service" "mysql_service" {
display_name = "MySQL"
}
resource "nsxt_policy_security_policy" "wordpress_policy" {
display_name = "${var.customer}_wordpress_policy"
description = "Terraform provisioned Security Policy"
category = "Application"
locked = false
stateful = true
tcp_strict = false
scope = [nsxt_policy_group.wordpress.path]
rule {
display_name = "Allow HTTP to App"
destination_groups = [nsxt_policy_group.wordpress_app_tier.path]
services = [data.nsxt_policy_service.http_service.path]
action = "ALLOW"
logged = false
disabled = false
}
rule {
display_name = "Allow App to DB"
source_groups = [nsxt_policy_group.wordpress_app_tier.path]
destination_groups = [nsxt_policy_group.wordpress_db_tier.path]
services = [data.nsxt_policy_service.mysql_service.path]
action = "ALLOW"
logged = false
disabled = false
}
rule {
display_name = "Default Deny"
action = "DROP"
logged = true
}
}
/*
AVI Networks : Virtual Service Creation
*/
data "avi_applicationprofile" "system_https_profile" {
name= "System-Secure-HTTP"
}
data "avi_tenant" "default_tenant" {
name= "admin"
}
data "avi_cloud" "default_cloud" {
name= "NSX-T Cloud"
}
data "avi_serviceenginegroup" "se_group" {
name = "Default-Group"
}
data "avi_networkprofile" "system_tcp_profile" {
name= "System-TCP-Proxy"
}
data "avi_analyticsprofile" "system_analytics_profile" {
name= "System-Analytics-Profile"
}
data "avi_sslkeyandcertificate" "system_default_cert" {
name= "System-Default-Cert"
}
data "avi_sslprofile" "system_standard_sslprofile" {
name= "System-Standard"
}
data "avi_vrfcontext" "global_vrf" {
name= "global"
}
data "avi_applicationpersistenceprofile" "persistence_sourceip" {
name = "System-Persistence-Client-IP"
}
data "avi_healthmonitor" "http_monitor" {
name = "System-HTTP"
}
# resource "avi_server" "pool_members" {
# count = 2
# ip = vsphere_virtual_machine.wordpress_app[count.index].default_ip_address
# port = "80"
# pool_ref = avi_pool.wordpress_pool.id
# hostname = vsphere_virtual_machine.wordpress_app[count.index].name
# }
resource "avi_pool" "wordpress_pool" {
name= "${var.customer}_wordpress_pool"
health_monitor_refs= [data.avi_healthmonitor.http_monitor.id]
tenant_ref= data.avi_tenant.default_tenant.id
cloud_ref= data.avi_cloud.default_cloud.id
application_persistence_profile_ref= data.avi_applicationpersistenceprofile.persistence_sourceip.id
servers {
ip {
type= "V4"
addr= vsphere_virtual_machine.wordpress_app[0].default_ip_address
}
hostname= vsphere_virtual_machine.wordpress_app[0].name
port = 80
}
servers {
ip {
type= "V4"
addr= vsphere_virtual_machine.wordpress_app[1].default_ip_address
}
hostname= vsphere_virtual_machine.wordpress_app[1].name
port = 80
}
tier1_lr="/infra/tier-1s/AVI-T1"
}
resource "avi_vsvip" "wordpress_vip" {
name= "${var.customer}_wordpress_vip"
vip {
vip_id= "1"
auto_allocate_ip = true
auto_allocate_floating_ip = true
}
dns_info {
fqdn= "${var.customer}-wordpress.avi.lab.mimran.pro"
num_records_in_response = 0
}
tier1_lr="/infra/tier-1s/AVI-T1"
cloud_ref= data.avi_cloud.default_cloud.id
}
data "avi_wafpolicy" "waf_policy" {
name = "Test-WAF-Policy"
}
data "avi_errorpageprofile" "error_page" {
name = "Error-Page"
}
resource "avi_virtualservice" "wordpress_vs" {
name= "${var.customer}_wordpress_vs"
pool_ref= avi_pool.wordpress_pool.id
tenant_ref= data.avi_tenant.default_tenant.id
cloud_ref= data.avi_cloud.default_cloud.id
waf_policy_ref = data.avi_wafpolicy.waf_policy.id
vsvip_ref = avi_vsvip.wordpress_vip.id
scaleout_ecmp = true
application_profile_ref= data.avi_applicationprofile.system_https_profile.id
services {
port= 443
enable_ssl= true
port_range_end= 443
}
cloud_type = "CLOUD_NSXT"
se_group_ref= data.avi_serviceenginegroup.se_group.id
analytics_profile_ref= data.avi_analyticsprofile.system_analytics_profile.id
ssl_key_and_certificate_refs= [data.avi_sslkeyandcertificate.system_default_cert.id]
ssl_profile_ref= data.avi_sslprofile.system_standard_sslprofile.id
vrf_context_ref= data.avi_vrfcontext.global_vrf.id
error_page_profile_ref= data.avi_errorpageprofile.error_page.id
analytics_policy {
all_headers= true
udf_log_throttle= 0
full_client_logs {
duration= 0
enabled= true
throttle= 50
}
metrics_realtime_update {
enabled= true
duration= 0
}
}
}
output "wordpress_fqdn" {
description = "FQDN of the newly created application"
value = "https://${avi_vsvip.wordpress_vip.dns_info[0].fqdn}"
}