-
Notifications
You must be signed in to change notification settings - Fork 304
/
main.tf
210 lines (180 loc) · 8.18 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
module "os" {
source = "./os"
vm_os_simple = var.vm_os_simple
}
data "azurerm_resource_group" "vm" {
name = var.resource_group_name
}
resource "random_id" "vm-sa" {
keepers = {
vm_hostname = var.vm_hostname
}
byte_length = 6
}
resource "azurerm_storage_account" "vm-sa" {
count = var.boot_diagnostics ? 1 : 0
name = "bootdiag${lower(random_id.vm-sa.hex)}"
resource_group_name = data.azurerm_resource_group.vm.name
location = coalesce(var.location, data.azurerm_resource_group.vm.location)
account_tier = element(split("_", var.boot_diagnostics_sa_type), 0)
account_replication_type = element(split("_", var.boot_diagnostics_sa_type), 1)
tags = var.tags
}
resource "azurerm_virtual_machine" "vm-linux" {
count = ! contains(list(var.vm_os_simple, var.vm_os_offer), "WindowsServer") && ! var.is_windows_image ? var.nb_instances : 0
name = "${var.vm_hostname}-vmLinux-${count.index}"
resource_group_name = data.azurerm_resource_group.vm.name
location = coalesce(var.location, data.azurerm_resource_group.vm.location)
availability_set_id = azurerm_availability_set.vm.id
vm_size = var.vm_size
network_interface_ids = [element(azurerm_network_interface.vm.*.id, count.index)]
delete_os_disk_on_termination = var.delete_os_disk_on_termination
storage_image_reference {
id = var.vm_os_id
publisher = var.vm_os_id == "" ? coalesce(var.vm_os_publisher, module.os.calculated_value_os_publisher) : ""
offer = var.vm_os_id == "" ? coalesce(var.vm_os_offer, module.os.calculated_value_os_offer) : ""
sku = var.vm_os_id == "" ? coalesce(var.vm_os_sku, module.os.calculated_value_os_sku) : ""
version = var.vm_os_id == "" ? var.vm_os_version : ""
}
storage_os_disk {
name = "osdisk-${var.vm_hostname}-${count.index}"
create_option = "FromImage"
caching = "ReadWrite"
managed_disk_type = var.storage_account_type
}
dynamic storage_data_disk {
for_each = range(var.nb_data_disk)
content {
name = "${var.vm_hostname}-datadisk-${count.index}-${storage_data_disk.value}"
create_option = "Empty"
lun = storage_data_disk.value
disk_size_gb = var.data_disk_size_gb
managed_disk_type = var.data_sa_type
}
}
os_profile {
computer_name = "${var.vm_hostname}-${count.index}"
admin_username = var.admin_username
admin_password = var.admin_password
custom_data = var.custom_data
}
os_profile_linux_config {
disable_password_authentication = var.enable_ssh_key
dynamic ssh_keys {
for_each = var.enable_ssh_key ? [var.ssh_key] : []
content {
path = "/home/${var.admin_username}/.ssh/authorized_keys"
key_data = file(var.ssh_key)
}
}
}
tags = var.tags
boot_diagnostics {
enabled = var.boot_diagnostics
storage_uri = var.boot_diagnostics ? join(",", azurerm_storage_account.vm-sa.*.primary_blob_endpoint) : ""
}
}
resource "azurerm_virtual_machine" "vm-windows" {
count = (var.is_windows_image || contains(list(var.vm_os_simple, var.vm_os_offer), "WindowsServer")) ? var.nb_instances : 0
name = "${var.vm_hostname}-vmWindows-${count.index}"
resource_group_name = data.azurerm_resource_group.vm.name
location = coalesce(var.location, data.azurerm_resource_group.vm.location)
availability_set_id = azurerm_availability_set.vm.id
vm_size = var.vm_size
network_interface_ids = [element(azurerm_network_interface.vm.*.id, count.index)]
delete_os_disk_on_termination = var.delete_os_disk_on_termination
storage_image_reference {
id = var.vm_os_id
publisher = var.vm_os_id == "" ? coalesce(var.vm_os_publisher, module.os.calculated_value_os_publisher) : ""
offer = var.vm_os_id == "" ? coalesce(var.vm_os_offer, module.os.calculated_value_os_offer) : ""
sku = var.vm_os_id == "" ? coalesce(var.vm_os_sku, module.os.calculated_value_os_sku) : ""
version = var.vm_os_id == "" ? var.vm_os_version : ""
}
storage_os_disk {
name = "${var.vm_hostname}-osdisk-${count.index}"
create_option = "FromImage"
caching = "ReadWrite"
managed_disk_type = var.storage_account_type
}
dynamic storage_data_disk {
for_each = range(var.nb_data_disk)
content {
name = "${var.vm_hostname}-datadisk-${count.index}-${storage_data_disk.value}"
create_option = "Empty"
lun = storage_data_disk.value
disk_size_gb = var.data_disk_size_gb
managed_disk_type = var.data_sa_type
}
}
os_profile {
computer_name = "${var.vm_hostname}-${count.index}"
admin_username = var.admin_username
admin_password = var.admin_password
}
tags = var.tags
os_profile_windows_config {
provision_vm_agent = true
}
boot_diagnostics {
enabled = var.boot_diagnostics
storage_uri = var.boot_diagnostics ? join(",", azurerm_storage_account.vm-sa.*.primary_blob_endpoint) : ""
}
}
resource "azurerm_availability_set" "vm" {
name = "${var.vm_hostname}-avset"
resource_group_name = data.azurerm_resource_group.vm.name
location = coalesce(var.location, data.azurerm_resource_group.vm.location)
platform_fault_domain_count = 2
platform_update_domain_count = 2
managed = true
tags = var.tags
}
resource "azurerm_public_ip" "vm" {
count = var.nb_public_ip
name = "${var.vm_hostname}-pip-${count.index}"
resource_group_name = data.azurerm_resource_group.vm.name
location = coalesce(var.location, data.azurerm_resource_group.vm.location)
allocation_method = var.allocation_method
domain_name_label = element(var.public_ip_dns, count.index)
tags = var.tags
}
resource "azurerm_network_security_group" "vm" {
name = "${var.vm_hostname}-nsg"
resource_group_name = data.azurerm_resource_group.vm.name
location = coalesce(var.location, data.azurerm_resource_group.vm.location)
tags = var.tags
}
resource "azurerm_network_security_rule" "vm" {
count = var.remote_port != "" ? 1 : 0
name = "allow_remote_${coalesce(var.remote_port, module.os.calculated_remote_port)}_in_all"
resource_group_name = data.azurerm_resource_group.vm.name
description = "Allow remote protocol in from all locations"
priority = 101
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = coalesce(var.remote_port, module.os.calculated_remote_port)
source_address_prefixes = var.source_address_prefixes
destination_address_prefix = "*"
network_security_group_name = azurerm_network_security_group.vm.name
}
resource "azurerm_network_interface" "vm" {
count = var.nb_instances
name = "${var.vm_hostname}-nic-${count.index}"
resource_group_name = data.azurerm_resource_group.vm.name
location = coalesce(var.location, data.azurerm_resource_group.vm.location)
enable_accelerated_networking = var.enable_accelerated_networking
ip_configuration {
name = "${var.vm_hostname}-ip-${count.index}"
subnet_id = var.vnet_subnet_id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = length(azurerm_public_ip.vm.*.id) > 0 ? element(concat(azurerm_public_ip.vm.*.id, list("")), count.index) : ""
}
tags = var.tags
}
resource "azurerm_network_interface_security_group_association" "test" {
count = var.nb_instances
network_interface_id = azurerm_network_interface.vm[count.index].id
network_security_group_id = azurerm_network_security_group.vm.id
}