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 1
/
main.tf
194 lines (175 loc) · 10.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
#-------------------------------
# Local Declarations
#-------------------------------
locals {
nsg_inbound_rules = { for idx, security_rule in var.nsg_inbound_rules : security_rule.name => {
idx : idx,
security_rule : security_rule,
}
}
import_command = "Import-Module ADDSDeployment"
password_command = "$password = ConvertTo-SecureString ${var.admin_password == null ? element(concat(random_password.passwd.*.result, [""]), 0) : var.admin_password} -AsPlainText -Force"
install_ad_command = "Install-WindowsFeature -Name AD-Domain-Services,DNS -IncludeManagementTools"
configure_ad_command = "Install-ADDSForest -CreateDnsDelegation:$false -DomainMode Win2012R2 -DomainName ${var.active_directory_domain} -DomainNetbiosName ${var.active_directory_netbios_name} -ForestMode Win2012R2 -InstallDns:$true -SafeModeAdministratorPassword $password -Force:$true"
shutdown_command = "shutdown -r -t 60"
exit_code_hack = "exit 0"
powershell_command = "${local.import_command}; ${local.password_command}; ${local.install_ad_command}; ${local.configure_ad_command}; ${local.shutdown_command}; ${local.exit_code_hack}"
}
#----------------------------------------------------------
# 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
}
resource "random_password" "passwd" {
count = 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
}
#---------------------------------------
# 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) # not more than 15 characters
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.windows_distribution_list[lower(var.windows_distribution_name)]["publisher"]
offer = var.windows_distribution_list[lower(var.windows_distribution_name)]["offer"]
sku = var.windows_distribution_list[lower(var.windows_distribution_name)]["sku"]
version = var.windows_distribution_list[lower(var.windows_distribution_name)]["version"]
}
}
os_disk {
storage_account_type = var.os_disk_storage_account_type
caching = "ReadWrite"
}
}
#---------------------------------------
# Promote Domain Controller
#---------------------------------------
resource "azurerm_virtual_machine_extension" "adforest" {
name = "ad-forest-creation"
virtual_machine_id = azurerm_windows_virtual_machine.win_vm.0.id
publisher = "Microsoft.Compute"
type = "CustomScriptExtension"
type_handler_version = "1.10"
settings = <<SETTINGS
{
"commandToExecute": "powershell.exe -Command \"${local.powershell_command}\""
}
SETTINGS
}