-
Notifications
You must be signed in to change notification settings - Fork 569
/
main.tf
69 lines (64 loc) · 2.63 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
# ---------------------------------------------------------------------------------------------------------------------
# Providers
# ---------------------------------------------------------------------------------------------------------------------
terraform {
required_version = "~> 1.0"
required_providers {
cloudflare = {
source = "cloudflare/cloudflare"
version = "2.26.1"
}
}
}
# ---------------------------------------------------------------------------------------------------------------------
# Required inputs
# These parameters must be specified.
# ---------------------------------------------------------------------------------------------------------------------
variable "zones" {
description = "Each zone id and their associated domain name."
type = map(string)
default = {}
}
# ---------------------------------------------------------------------------------------------------------------------
# Resources
# ---------------------------------------------------------------------------------------------------------------------
resource "cloudflare_record" "all" {
for_each = local.zones_to_add
zone_id = each.value.zone_id
name = each.value.subdomain
value = each.value.ns_record
type = "NS"
proxied = false
}
# ---------------------------------------------------------------------------------------------------------------------
# Computations
# ---------------------------------------------------------------------------------------------------------------------
locals {
zones_to_add = merge(flatten([for zone_id, domain in var.zones :
[for subdomain, ns_records in yamldecode(file("zones/${domain}.yaml")) :
{ for ns_record in distinct(ns_records) :
"${domain}_${subdomain}_${ns_record}" => {
zone_id = zone_id
subdomain = subdomain
ns_record = ns_record
}
}
]
])...)
}
# ---------------------------------------------------------------------------------------------------------------------
# Outputs
# ---------------------------------------------------------------------------------------------------------------------
output "all" {
description = "All records for all domains."
value = { for id, domain in var.zones :
domain => { for subdomain, ns_records in yamldecode(file("zones/${domain}.yaml")) :
"${subdomain}.${domain}" => { for ns_record in distinct(ns_records) :
ns_record => {
created = cloudflare_record.all["${domain}_${subdomain}_${ns_record}"].created_on
modified = cloudflare_record.all["${domain}_${subdomain}_${ns_record}"].modified_on
}
}
}
}
}