forked from kendrickcc/odm-azure-wf1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webODM.tf
242 lines (241 loc) · 8.44 KB
/
webODM.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
#-------------------------------
# Terraform provider and backend
#-------------------------------
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "2.95.0"
}
}
#/* disabling the backend, remove the leading "#"
backend "azurerm" {
resource_group_name = "odm-rsg"
#storage_account_name = Stored as a GitHub secret
container_name = "tfstates"
key = "terraform.tfstate"
} # */
}
provider "azurerm" {
features {}
}
#-------------------------------
# Define tags, edit in variables
#------------------------------
locals {
common_tags = {
environment = "${var.repo_name}"
project = "${var.project}"
Owner = "${var.repo_owner}"
}
/*
extra_tags = {
network = "${var.network1_name}"
support = "${var.network_support_name}"
}*/
}
#-------------------------------
# Get cloud-init template file
#-------------------------------
data "template_file" "webodm" {
template = file("webodm.tpl")
vars = {
ssh_key = var.pub_key_data
}
}
data "template_file" "nodeodm" {
template = file("nodeodm.tpl")
vars = {
ssh_key = var.pub_key_data
}
}
#-------------------------------
# Create resource group
#-------------------------------
resource "azurerm_resource_group" "rg" {
name = "${var.prefix}-rsg"
location = var.location
tags = merge(local.common_tags)
}
#-------------------------------
# Networking
#-------------------------------
resource "azurerm_public_ip" "webodm" {
name = "${var.prefix}-webodm${count.index}-pip"
count = var.webodm_servers
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
allocation_method = "Dynamic"
sku = "Basic"
tags = merge(local.common_tags)
}
resource "azurerm_public_ip" "nodeodm" {
name = "${var.prefix}-nodeodm${count.index}-pip"
count = var.nodeodm_servers
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
allocation_method = "Dynamic"
sku = "Basic"
tags = merge(local.common_tags)
}
resource "azurerm_virtual_network" "rg" {
name = "${var.prefix}-network"
address_space = [var.vnet_cidr]
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
tags = merge(local.common_tags)
}
resource "azurerm_subnet" "internal" {
name = "internal"
resource_group_name = azurerm_resource_group.rg.name
virtual_network_name = azurerm_virtual_network.rg.name
address_prefixes = [var.subnet_cidr]
}
resource "azurerm_network_interface" "webodm" {
name = "${var.prefix}-webodm${count.index}-nic"
count = var.webodm_servers
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.internal.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.webodm[count.index].id
}
tags = merge(local.common_tags)
}
resource "azurerm_network_interface" "nodeodm" {
name = "${var.prefix}-nodeodm${count.index}-nic"
count = var.nodeodm_servers
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.internal.id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = azurerm_public_ip.nodeodm[count.index].id
}
tags = merge(local.common_tags)
}
#-------------------------------
# Network security group
#-------------------------------
resource "azurerm_network_security_group" "nsg" {
name = "${var.prefix}-nsg"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
tags = merge(local.common_tags)
#/* when needed to connect to VM, add a leading "#"
security_rule {
name = "SSH"
priority = 300
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefix = "0.0.0.0/0"
destination_address_prefix = "*"
} # */
security_rule {
name = "AllowWebODMInBound"
priority = 400
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "8000"
source_address_prefix = "0.0.0.0/0"
destination_address_prefix = "*"
}
security_rule {
name = "AllowClusterODMInBound"
priority = 401
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "8001"
source_address_prefix = "0.0.0.0/0"
destination_address_prefix = "*"
}
}
resource "azurerm_subnet_network_security_group_association" "sec_group" {
subnet_id = azurerm_subnet.internal.id
network_security_group_id = azurerm_network_security_group.nsg.id
}
#-------------------------------
# Create virtual machines
#-------------------------------
resource "azurerm_linux_virtual_machine" "webodm" {
name = "${var.prefix}-webodm${count.index}-vm"
count = var.webodm_servers
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
size = var.vmSize
admin_username = var.adminUser
network_interface_ids = [
azurerm_network_interface.webodm[count.index].id,
]
computer_name = "${var.prefix}-webodm${count.index}-vm"
disable_password_authentication = true
custom_data = base64encode(data.template_file.webodm.rendered)
source_image_reference {
publisher = element(split(",", lookup(var.standard_os, var.simple_os, "")), 0)
offer = element(split(",", lookup(var.standard_os, var.simple_os, "")), 1)
sku = element(split(",", lookup(var.standard_os, var.simple_os, "")), 2)
version = "latest"
}
os_disk {
storage_account_type = var.storageAccountType
caching = "ReadWrite"
disk_size_gb = var.diskSizeGB
}
admin_ssh_key {
username = var.adminUser
public_key = var.pub_key_data
}
tags = merge(local.common_tags)
}
resource "azurerm_linux_virtual_machine" "nodeodm" {
name = "${var.prefix}-nodeodm${count.index}-vm"
count = var.nodeodm_servers
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
size = var.vmSize
admin_username = var.adminUser
network_interface_ids = [
azurerm_network_interface.nodeodm[count.index].id,
]
computer_name = "${var.prefix}-nodeodm${count.index}-vm"
disable_password_authentication = true
custom_data = base64encode(data.template_file.nodeodm.rendered)
source_image_reference {
publisher = element(split(",", lookup(var.standard_os, var.simple_os, "")), 0)
offer = element(split(",", lookup(var.standard_os, var.simple_os, "")), 1)
sku = element(split(",", lookup(var.standard_os, var.simple_os, "")), 2)
version = "latest"
}
os_disk {
storage_account_type = var.storageAccountType
caching = "ReadWrite"
disk_size_gb = var.diskSizeGB
}
admin_ssh_key {
username = var.adminUser
public_key = var.pub_key_data
}
tags = merge(local.common_tags)
}
#-------------------------------
# Outputs
#-------------------------------
output "WebODM_public_ip_port_8000" {
value = azurerm_linux_virtual_machine.webodm.*.public_ip_addresses
}
output "ClusterODM_private_ip_addresses_port_8001" {
value = azurerm_linux_virtual_machine.webodm.*.private_ip_addresses
}
output "NodeODM_private_ip_addresses_port_3000" {
value = azurerm_linux_virtual_machine.nodeodm.*.private_ip_addresses
}