2024-10-23 04:22:32 +00:00
|
|
|
# terraform file to create dns resource in cloudflare
|
|
|
|
terraform {
|
|
|
|
required_version = ">= 1.8.0"
|
|
|
|
required_providers {
|
|
|
|
cloudflare = {
|
|
|
|
source = "cloudflare/cloudflare"
|
|
|
|
version = "~> 4.44"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
backend "local" {
|
2024-11-29 06:35:04 +00:00
|
|
|
# path = pathexpand("~/Backups/tfstate/cloudflare.tfstate")
|
2024-10-23 04:22:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
provider "cloudflare" {
|
|
|
|
api_token = var.api_token
|
|
|
|
}
|
|
|
|
|
2024-10-24 05:39:30 +00:00
|
|
|
data "cloudflare_accounts" "default" {}
|
|
|
|
|
2024-10-26 03:48:20 +00:00
|
|
|
locals {
|
|
|
|
dns_records = {
|
|
|
|
for index, record in distinct(var.dns_records) : # 'distint' removes duplicate values from a list
|
|
|
|
tostring(index) => record
|
|
|
|
}
|
2024-11-29 06:35:04 +00:00
|
|
|
mx_records = {
|
|
|
|
for index, record in distinct(var.mx_records) : # 'distint' removes duplicate values from a list
|
|
|
|
tostring(index) => record
|
|
|
|
}
|
2024-10-26 03:48:20 +00:00
|
|
|
}
|
2024-10-23 04:22:32 +00:00
|
|
|
|
2024-12-18 20:42:45 +00:00
|
|
|
import {
|
|
|
|
to = cloudflare_zone.balsillie_net
|
|
|
|
id = var.zone_id
|
|
|
|
}
|
|
|
|
|
|
|
|
import {
|
|
|
|
to = cloudflare_zone_dnssec.balsillie_net
|
|
|
|
id = var.zone_id
|
|
|
|
}
|
|
|
|
|
2024-10-23 04:22:32 +00:00
|
|
|
resource "cloudflare_zone" "balsillie_net" {
|
2024-10-24 05:39:30 +00:00
|
|
|
account_id = data.cloudflare_accounts.default.accounts[0].id
|
2024-10-23 04:22:32 +00:00
|
|
|
zone = "balsillie.net"
|
|
|
|
paused = false
|
|
|
|
plan = "free"
|
|
|
|
type = "full"
|
|
|
|
}
|
|
|
|
|
2024-10-24 05:39:30 +00:00
|
|
|
resource "cloudflare_zone_dnssec" "balsillie_net" {
|
2024-10-23 04:22:32 +00:00
|
|
|
zone_id = cloudflare_zone.balsillie_net.id
|
|
|
|
}
|
|
|
|
|
2024-10-26 03:48:20 +00:00
|
|
|
resource "cloudflare_record" "dns_records" {
|
|
|
|
for_each = local.dns_records
|
|
|
|
zone_id = cloudflare_zone.balsillie_net.id
|
|
|
|
proxied = false
|
|
|
|
name = each.value.name
|
|
|
|
type = each.value.type
|
|
|
|
content = each.value.content
|
|
|
|
ttl = each.value.ttl
|
2024-10-24 05:39:30 +00:00
|
|
|
}
|
|
|
|
|
2024-11-29 06:35:04 +00:00
|
|
|
resource "cloudflare_record" "mx_records" {
|
|
|
|
for_each = local.mx_records
|
|
|
|
zone_id = cloudflare_zone.balsillie_net.id
|
|
|
|
proxied = false
|
|
|
|
name = each.value.name
|
|
|
|
type = each.value.type
|
|
|
|
content = each.value.content
|
|
|
|
priority = each.value.priority
|
|
|
|
ttl = each.value.ttl
|
|
|
|
}
|
|
|
|
|
2024-10-26 03:48:20 +00:00
|
|
|
# TODO update the SOA record when dns_records resource was changed
|