-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
344 lines (291 loc) · 11.4 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
locals {
value_list = [for i in range(var.storage_account_count) : "${var.name}${i + 1}"]
}
data "azurerm_client_config" "current" {
}
data "azurerm_client_config" "currentdns" {
provider = azurerm.dns
}
data "azurerm_dns_zone" "example" {
name = var.domain
provider = azurerm.dns
resource_group_name = var.domain_rg
}
resource "azurerm_dns_cname_record" "example" {
for_each = toset(local.value_list)
name = "asverify.${each.value}"
provider = azurerm.dns
zone_name = data.azurerm_dns_zone.example.name
resource_group_name = data.azurerm_dns_zone.example.resource_group_name
ttl = 300
record = "asverify.${each.value}.z6.web.core.windows.net"
}
resource "azurerm_resource_group" "demo_rg_terraformazureappgw" {
name = "demo-rg-terraformazureappgw"
location = "West Europe"
}
resource "azurerm_storage_account" "demo_sa_terraformazureappgw" {
for_each = toset(local.value_list)
name = each.value
resource_group_name = azurerm_resource_group.demo_rg_terraformazureappgw.name
location = azurerm_resource_group.demo_rg_terraformazureappgw.location
account_tier = "Standard"
account_replication_type = "LRS"
min_tls_version = "TLS1_2"
allow_nested_items_to_be_public = true
static_website {
index_document = "index.html"
}
custom_domain {
name = "${each.value}.${var.domain}"
use_subdomain = true
}
tags = {
environment = "demo"
}
lifecycle {
ignore_changes = [
custom_domain.0.use_subdomain,
]
}
depends_on = [azurerm_dns_cname_record.example]
}
data "azurerm_storage_container" "example" {
for_each = toset(local.value_list)
name = "$web"
storage_account_name = azurerm_storage_account.demo_sa_terraformazureappgw[each.value].name
}
resource "local_file" "htmlfile" {
for_each = toset(local.value_list)
content = each.value
filename = "content/index-${each.value}.html"
}
resource "azurerm_storage_blob" "example" {
for_each = toset(local.value_list)
name = "index.html"
storage_account_name = azurerm_storage_account.demo_sa_terraformazureappgw[each.value].name
storage_container_name = data.azurerm_storage_container.example[each.value].name
type = "Block"
content_type = "text/html"
source = "content/index-${each.value}.html"
}
resource "azurerm_virtual_network" "example" {
name = "example-network"
resource_group_name = azurerm_resource_group.demo_rg_terraformazureappgw.name
location = azurerm_resource_group.demo_rg_terraformazureappgw.location
address_space = ["10.254.0.0/16"]
}
resource "azurerm_subnet" "frontend" {
name = "frontend"
resource_group_name = azurerm_resource_group.demo_rg_terraformazureappgw.name
virtual_network_name = azurerm_virtual_network.example.name
address_prefixes = ["10.254.0.0/24"]
}
resource "azurerm_subnet" "backend" {
name = "backend"
resource_group_name = azurerm_resource_group.demo_rg_terraformazureappgw.name
virtual_network_name = azurerm_virtual_network.example.name
address_prefixes = ["10.254.2.0/24"]
}
resource "azurerm_public_ip" "example" {
name = "example-pip"
resource_group_name = azurerm_resource_group.demo_rg_terraformazureappgw.name
location = azurerm_resource_group.demo_rg_terraformazureappgw.location
allocation_method = "Static"
sku = "Standard"
domain_name_label = "demoterraformazureappgw"
}
# since these variables are re-used - a locals block makes this more maintainable
locals {
backend_address_pool_name = "${azurerm_virtual_network.example.name}-beap"
frontend_port_name = "${azurerm_virtual_network.example.name}-feport"
frontend_ip_configuration_name = "${azurerm_virtual_network.example.name}-feip"
http_setting_name = "${azurerm_virtual_network.example.name}-be-htst"
listener_name = "${azurerm_virtual_network.example.name}-httplstn"
request_routing_rule_name = "${azurerm_virtual_network.example.name}-rqrt"
redirect_configuration_name = "${azurerm_virtual_network.example.name}-rdrcfg"
}
resource "azurerm_application_gateway" "network" {
name = "example-appgateway"
resource_group_name = azurerm_resource_group.demo_rg_terraformazureappgw.name
location = azurerm_resource_group.demo_rg_terraformazureappgw.location
sku {
name = "Standard_v2"
tier = "Standard_v2"
capacity = 1
}
gateway_ip_configuration {
name = "my-gateway-ip-configuration"
subnet_id = azurerm_subnet.frontend.id
}
frontend_port {
name = local.frontend_port_name
port = 443
}
frontend_ip_configuration {
name = local.frontend_ip_configuration_name
public_ip_address_id = azurerm_public_ip.example.id
}
dynamic "backend_address_pool" {
for_each = toset(local.value_list)
content {
name = "${local.backend_address_pool_name}-${backend_address_pool.value}"
fqdns = ["${backend_address_pool.value}.z6.web.core.windows.net"]
}
}
backend_http_settings {
name = local.http_setting_name
cookie_based_affinity = "Disabled"
port = 443
protocol = "Https"
request_timeout = 60
pick_host_name_from_backend_address = true
}
dynamic "http_listener" {
for_each = toset(local.value_list)
content {
name = "${local.listener_name}-${http_listener.value}"
frontend_ip_configuration_name = local.frontend_ip_configuration_name
frontend_port_name = local.frontend_port_name
protocol = "Https"
host_name = "${http_listener.value}.${var.domain}"
ssl_certificate_name = "${http_listener.value}-${replace(var.domain, ".", "-")}"
require_sni = true
}
}
dynamic "request_routing_rule" {
for_each = toset(local.value_list)
content {
name = "${local.request_routing_rule_name}-${request_routing_rule.value}"
rule_type = "Basic"
http_listener_name = "${local.listener_name}-${request_routing_rule.value}"
backend_address_pool_name = "${local.backend_address_pool_name}-${request_routing_rule.value}"
backend_http_settings_name = local.http_setting_name
priority = (index(local.value_list, request_routing_rule.value) + 210)
}
}
dynamic "ssl_certificate" {
for_each = toset(local.value_list)
content {
name = "${ssl_certificate.value}-${replace(var.domain, ".", "-")}"
key_vault_secret_id = azurerm_key_vault_certificate.example[ssl_certificate.value].versionless_secret_id
}
}
identity {
type = "UserAssigned"
identity_ids = [
azurerm_user_assigned_identity.example.id,
]
}
ssl_policy {
policy_type = "Predefined"
policy_name = "AppGwSslPolicy20220101S"
}
}
resource "azurerm_dns_a_record" "example2" {
for_each = toset(local.value_list)
name = each.value
provider = azurerm.dns
zone_name = data.azurerm_dns_zone.example.name
resource_group_name = data.azurerm_dns_zone.example.resource_group_name
ttl = 300
records = [azurerm_public_ip.example.ip_address]
}
resource "azuread_application" "example" {
display_name = "Demo Terraform Azure App Gateway"
}
resource "azuread_service_principal" "example" {
application_id = azuread_application.example.application_id
}
resource "time_rotating" "example" {
rotation_days = 7
}
resource "azuread_service_principal_password" "example" {
service_principal_id = azuread_service_principal.example.object_id
rotate_when_changed = {
rotation = time_rotating.example.id
}
}
resource "azurerm_role_assignment" "example" {
scope = data.azurerm_dns_zone.example.id
role_definition_name = "DNS Zone Contributor"
principal_id = azuread_service_principal.example.object_id
}
resource "tls_private_key" "private_key" {
algorithm = "RSA"
}
resource "acme_registration" "reg" {
account_key_pem = tls_private_key.private_key.private_key_pem
email_address = "letsencrypt@techie.cloud"
}
resource "random_password" "password" {
for_each = toset(local.value_list)
length = 16
special = true
override_special = "_%@"
}
resource "acme_certificate" "certificate" {
for_each = toset(local.value_list)
account_key_pem = acme_registration.reg.account_key_pem
common_name = "${each.value}.${var.domain}"
certificate_p12_password = random_password.password[each.value].result
dns_challenge {
provider = "azure"
config = {
AZURE_CLIENT_ID = azuread_application.example.application_id
AZURE_CLIENT_SECRET = azuread_service_principal_password.example.value
AZURE_ZONE_NAME = data.azurerm_dns_zone.example.name
AZURE_SUBSCRIPTION_ID = data.azurerm_client_config.currentdns.subscription_id
AZURE_TENANT_ID = data.azurerm_client_config.currentdns.tenant_id
AZURE_RESOURCE_GROUP = data.azurerm_dns_zone.example.resource_group_name
}
}
}
resource "azurerm_key_vault" "example" {
name = var.name
resource_group_name = azurerm_resource_group.demo_rg_terraformazureappgw.name
location = azurerm_resource_group.demo_rg_terraformazureappgw.location
tenant_id = data.azurerm_client_config.current.tenant_id
sku_name = "standard"
purge_protection_enabled = true
}
resource "azurerm_user_assigned_identity" "example" {
resource_group_name = azurerm_resource_group.demo_rg_terraformazureappgw.name
location = azurerm_resource_group.demo_rg_terraformazureappgw.location
name = "${var.name}-identity"
}
resource "azurerm_key_vault_access_policy" "example" {
key_vault_id = azurerm_key_vault.example.id
tenant_id = data.azurerm_client_config.current.tenant_id
object_id = azurerm_user_assigned_identity.example.principal_id
certificate_permissions = [
"Get", "List"
]
secret_permissions = [
"Get",
]
}
resource "azurerm_key_vault_access_policy" "example2" {
key_vault_id = azurerm_key_vault.example.id
tenant_id = data.azurerm_client_config.current.tenant_id
object_id = data.azurerm_client_config.current.object_id
certificate_permissions = [
"Get", "List", "Create", "Import", "Update", "Delete", "Backup", "Restore", "Recover"
]
secret_permissions = [
"Get", "List", "Set", "Purge", "Delete", "Backup", "Restore", "Recover"
]
}
resource "azurerm_key_vault_certificate" "example" {
for_each = toset(local.value_list)
name = "${each.value}-${replace(var.domain, ".", "-")}"
key_vault_id = azurerm_key_vault.example.id
certificate {
contents = acme_certificate.certificate[each.value].certificate_p12
password = random_password.password[each.value].result
}
depends_on = [
azurerm_key_vault_access_policy.example,
azurerm_key_vault_access_policy.example2,
]
}