-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathvm-web.tf
185 lines (159 loc) · 6.69 KB
/
vm-web.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
# Create Network Security Group and rule
resource "azurerm_network_security_group" "tfwebnsg" {
name = "${var.resource.prefix}-webnsg"
location = var.resource.location
resource_group_name = azurerm_resource_group.tfrg.name
security_rule {
name = "web"
priority = 1001
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "80"
source_address_prefix = "*"
destination_address_prefix = "*"
}
tags = {
environment = var.resource.tag
}
}
# Create network interface
resource "azurerm_network_interface" "tfwebnic" {
count = var.vm.webcount
name = "${var.resource.prefix}-webnic${count.index}"
location = var.resource.location
resource_group_name = azurerm_resource_group.tfrg.name
#-network_security_group_id = azurerm_network_security_group.tfwebnsg.id
ip_configuration {
name = "${var.resource.prefix}-webnic-config${count.index}"
subnet_id = azurerm_subnet.tfwebvnet.id
#private_ip_address_allocation = "dynamic"
private_ip_address_allocation = "Static"
private_ip_address = format("10.0.1.%d", count.index + 4)
}
tags = {
environment = var.resource.tag
}
}
resource "azurerm_network_interface_security_group_association" "tfwebnic" {
count = var.vm.webcount
network_interface_id = azurerm_network_interface.tfwebnic[count.index].id
network_security_group_id = azurerm_network_security_group.tfwebnsg.id
}
resource "azurerm_network_interface_backend_address_pool_association" "tfwebpoolassc" {
count = var.vm.webcount
network_interface_id = element(azurerm_network_interface.tfwebnic.*.id, count.index)
ip_configuration_name = "${var.resource.prefix}-webnic-config${count.index}"
backend_address_pool_id = azurerm_lb_backend_address_pool.tflbbackendpool.id
}
resource "azurerm_network_interface_nat_rule_association" "tfnatruleassc" {
count = var.vm.webcount
network_interface_id = element(azurerm_network_interface.tfwebnic.*.id, count.index)
ip_configuration_name = "${var.resource.prefix}-webnic-config${count.index}"
nat_rule_id = element(azurerm_lb_nat_rule.lbnatrule.*.id, count.index)
}
resource "azurerm_network_interface_application_security_group_association" "tfwebsecassc" {
count = var.vm.webcount
network_interface_id = element(azurerm_network_interface.tfwebnic.*.id, count.index)
#-ip_configuration_name = "${var.resource.prefix}-webnic-config${count.index}"
application_security_group_id = azurerm_application_security_group.tfwebasg.id
}
resource "azurerm_availability_set" "tfwebavset" {
name = "${var.resource.prefix}-webavset"
location = var.resource.location
resource_group_name = azurerm_resource_group.tfrg.name
managed = "true"
platform_fault_domain_count = 2 # default 3 not working in some regions like Korea
tags = {
environment = var.resource.tag
}
}
# Create virtual machine
# https://www.terraform.io/docs/providers/azurerm/r/linux_virtual_machine.html
resource "azurerm_linux_virtual_machine" "tfwebvm" {
count = var.vm.webcount
name = "${var.resource.prefix}webvm${count.index}"
location = var.resource.location
resource_group_name = azurerm_resource_group.tfrg.name
network_interface_ids = [azurerm_network_interface.tfwebnic[count.index].id]
size = var.vm.size
availability_set_id = azurerm_availability_set.tfwebavset.id
computer_name = format("tfwebvm%03d", count.index + 1)
admin_username = var.vm.admin_username
admin_password = var.vm.admin_password
disable_password_authentication = false
custom_data = base64encode( file("./script/cloud-init.txt") )
os_disk {
name = format("%s-web-%03d-osdisk", var.resource.prefix, count.index + 1)
caching = "ReadWrite"
storage_account_type = "Premium_LRS"
}
//source_image_id = "${var.vm.osimageuri}"
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
tags = {
environment = var.resource.tag
}
}
resource "azurerm_public_ip" "tflbpip" {
name = "${var.resource.prefix}-flbpip"
location = var.resource.location
resource_group_name = azurerm_resource_group.tfrg.name
allocation_method = "Static"
sku = "Standard"
#zones = ["1"]
}
resource "azurerm_lb" "tflb" {
name = "${var.resource.prefix}lb"
location = var.resource.location
resource_group_name = azurerm_resource_group.tfrg.name
sku = "Standard"
frontend_ip_configuration {
name = "PublicIPAddress"
public_ip_address_id = azurerm_public_ip.tflbpip.id
}
}
resource "azurerm_lb_backend_address_pool" "tflbbackendpool" {
##resource_group_name = azurerm_resource_group.tfrg.name
loadbalancer_id = azurerm_lb.tflb.id
name = "BackEndAddressPool"
}
resource "azurerm_lb_nat_rule" "lbnatrule" {
count = var.vm.webcount
resource_group_name = azurerm_resource_group.tfrg.name
loadbalancer_id = azurerm_lb.tflb.id
name = "ssh-${count.index}"
protocol = "Tcp"
frontend_port = "5000${count.index + 1}"
backend_port = 22
frontend_ip_configuration_name = "PublicIPAddress" # "${azurerm_lb.tflb.frontend_ip_configuration.name}" not working
}
resource "azurerm_lb_rule" "lb_rule" {
loadbalancer_id = azurerm_lb.tflb.id
name = "LBRule"
protocol = "Tcp"
frontend_port = 80
backend_port = 80
frontend_ip_configuration_name = "PublicIPAddress"
enable_floating_ip = false
backend_address_pool_ids = [azurerm_lb_backend_address_pool.tflbbackendpool.id]
idle_timeout_in_minutes = 5
probe_id = azurerm_lb_probe.lb_probe.id
depends_on = [azurerm_lb_probe.lb_probe]
}
resource "azurerm_lb_probe" "lb_probe" {
loadbalancer_id = azurerm_lb.tflb.id
name = "tcpProbe"
protocol = "Tcp"
port = 80
interval_in_seconds = 5
number_of_probes = 2
}
output "weblb_pip" {
value = azurerm_public_ip.tflbpip.*.ip_address
}