-
Notifications
You must be signed in to change notification settings - Fork 1
/
dns.tf
69 lines (60 loc) · 2.9 KB
/
dns.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
locals {
create_dns = var.custom_dns != null
dns_zone_subscription_id = local.create_dns ? split("/", var.custom_dns.dns_zone_id)[2] : ""
dns_zone_resource_group_name = local.create_dns ? split("/", var.custom_dns.dns_zone_id)[4] : ""
dns_zone_name = local.create_dns ? split("/", var.custom_dns.dns_zone_id)[8] : ""
# Maps create more meaningful terraform state names than counts (which can cause errors if you re-order the list)
dns_cname_list = local.create_dns ? [for h in var.custom_dns.hostnames : h if h != "@"] : []
dns_cname_map = { for h in local.dns_cname_list : h => h }
dns_naked_a_record = local.create_dns ? contains(var.custom_dns.hostnames, "@") : false
}
# data.azurerm_dns_zone.custom.0
data "azurerm_dns_zone" "custom" {
count = local.create_dns ? 1 : 0
name = local.dns_zone_name
resource_group_name = local.dns_zone_resource_group_name
}
resource "azurerm_dns_cname_record" "cnames_to_function" {
for_each = local.dns_cname_map
name = each.key
zone_name = data.azurerm_dns_zone.custom.0.name
resource_group_name = data.azurerm_dns_zone.custom.0.resource_group_name
ttl = 60
record = azurerm_linux_function_app.static_site.default_hostname
tags = var.tags
}
# need a wait for eventual consistency?
data "dns_a_record_set" "function" {
host = azurerm_linux_function_app.static_site.default_hostname
depends_on = [azurerm_linux_function_app.static_site]
}
resource "azurerm_dns_a_record" "naked_domain" {
count = local.dns_naked_a_record ? 1 : 0
name = "@"
zone_name = data.azurerm_dns_zone.custom.0.name
resource_group_name = data.azurerm_dns_zone.custom.0.resource_group_name
ttl = 300
records = data.dns_a_record_set.function.addrs
}
resource "azurerm_dns_txt_record" "function_domain_verification" {
for_each = local.hostnames
name = each.value.verification_name
zone_name = data.azurerm_dns_zone.custom.0.name
resource_group_name = data.azurerm_dns_zone.custom.0.resource_group_name
ttl = 30
record {
value = azurerm_linux_function_app.static_site.custom_domain_verification_id
}
# It takes time for DNS to propagate. If the check is made immediately, then the check was sometimes failing.
# If this blows up, just increase the duration or rerun apply
provisioner "local-exec" {
command = "sleep 10s"
}
}
resource "azurerm_app_service_custom_hostname_binding" "static_site" {
for_each = local.hostnames
hostname = each.value.full_domain
app_service_name = azurerm_linux_function_app.static_site.name
resource_group_name = azurerm_resource_group.static_site.name
depends_on = [azurerm_dns_txt_record.function_domain_verification]
}