forked from weka/terraform-azure-weka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmss.tf
186 lines (172 loc) · 7.82 KB
/
vmss.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
data "azurerm_resource_group" "rg" {
name = var.rg_name
}
data "azurerm_subnet" "subnet" {
resource_group_name = var.vnet_rg_name
virtual_network_name = var.vnet_name
name = var.subnet_name
}
data "azurerm_virtual_network" "vnet" {
name = var.vnet_name
resource_group_name = var.vnet_rg_name
}
# ===================== SSH key ++++++++++++++++++++++++= #
resource "tls_private_key" "ssh_key" {
count = var.ssh_public_key == null ? 1 : 0
algorithm = "RSA"
rsa_bits = 4096
}
resource "local_file" "public_key" {
count = var.ssh_public_key == null ? 1 : 0
content = tls_private_key.ssh_key[count.index].public_key_openssh
filename = "${local.ssh_path}-public-key.pub"
file_permission = "0600"
}
resource "local_file" "private_key" {
count = var.ssh_public_key == null ? 1 : 0
content = tls_private_key.ssh_key[count.index].private_key_pem
filename = "${local.ssh_path}-private-key.pem"
file_permission = "0600"
}
locals {
ssh_path = "/tmp/${var.prefix}-${var.cluster_name}"
public_ssh_key = var.ssh_public_key == null ? tls_private_key.ssh_key[0].public_key_openssh : file(var.ssh_public_key)
private_ssh_key = var.ssh_private_key == null ? tls_private_key.ssh_key[0].private_key_pem : file(var.ssh_private_key)
disk_size = var.default_disk_size + var.traces_per_ionode * (var.container_number_map[var.instance_type].compute + var.container_number_map[var.instance_type].drive + var.container_number_map[var.instance_type].frontend)
private_nic_first_index = var.private_network ? 0 : 1
alphanumeric_cluster_name = lower(replace(var.cluster_name, "/\\W|_|\\s/", ""))
alphanumeric_prefix_name = lower(replace(var.prefix, "/\\W|_|\\s/", ""))
subnet_range = data.azurerm_subnet.subnet.address_prefix
nics_numbers = var.install_cluster_dpdk ? var.container_number_map[var.instance_type].nics : 1
custom_data_script = templatefile("${path.module}/user-data.sh", {
apt_repo_url = var.apt_repo_url
private_ssh_key = local.private_ssh_key
user = var.vm_username
install_cluster_dpdk = var.install_cluster_dpdk
subnet_range = local.subnet_range
nics_num = local.nics_numbers
deploy_url = "https://${var.prefix}-${var.cluster_name}-function-app.azurewebsites.net/api/deploy"
report_url = "https://${var.prefix}-${var.cluster_name}-function-app.azurewebsites.net/api/report"
function_app_default_key = data.azurerm_function_app_host_keys.function_keys.default_function_key
disk_size = local.disk_size
})
}
resource "azurerm_proximity_placement_group" "ppg" {
name = "${var.prefix}-${var.cluster_name}-backend-ppg"
location = data.azurerm_resource_group.rg.location
resource_group_name = var.rg_name
tags = merge(var.tags_map, { "weka_cluster" : var.cluster_name })
lifecycle {
ignore_changes = [tags]
}
}
resource "azurerm_linux_virtual_machine_scale_set" "vmss" {
name = "${var.prefix}-${var.cluster_name}-vmss"
location = data.azurerm_resource_group.rg.location
resource_group_name = var.rg_name
sku = var.instance_type
upgrade_mode = "Manual"
health_probe_id = azurerm_lb_probe.backend_lb_probe.id
admin_username = var.vm_username
instances = var.cluster_size
computer_name_prefix = "${var.prefix}-${var.cluster_name}-backend"
custom_data = base64encode(local.custom_data_script)
disable_password_authentication = true
proximity_placement_group_id = azurerm_proximity_placement_group.ppg.id
source_image_id = var.source_image_id
tags = merge(var.tags_map, {
"weka_cluster" : var.cluster_name, "user_id" : data.azurerm_client_config.current.object_id
})
os_disk {
caching = "ReadWrite"
storage_account_type = "StandardSSD_LRS"
}
data_disk {
lun = 0
caching = "ReadWrite"
create_option = "Empty"
disk_size_gb = local.disk_size
storage_account_type = "StandardSSD_LRS"
}
admin_ssh_key {
username = var.vm_username
public_key = local.public_ssh_key
}
identity {
type = "SystemAssigned"
}
dynamic "network_interface" {
for_each = range(local.private_nic_first_index)
content {
name = "${var.prefix}-${var.cluster_name}-backend-nic-0"
network_security_group_id = var.sg_id
primary = true
enable_accelerated_networking = var.install_cluster_dpdk
ip_configuration {
primary = true
name = "ipconfig0"
subnet_id = data.azurerm_subnet.subnet.id
load_balancer_backend_address_pool_ids = [azurerm_lb_backend_address_pool.lb_backend_pool.id]
public_ip_address {
name = "${var.prefix}-${var.cluster_name}-public-ip"
domain_name_label = "${var.prefix}-${var.cluster_name}-backend"
}
}
}
}
dynamic "network_interface" {
for_each = range(local.private_nic_first_index, 1)
content {
name = "${var.prefix}-${var.cluster_name}-backend-nic-0"
network_security_group_id = var.sg_id
primary = true
enable_accelerated_networking = var.install_cluster_dpdk
ip_configuration {
primary = true
name = "ipconfig0"
subnet_id = data.azurerm_subnet.subnet.id
load_balancer_backend_address_pool_ids = [azurerm_lb_backend_address_pool.lb_backend_pool.id]
}
}
}
dynamic "network_interface" {
for_each = range(1, local.nics_numbers)
content {
name = "${var.prefix}-${var.cluster_name}-backend-nic-${network_interface.value}"
network_security_group_id = var.sg_id
primary = false
enable_accelerated_networking = var.install_cluster_dpdk
ip_configuration {
primary = false
name = "ipconfig${network_interface.value}"
subnet_id = data.azurerm_subnet.subnet.id
load_balancer_backend_address_pool_ids = [azurerm_lb_backend_address_pool.lb_backend_pool.id]
}
}
}
lifecycle {
ignore_changes = [instances, custom_data, tags]
}
depends_on = [
azurerm_lb_backend_address_pool.lb_backend_pool, azurerm_lb_probe.backend_lb_probe,
azurerm_proximity_placement_group.ppg, azurerm_lb_rule.backend_lb_rule, azurerm_lb_rule.ui_lb_rule
]
}
resource "azurerm_role_assignment" "vm_role_assignment" {
scope = data.azurerm_resource_group.rg.id
role_definition_name = "Contributor"
principal_id = azurerm_linux_virtual_machine_scale_set.vmss.identity[0].principal_id
depends_on = [azurerm_linux_virtual_machine_scale_set.vmss]
}
resource "null_resource" "force-delete-vmss" {
triggers = {
vmss_name = azurerm_linux_virtual_machine_scale_set.vmss.name
rg_name = data.azurerm_resource_group.rg.name
subscription_id = var.subscription_id
}
provisioner "local-exec" {
when = destroy
command = "az vmss delete --name ${self.triggers.vmss_name} --resource-group ${self.triggers.rg_name} --force-deletion true --subscription ${self.triggers.subscription_id}"
}
depends_on = [azurerm_linux_virtual_machine_scale_set.vmss]
}