-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
371 lines (312 loc) · 14.1 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
data "http" "source_address" {
url = "https://ipinfo.io/ip"
// url = "https://myexternalip.com/raw"
request_headers = {
Accept = "application/json"
}
}
locals {
name = "onprem_servers"
uniq = substr(sha1(azurerm_resource_group.onprem.id), 0, 8)
linux_vm_names = length(var.linux_vm_names) > 0 ? var.linux_vm_names : [for n in range(var.linux_count) :
format("%s-%02d", var.linux_prefix, n + 1)
]
windows_vm_names = length(var.windows_vm_names) > 0 ? var.windows_vm_names : [for n in range(var.windows_count) :
format("%s-%02d", var.windows_prefix, n + 1)
]
windows_location = var.windows_location != null ? var.windows_location : var.location
linux_location = var.linux_location != null ? var.linux_location : var.location
split_vnet = local.windows_location == local.linux_location ? false : true
vnet = local.split_vnet ? {
(local.windows_location) = {
name = format("%s-%s-vnet", local.name, local.windows_location)
location = local.windows_location
address_space = "10.0.0.0/23"
subnets = {
bastion = "10.0.0.0/24"
windows = "10.0.1.0/24"
}
}
(local.linux_location) = {
name = format("%s-%s-vnet", local.name, local.linux_location)
location = local.linux_location
address_space = "10.0.2.0/23"
subnets = {
linux = "10.0.2.0/24"
}
}
} : {
(local.windows_location) = {
name = format("%s-%s-vnet", local.name, local.windows_location)
location = local.windows_location
address_space = "10.0.0.0/22"
subnets = {
bastion = "10.0.0.0/24"
windows = "10.0.1.0/24"
linux = "10.0.2.0/24"
}
}
}
windows_admin_password = var.windows_admin_password == null ? format("%s!", title(random_pet.onprem.id)) : var.windows_admin_password
azcmagent = var.azcmagent != null ? var.azcmagent : var.arc != null ? {
windows = {
install = true
connect = true
}
linux = {
install = true
connect = true
}
} : {
windows = {
install = false
connect = false
}
linux = {
install = false
connect = false
}
}
# Use source_address_prefices if set, if not just use current public IP
source_address_prefixes = sort(setunion(var.source_address_prefixes, [data.http.source_address.response_body]))
# Set a boolean for the connect if the arc object has been set
# azcmagent_connect = var.arc == null ? false : true
// And then force azcmagent_download to true
# azcmagent_download = local.azcmagent_connect ? true : var.azcmagent
}
// Resource groups
resource "azurerm_resource_group" "onprem" {
name = var.resource_group_name
location = var.location
lifecycle {
ignore_changes = [tags, ]
}
}
resource "azurerm_ssh_public_key" "onprem" {
name = "${local.name}-ssh-public-key"
resource_group_name = upper(azurerm_resource_group.onprem.name)
location = local.linux_location
public_key = file(var.admin_ssh_key_file)
}
resource "random_pet" "onprem" {
length = 2
keepers = {
resource_group_id = azurerm_resource_group.onprem.id
}
}
// Networking
resource "azurerm_application_security_group" "linux" {
name = format("%s-%s-linux-asg", local.name, local.linux_location)
location = local.linux_location
resource_group_name = azurerm_resource_group.onprem.name
}
resource "azurerm_application_security_group" "windows" {
name = format("%s-%s-windows-asg", local.name, local.windows_location)
location = local.windows_location
resource_group_name = azurerm_resource_group.onprem.name
}
resource "azurerm_network_security_group" "linux" {
name = format("%s-%s-linux-nsg", local.name, local.linux_location)
location = local.linux_location
resource_group_name = azurerm_resource_group.onprem.name
security_rule {
name = "SSH"
priority = 1000
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_address_prefixes = local.source_address_prefixes
source_port_range = "*"
destination_application_security_group_ids = [azurerm_application_security_group.linux.id]
destination_port_range = "22"
}
security_rule {
name = "CloudShell"
priority = 1001
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_address_prefix = "AzureCloud"
source_port_range = "*"
destination_application_security_group_ids = [azurerm_application_security_group.linux.id]
destination_port_range = "22"
}
security_rule {
name = "Web"
priority = 1004
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_address_prefix = "*"
source_port_range = "*"
destination_application_security_group_ids = [azurerm_application_security_group.linux.id]
destination_port_ranges = ["80", "443"]
}
}
resource "azurerm_network_security_group" "windows" {
name = format("%s-%s-windows-nsg", local.name, local.windows_location)
location = local.windows_location
resource_group_name = azurerm_resource_group.onprem.name
security_rule {
name = "RDP"
priority = 1001
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_address_prefixes = local.source_address_prefixes
source_port_range = "*"
destination_application_security_group_ids = [azurerm_application_security_group.windows.id]
destination_port_range = "3389"
}
security_rule {
name = "WinRm"
priority = 1002
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_address_prefixes = local.source_address_prefixes
source_port_range = "*"
destination_application_security_group_ids = [azurerm_application_security_group.windows.id]
destination_port_ranges = ["5985", "5986"]
}
security_rule {
name = "WindowsAdminCenter"
priority = 1003
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_address_prefixes = local.source_address_prefixes
source_port_range = "*"
destination_application_security_group_ids = [azurerm_application_security_group.windows.id]
destination_port_ranges = ["6516"]
}
security_rule {
name = "Web"
priority = 1004
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_address_prefix = "*"
source_port_range = "*"
destination_application_security_group_ids = [azurerm_application_security_group.windows.id]
destination_port_ranges = ["80", "443"]
}
security_rule {
name = "SMB"
priority = 1005
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_address_prefix = "VirtualNetwork"
source_port_range = "*"
destination_application_security_group_ids = [azurerm_application_security_group.windows.id]
destination_port_ranges = ["445"]
}
}
resource "azurerm_virtual_network" "onprem" {
for_each = local.vnet
name = each.value.name
address_space = [each.value.address_space]
location = each.value.location
resource_group_name = azurerm_resource_group.onprem.name
}
resource "azurerm_virtual_network_peering" "to" {
count = local.split_vnet ? 1 : 0
name = "${local.windows_location}-to-${local.linux_location}"
resource_group_name = azurerm_resource_group.onprem.name
virtual_network_name = azurerm_virtual_network.onprem[local.windows_location].name
remote_virtual_network_id = azurerm_virtual_network.onprem[local.linux_location].id
}
resource "azurerm_virtual_network_peering" "from" {
count = local.split_vnet ? 1 : 0
name = "${local.linux_location}-to-${local.windows_location}"
resource_group_name = azurerm_resource_group.onprem.name
virtual_network_name = azurerm_virtual_network.onprem[local.linux_location].name
remote_virtual_network_id = azurerm_virtual_network.onprem[local.windows_location].id
}
resource "azurerm_subnet" "bastion" {
for_each = toset(var.bastion ? ["onprem"] : [])
name = "AzureBastionSubnet"
resource_group_name = azurerm_resource_group.onprem.name
virtual_network_name = azurerm_virtual_network.onprem[local.windows_location].name
address_prefixes = [azurerm_virtual_network.onprem[local.windows_location].subnets.bastion]
}
resource "azurerm_subnet" "windows" {
name = "${local.name}-windows-subnet"
resource_group_name = azurerm_resource_group.onprem.name
virtual_network_name = azurerm_virtual_network.onprem[local.windows_location].name
address_prefixes = [local.vnet[local.windows_location].subnets.windows]
}
resource "azurerm_subnet" "linux" {
name = "${local.name}-linux-subnet"
resource_group_name = azurerm_resource_group.onprem.name
virtual_network_name = azurerm_virtual_network.onprem[local.linux_location].name
address_prefixes = [local.vnet[local.linux_location].subnets.linux]
}
resource "azurerm_subnet_network_security_group_association" "windows" {
subnet_id = azurerm_subnet.windows.id
network_security_group_id = azurerm_network_security_group.windows.id
}
resource "azurerm_subnet_network_security_group_association" "linux" {
subnet_id = azurerm_subnet.linux.id
network_security_group_id = azurerm_network_security_group.linux.id
}
// Bastion
resource "azurerm_public_ip" "bastion" {
for_each = toset(var.bastion ? ["onprem"] : [])
name = "${local.name}-bastion-pip"
location = azurerm_resource_group.onprem.location
resource_group_name = azurerm_resource_group.onprem.name
allocation_method = "Static"
sku = "Standard"
}
resource "azurerm_bastion_host" "bastion" {
for_each = toset(var.bastion ? ["onprem"] : [])
name = "${local.name}-bastion"
location = azurerm_resource_group.onprem.location
resource_group_name = azurerm_resource_group.onprem.name
ip_configuration {
name = "bastion-ipconfig"
subnet_id = azurerm_subnet.bastion["onprem"].id
public_ip_address_id = azurerm_public_ip.bastion["onprem"].id
}
}
// Linux virtual machines
module "linux_vms" {
// source = "../../git/terraform-azurerm-arc-onprem-linux-vm"
source = "github.com/terraform-azurerm-modules/terraform-azurerm-arc-onprem-linux-vm?ref=v1.4"
resource_group_name = azurerm_resource_group.onprem.name
location = local.linux_location
tags = var.tags
depends_on = [ azurerm_application_security_group.linux ]
for_each = toset(local.linux_vm_names)
name = each.value
size = var.linux_size
public_ip = var.pip && !var.bastion ? true : false
dns_label = var.pip && !var.bastion ? "onprem-${local.uniq}-${each.value}" : null
subnet_id = azurerm_subnet.linux.id
asg_id = azurerm_application_security_group.linux.id
admin_username = var.admin_username
admin_ssh_public_key = azurerm_ssh_public_key.onprem.public_key
azcmagent = local.azcmagent.linux.install
arc = local.azcmagent.linux.connect ? var.arc : null
}
module "windows_vms" {
// source = "../../git/terraform-azurerm-arc-onprem-windows-vm"
source = "github.com/terraform-azurerm-modules/terraform-azurerm-arc-onprem-windows-vm?ref=v1.4"
resource_group_name = azurerm_resource_group.onprem.name
location = local.windows_location
tags = var.tags
depends_on = [ azurerm_application_security_group.windows ]
for_each = toset(local.windows_vm_names)
name = each.value
size = var.windows_size
public_ip = var.pip && (each.value == local.windows_vm_names[0] || !var.bastion) ? true : false
dns_label = var.pip && (each.value == local.windows_vm_names[0] || !var.bastion) ? "onprem-${local.uniq}-${each.value}" : null
subnet_id = azurerm_subnet.windows.id
asg_id = azurerm_application_security_group.windows.id
admin_username = var.admin_username
admin_password = local.windows_admin_password
azcmagent = local.azcmagent.windows.install
arc = local.azcmagent.windows.connect ? var.arc : null
}