1
0
IaC/terraform/cloudflare/main.tf

86 lines
2.1 KiB
Terraform
Raw Normal View History

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"
}
}
2024-12-30 06:18:40 +00:00
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
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