This repository has been archived by the owner on Jun 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
303 lines (268 loc) · 15.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
locals {
nsg_inbound_rules = { for idx, security_rule in var.nsg_inbound_rules : security_rule.name => {
idx : idx,
security_rule : security_rule,
}
}
}
#---------------------------------------------------------------
# Generates SSH2 key Pair for Linux VM's (Dev Environment only)
#---------------------------------------------------------------
resource "tls_private_key" "rsa" {
count = var.generate_admin_ssh_key == true && var.os_flavor == "linux" ? 1 : 0
algorithm = "RSA"
rsa_bits = 4096
}
#----------------------------------------------------------
# Resource Group, VNet, Subnet selection & Random Resources
#----------------------------------------------------------
data "azurerm_resource_group" "rg" {
name = var.resource_group_name
}
data "azurerm_virtual_network" "vnet" {
name = var.virtual_network_name
resource_group_name = data.azurerm_resource_group.rg.name
}
data "azurerm_subnet" "snet" {
name = var.subnet_name
virtual_network_name = data.azurerm_virtual_network.vnet.name
resource_group_name = data.azurerm_resource_group.rg.name
}
data "azurerm_log_analytics_workspace" "logws" {
count = var.log_analytics_workspace_name != null ? 1 : 0
name = var.log_analytics_workspace_name
resource_group_name = data.azurerm_resource_group.rg.name
}
data "azurerm_storage_account" "storeacc" {
count = var.hub_storage_account_name != null ? 1 : 0
name = var.hub_storage_account_name
resource_group_name = data.azurerm_resource_group.rg.name
}
resource "random_password" "passwd" {
count = var.disable_password_authentication != true || var.os_flavor == "windows" && var.admin_password == null ? 1 : 0
length = 24
min_upper = 4
min_lower = 2
min_numeric = 4
special = false
keepers = {
admin_password = var.os_flavor
}
}
resource "random_string" "str" {
count = var.enable_public_ip_address == true ? var.instances_count : 0
length = 6
special = false
upper = false
keepers = {
domain_name_label = var.virtual_machine_name
}
}
#-----------------------------------
# Public IP for Virtual Machine
#-----------------------------------
resource "azurerm_public_ip" "pip" {
count = var.enable_public_ip_address == true ? var.instances_count : 0
name = lower("pip-vm-${var.virtual_machine_name}-${data.azurerm_resource_group.rg.location}-0${count.index + 1}")
location = data.azurerm_resource_group.rg.location
resource_group_name = data.azurerm_resource_group.rg.name
allocation_method = "Static"
sku = "Standard"
domain_name_label = format("%s%s", lower(replace(var.virtual_machine_name, "/[[:^alnum:]]/", "")), random_string.str[count.index].result)
tags = merge({ "ResourceName" = lower("pip-vm-${var.virtual_machine_name}-${data.azurerm_resource_group.rg.location}-0${count.index + 1}") }, var.tags, )
}
#---------------------------------------
# Network Interface for Virtual Machine
#---------------------------------------
resource "azurerm_network_interface" "nic" {
count = var.instances_count
name = var.instances_count == 1 ? lower("nic-${format("vm%s", lower(replace(var.virtual_machine_name, "/[[:^alnum:]]/", "")))}") : lower("nic-${format("vm%s%s", lower(replace(var.virtual_machine_name, "/[[:^alnum:]]/", "")), count.index + 1)}")
resource_group_name = data.azurerm_resource_group.rg.name
location = data.azurerm_resource_group.rg.location
dns_servers = var.dns_servers
enable_ip_forwarding = var.enable_ip_forwarding
enable_accelerated_networking = var.enable_accelerated_networking
tags = merge({ "ResourceName" = var.instances_count == 1 ? lower("nic-${format("vm%s", lower(replace(var.virtual_machine_name, "/[[:^alnum:]]/", "")))}") : lower("nic-${format("vm%s%s", lower(replace(var.virtual_machine_name, "/[[:^alnum:]]/", "")), count.index + 1)}") }, var.tags, )
ip_configuration {
name = lower("ipconig-${format("vm%s%s", lower(replace(var.virtual_machine_name, "/[[:^alnum:]]/", "")), count.index + 1)}")
primary = true
subnet_id = data.azurerm_subnet.snet.id
private_ip_address_allocation = var.private_ip_address_allocation_type
private_ip_address = var.private_ip_address_allocation_type == "Static" ? element(concat(var.private_ip_address, [""]), count.index) : null
public_ip_address_id = var.enable_public_ip_address == true ? element(concat(azurerm_public_ip.pip.*.id, [""]), count.index) : null
}
}
resource "azurerm_availability_set" "aset" {
count = var.enable_vm_availability_set ? 1 : 0
name = lower("avail-${var.virtual_machine_name}-${data.azurerm_resource_group.rg.location}")
resource_group_name = data.azurerm_resource_group.rg.name
location = data.azurerm_resource_group.rg.location
platform_fault_domain_count = 2
platform_update_domain_count = 2
managed = true
tags = merge({ "ResourceName" = lower("avail-${var.virtual_machine_name}-${data.azurerm_resource_group.rg.location}") }, var.tags, )
}
#---------------------------------------------------------------
# Network security group for Virtual Machine Network Interface
#---------------------------------------------------------------
resource "azurerm_network_security_group" "nsg" {
name = lower("nsg_${var.virtual_machine_name}_${data.azurerm_resource_group.rg.location}_in")
resource_group_name = data.azurerm_resource_group.rg.name
location = data.azurerm_resource_group.rg.location
tags = merge({ "ResourceName" = lower("nsg_${var.virtual_machine_name}_${data.azurerm_resource_group.rg.location}_in") }, var.tags, )
}
resource "azurerm_network_security_rule" "nsg_rule" {
for_each = local.nsg_inbound_rules
name = each.key
priority = 100 * (each.value.idx + 1)
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = each.value.security_rule.destination_port_range
source_address_prefix = each.value.security_rule.source_address_prefix
destination_address_prefix = element(concat(data.azurerm_subnet.snet.address_prefixes, [""]), 0)
description = "Inbound_Port_${each.value.security_rule.destination_port_range}"
resource_group_name = data.azurerm_resource_group.rg.name
network_security_group_name = azurerm_network_security_group.nsg.name
depends_on = [azurerm_network_security_group.nsg]
}
resource "azurerm_network_interface_security_group_association" "nsgassoc" {
count = var.instances_count
network_interface_id = element(concat(azurerm_network_interface.nic.*.id, [""]), count.index)
network_security_group_id = azurerm_network_security_group.nsg.id
}
#---------------------------------------
# Linux Virutal machine
#---------------------------------------
resource "azurerm_linux_virtual_machine" "linux_vm" {
count = var.os_flavor == "linux" ? var.instances_count : 0
name = var.instances_count == 1 ? var.virtual_machine_name : format("%s%s", lower(replace(var.virtual_machine_name, "/[[:^alnum:]]/", "")), count.index + 1)
resource_group_name = data.azurerm_resource_group.rg.name
location = data.azurerm_resource_group.rg.location
size = var.virtual_machine_size
admin_username = var.admin_username
admin_password = var.disable_password_authentication != true && var.admin_password == null ? element(concat(random_password.passwd.*.result, [""]), 0) : var.admin_password
network_interface_ids = [element(concat(azurerm_network_interface.nic.*.id, [""]), count.index)]
source_image_id = var.source_image_id != null ? var.source_image_id : null
provision_vm_agent = true
allow_extension_operations = true
dedicated_host_id = var.dedicated_host_id
availability_set_id = var.enable_vm_availability_set == true ? element(concat(azurerm_availability_set.aset.*.id, [""]), 0) : null
tags = merge({ "ResourceName" = var.instances_count == 1 ? var.virtual_machine_name : format("%s%s", lower(replace(var.virtual_machine_name, "/[[:^alnum:]]/", "")), count.index + 1) }, var.tags, )
admin_ssh_key {
username = var.admin_username
public_key = var.generate_admin_ssh_key == true && var.os_flavor == "linux" ? tls_private_key.rsa[0].public_key_openssh : file(var.admin_ssh_key_data)
}
dynamic "source_image_reference" {
for_each = var.source_image_id != null ? [] : [1]
content {
publisher = var.custom_image != null ? var.custom_image["publisher"] : var.linux_distribution_list[lower(var.linux_distribution_name)]["publisher"]
offer = var.custom_image != null ? var.custom_image["offer"] : var.linux_distribution_list[lower(var.linux_distribution_name)]["offer"]
sku = var.custom_image != null ? var.custom_image["sku"] : var.linux_distribution_list[lower(var.linux_distribution_name)]["sku"]
version = var.custom_image != null ? var.custom_image["version"] : var.linux_distribution_list[lower(var.linux_distribution_name)]["version"]
}
}
os_disk {
storage_account_type = var.os_disk_storage_account_type
caching = "ReadWrite"
}
}
#---------------------------------------
# Windows Virutal machine
#---------------------------------------
resource "azurerm_windows_virtual_machine" "win_vm" {
count = var.os_flavor == "windows" ? var.instances_count : 0
name = var.instances_count == 1 ? var.virtual_machine_name : format("%s%s", lower(replace(var.virtual_machine_name, "/[[:^alnum:]]/", "")), count.index + 1)
computer_name = var.instances_count == 1 ? var.virtual_machine_name : format("%s%s", lower(replace(var.virtual_machine_name, "/[[:^alnum:]]/", "")), count.index + 1)
resource_group_name = data.azurerm_resource_group.rg.name
location = data.azurerm_resource_group.rg.location
size = var.virtual_machine_size
admin_username = var.admin_username
admin_password = var.admin_password == null ? element(concat(random_password.passwd.*.result, [""]), 0) : var.admin_password
network_interface_ids = [element(concat(azurerm_network_interface.nic.*.id, [""]), count.index)]
source_image_id = var.source_image_id != null ? var.source_image_id : null
provision_vm_agent = true
allow_extension_operations = true
dedicated_host_id = var.dedicated_host_id
license_type = var.license_type
availability_set_id = var.enable_vm_availability_set == true ? element(concat(azurerm_availability_set.aset.*.id, [""]), 0) : null
tags = merge({ "ResourceName" = var.instances_count == 1 ? var.virtual_machine_name : format("%s%s", lower(replace(var.virtual_machine_name, "/[[:^alnum:]]/", "")), count.index + 1) }, var.tags, )
dynamic "source_image_reference" {
for_each = var.source_image_id != null ? [] : [1]
content {
publisher = var.custom_image != null ? var.custom_image["publisher"] : var.windows_distribution_list[lower(var.windows_distribution_name)]["publisher"]
offer = var.custom_image != null ? var.custom_image["offer"] : var.windows_distribution_list[lower(var.windows_distribution_name)]["offer"]
sku = var.custom_image != null ? var.custom_image["sku"] : var.windows_distribution_list[lower(var.windows_distribution_name)]["sku"]
version = var.custom_image != null ? var.custom_image["version"] : var.windows_distribution_list[lower(var.windows_distribution_name)]["version"]
}
}
os_disk {
storage_account_type = var.os_disk_storage_account_type
caching = "ReadWrite"
}
}
#--------------------------------------------------------------
# Azure Log Analytics Workspace Agent Installation for windows
#--------------------------------------------------------------
resource "azurerm_virtual_machine_extension" "omsagentwin" {
count = var.log_analytics_workspace_name != null && var.os_flavor == "windows" ? var.instances_count : 0
name = var.instances_count == 1 ? "OmsAgentForWindows" : format("%s%s", "OmsAgentForWindows", count.index + 1)
virtual_machine_id = azurerm_windows_virtual_machine.win_vm[count.index].id
publisher = "Microsoft.EnterpriseCloud.Monitoring"
type = "MicrosoftMonitoringAgent"
type_handler_version = "1.0"
auto_upgrade_minor_version = true
settings = <<SETTINGS
{
"workspaceId": "${data.azurerm_log_analytics_workspace.logws.0.workspace_id}"
}
SETTINGS
protected_settings = <<PROTECTED_SETTINGS
{
"workspaceKey": "${data.azurerm_log_analytics_workspace.logws.0.primary_shared_key}"
}
PROTECTED_SETTINGS
}
#--------------------------------------------------------------
# Azure Log Analytics Workspace Agent Installation for Linux
#--------------------------------------------------------------
resource "azurerm_virtual_machine_extension" "omsagentlinux" {
count = var.log_analytics_workspace_name != null && var.os_flavor == "linux" ? var.instances_count : 0
name = var.instances_count == 1 ? "OmsAgentForLinux" : format("%s%s", "OmsAgentForLinux", count.index + 1)
virtual_machine_id = azurerm_linux_virtual_machine.linux_vm[count.index].id
publisher = "Microsoft.EnterpriseCloud.Monitoring"
type = "OmsAgentForLinux"
type_handler_version = "1.13"
auto_upgrade_minor_version = true
settings = <<SETTINGS
{
"workspaceId": "${data.azurerm_log_analytics_workspace.logws.0.workspace_id}"
}
SETTINGS
protected_settings = <<PROTECTED_SETTINGS
{
"workspaceKey": "${data.azurerm_log_analytics_workspace.logws.0.primary_shared_key}"
}
PROTECTED_SETTINGS
}
#--------------------------------------
# azurerm monitoring diagnostics
#--------------------------------------
resource "azurerm_monitor_diagnostic_setting" "nsg" {
count = var.log_analytics_workspace_name != null && var.hub_storage_account_name != null ? 1 : 0
name = lower("nsg-${var.virtual_machine_name}-diag")
target_resource_id = azurerm_network_security_group.nsg.id
storage_account_id = data.azurerm_storage_account.storeacc.0.id
log_analytics_workspace_id = data.azurerm_log_analytics_workspace.logws.0.id
dynamic "log" {
for_each = var.nsg_diag_logs
content {
category = log.value
enabled = true
retention_policy {
enabled = false
}
}
}
}