1
0
IaC/terraform/cloudflare/main.tf
2024-12-30 01:18:40 -05:00

86 lines
2.1 KiB
HCL

# terraform file to create dns resource in cloudflare
terraform {
required_version = ">= 1.8.0"
required_providers {
cloudflare = {
source = "cloudflare/cloudflare"
version = "~> 4.44"
}
}
backend "s3" {
endpoints = {
s3 = "https://s3.balsillie.house:9000"
}
region = "home"
bucket = "terraform"
use_path_style = true
key = "cloudflare/terraform.tfstate"
shared_credentials_files = ["~/.aws/credentials"]
profile = "terraform"
skip_credentials_validation = true
skip_region_validation = true
skip_requesting_account_id = true
}
}
provider "cloudflare" {
api_token = var.api_token
}
data "cloudflare_accounts" "default" {}
locals {
dns_records = {
for index, record in distinct(var.dns_records) : # 'distint' removes duplicate values from a list
tostring(index) => record
}
mx_records = {
for index, record in distinct(var.mx_records) : # 'distint' removes duplicate values from a list
tostring(index) => record
}
}
import {
to = cloudflare_zone.balsillie_net
id = var.zone_id
}
import {
to = cloudflare_zone_dnssec.balsillie_net
id = var.zone_id
}
resource "cloudflare_zone" "balsillie_net" {
account_id = data.cloudflare_accounts.default.accounts[0].id
zone = "balsillie.net"
paused = false
plan = "free"
type = "full"
}
resource "cloudflare_zone_dnssec" "balsillie_net" {
zone_id = cloudflare_zone.balsillie_net.id
}
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
}
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
}
# TODO update the SOA record when dns_records resource was changed