-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.tf
181 lines (147 loc) · 5.04 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
# Randoms
resource "random_string" "windows_name" {
length = 15
special = false
}
resource "random_string" "username" {
length = 20
special = false
}
resource "random_password" "password" {
count = (var.kernel_type == "windows" && var.admin_password == "" ? 1 : 0)
length = 32
min_lower = 1
min_upper = 1
min_numeric = 1
min_special = 1
special = true
override_special = "!@#$%*()-_=+[]{}:?"
}
resource "tls_private_key" "ssh_key" {
count = (var.kernel_type == "linux" && var.admin_ssh_public_key == "" ? 1 : 0)
algorithm = "RSA"
rsa_bits = 4096
}
resource "random_integer" "count" {
min = 01
max = 99
}
# Network Interfaces
resource "azurerm_public_ip" "primary" {
count = (var.public_ip_enabled == true ? 1 : 0)
name = local.virtual_machine_name
location = var.names.location
resource_group_name = var.resource_group_name
tags = var.tags
allocation_method = "Static"
sku = var.public_ip_sku
}
resource "azurerm_network_interface" "dynamic" {
name = local.virtual_machine_name
location = var.names.location
resource_group_name = var.resource_group_name
tags = var.tags
accelerated_networking_enabled = var.accelerated_networking
ip_configuration {
name = "internal"
subnet_id = var.subnet_id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = var.public_ip_enabled ? azurerm_public_ip.primary[0].id : null
}
}
# Linux Virtual Machine
resource "azurerm_linux_virtual_machine" "linux" {
count = (var.kernel_type == "linux" ? 1 : 0)
name = local.virtual_machine_name
location = var.names.location
resource_group_name = var.resource_group_name
tags = var.tags
size = var.virtual_machine_size
admin_username = local.admin_username
disable_password_authentication = true
network_interface_ids = [azurerm_network_interface.dynamic.id]
proximity_placement_group_id = var.proximity_placement_group
admin_ssh_key {
username = local.admin_username
public_key = local.admin_ssh_public_key
}
source_image_id = var.custom_image_id
custom_data = var.custom_data
dynamic "source_image_reference" {
for_each = var.custom_image_id == null ? ["no_custom_image_provided"] : []
content {
publisher = var.source_image_publisher
offer = var.source_image_offer
sku = var.source_image_sku
version = var.source_image_version
}
}
dynamic "boot_diagnostics" {
for_each = var.enable_boot_diagnostics ? ["enabled"] : []
content {
storage_account_uri = var.diagnostics_storage_account_uri
}
}
os_disk {
caching = var.operating_system_disk_cache
storage_account_type = var.operating_system_disk_type
write_accelerator_enabled = var.operating_system_disk_write_accelerator
}
dynamic "additional_capabilities" {
for_each = var.ultra_ssd_enabled ? ["enabled"] : []
content {
ultra_ssd_enabled = var.ultra_ssd_enabled
}
}
zone = var.availability_zone
identity {
type = var.identity_type
identity_ids = var.identity_ids
}
}
# Windows
resource "azurerm_windows_virtual_machine" "windows" {
count = (var.kernel_type == "windows" ? 1 : 0)
name = local.virtual_machine_name
location = var.names.location
resource_group_name = var.resource_group_name
tags = merge(var.tags, { "name" : local.virtual_machine_name })
size = var.virtual_machine_size
admin_username = local.admin_username
admin_password = local.admin_password
network_interface_ids = [azurerm_network_interface.dynamic.id]
proximity_placement_group_id = var.proximity_placement_group
source_image_id = var.custom_image_id
custom_data = var.custom_data
dynamic "source_image_reference" {
for_each = var.custom_image_id == null ? ["no_custom_image_provided"] : []
content {
publisher = var.source_image_publisher
offer = var.source_image_offer
sku = var.source_image_sku
version = var.source_image_version
}
}
dynamic "boot_diagnostics" {
for_each = var.enable_boot_diagnostics ? ["enabled"] : []
content {
storage_account_uri = var.diagnostics_storage_account_uri
}
}
os_disk {
caching = var.operating_system_disk_cache
storage_account_type = var.operating_system_disk_type
write_accelerator_enabled = var.operating_system_disk_write_accelerator
}
dynamic "additional_capabilities" {
for_each = var.ultra_ssd_enabled ? ["enabled"] : []
content {
ultra_ssd_enabled = var.ultra_ssd_enabled
}
}
zone = var.availability_zone
identity {
type = var.identity_type
identity_ids = var.identity_ids
}
}