1
0
IaC/terraform/cloudflare/main.tf

50 lines
1.2 KiB
Terraform
Raw Normal View History

2024-10-23 00:22:32 -04: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-10-24 01:39:30 -04:00
path = pathexpand("~/Backups/tfstate/cloudflare.tfstate")
2024-10-23 00:22:32 -04:00
}
}
provider "cloudflare" {
api_token = var.api_token
}
2024-10-24 01:39:30 -04:00
data "cloudflare_accounts" "default" {}
2024-10-25 23:48:20 -04:00
locals {
dns_records = {
for index, record in distinct(var.dns_records) : # 'distint' removes duplicate values from a list
tostring(index) => record
}
}
2024-10-23 00:22:32 -04:00
resource "cloudflare_zone" "balsillie_net" {
2024-10-24 01:39:30 -04:00
account_id = data.cloudflare_accounts.default.accounts[0].id
2024-10-23 00:22:32 -04:00
zone = "balsillie.net"
paused = false
plan = "free"
type = "full"
}
2024-10-24 01:39:30 -04:00
resource "cloudflare_zone_dnssec" "balsillie_net" {
2024-10-23 00:22:32 -04:00
zone_id = cloudflare_zone.balsillie_net.id
}
2024-10-25 23:48:20 -04: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 01:39:30 -04:00
}
2024-10-25 23:48:20 -04:00
# TODO update the SOA record when dns_records resource was changed