terraform/modules/dns/main.tf

162 lines
3.6 KiB
Terraform
Raw Normal View History

2022-09-14 15:52:46 +02:00
# todo:
# I am also creating the subdomain `wg.monotremata.xyz` manually
# I decided to manage that subdomain outside of terraform because it has a
# dynamic IP that I update with a cron job
2023-05-23 12:50:26 +02:00
terraform {
required_providers {
namecheap = {
source = "namecheap/namecheap"
version = ">= 2.0.0"
}
linode = {
source = "linode/linode"
version = ">= 1.29.0"
}
2022-09-14 15:52:46 +02:00
}
2023-05-23 12:50:26 +02:00
}
2022-09-14 15:52:46 +02:00
2023-05-23 12:50:26 +02:00
provider "namecheap" {
user_name = "gthar"
api_user = "gthar"
client_ip = "139.162.137.29" // caladan's public IP
use_sandbox = false
2022-08-23 18:14:36 +02:00
}
2022-09-14 15:52:46 +02:00
resource "namecheap_domain_records" "namecheap-monotremata-xyz" {
domain = "monotremata.xyz"
mode = "OVERWRITE"
nameservers = [
"ns1.linode.com",
"ns2.linode.com",
"ns3.linode.com",
"ns4.linode.com",
"ns5.linode.com"
2022-08-23 17:40:16 +02:00
]
}
2022-09-14 15:52:46 +02:00
resource "linode_domain" "monotremata_xyz" {
type = "master"
2023-05-23 12:50:26 +02:00
domain = var.domain
soa_email = format("admin@%s", var.domain)
2022-08-23 17:24:21 +02:00
}
2022-09-14 15:52:46 +02:00
resource "linode_domain_record" "caladan_a" {
domain_id = linode_domain.monotremata_xyz.id
name = each.key
record_type = "A"
2023-05-23 12:50:26 +02:00
target = var.caladan.ipv4
for_each = var.caladan.domains
2022-08-23 17:50:21 +02:00
}
2022-09-14 15:52:46 +02:00
resource "linode_domain_record" "caladan_aaaa" {
domain_id = linode_domain.monotremata_xyz.id
name = each.key
record_type = "AAAA"
2023-05-23 12:50:26 +02:00
target = var.caladan.ipv6
for_each = var.caladan.domains
2022-09-14 15:52:46 +02:00
}
2022-08-23 17:16:47 +02:00
2022-09-14 15:52:46 +02:00
resource "linode_domain_record" "mx" {
domain_id = linode_domain.monotremata_xyz.id
name = each.value.name
target = each.value.target
record_type = each.key
priority = each.value.priority
for_each = {
A = {
name = "mail"
2023-05-23 12:50:26 +02:00
target = var.fugu.ipv4
2022-09-14 15:52:46 +02:00
priority = null
}
AAAA = {
name = "mail"
2023-05-23 12:50:26 +02:00
target = var.fugu.ipv6
2022-09-14 15:52:46 +02:00
priority = null
}
MX = {
2023-05-23 12:50:26 +02:00
name = var.domain,
target = format("mail.%s", var.domain)
2022-09-14 15:52:46 +02:00
priority = 0
2022-08-23 17:16:47 +02:00
}
}
2022-09-14 15:52:46 +02:00
}
2022-08-23 17:16:47 +02:00
2022-09-14 15:52:46 +02:00
resource "linode_domain_record" "mx2" {
domain_id = linode_domain.monotremata_xyz.id
name = each.value.name
target = each.value.target
record_type = each.key
priority = each.value.priority
for_each = {
A = {
name = "mx2"
2023-05-23 12:50:26 +02:00
target = var.caladan.ipv4
2022-09-14 15:52:46 +02:00
priority = null
}
AAAA = {
name = "mx2"
2023-05-23 12:50:26 +02:00
target = var.caladan.ipv6
2022-09-14 15:52:46 +02:00
priority = null
}
MX = {
2023-05-23 12:50:26 +02:00
name = var.domain
target = format("mx2.%s", var.domain)
2022-09-14 15:52:46 +02:00
priority = 5
}
2022-08-23 18:14:36 +02:00
}
2022-09-14 15:52:46 +02:00
}
2022-08-23 18:14:36 +02:00
2022-09-14 15:52:46 +02:00
resource "linode_domain_record" "mail_txt" {
domain_id = linode_domain.monotremata_xyz.id
record_type = "TXT"
name = each.value.name
target = each.value.target
for_each = {
spf = {
2023-05-23 12:50:26 +02:00
name = var.domain
2022-09-14 15:52:46 +02:00
target = "v=spf1 mx -all"
}
dmarc = {
name = "_dmarc"
2023-05-23 12:50:26 +02:00
target = format("v=DMARC1;p=quarantine;pct=100;rua=mailto:postmaster@%s;;", var.domain)
2022-09-14 15:52:46 +02:00
}
dkim = {
name = "20201210._domainkey"
2023-05-23 12:50:26 +02:00
target = format("v=DKIM1;k=rsa;p=%s;", var.dkim_pub_key)
2022-09-14 15:52:46 +02:00
}
2022-08-23 18:14:36 +02:00
}
2022-09-14 15:52:46 +02:00
}
2022-08-23 18:14:36 +02:00
2022-09-14 15:52:46 +02:00
resource "linode_domain_record" "matrix_srv" {
domain_id = linode_domain.monotremata_xyz.id
record_type = "SRV"
service = "matrix"
protocol = "tcp"
priority = 0
weight = 10
port = 443
2023-05-23 12:50:26 +02:00
target = format("matrix.%s", var.domain)
2022-09-14 15:52:46 +02:00
ttl_sec = 1800 // 30 min
}
2022-08-23 18:14:36 +02:00
2022-09-14 15:52:46 +02:00
resource "linode_domain_record" "xmpp_srv" {
domain_id = linode_domain.monotremata_xyz.id
record_type = "SRV"
service = each.key
protocol = "tcp"
port = each.value.port
priority = 5
weight = 0
2023-05-23 12:50:26 +02:00
target = format("xmpp.%s", var.domain)
2022-09-14 15:52:46 +02:00
ttl_sec = 1800 // 30 min
for_each = {
xmpp-client = {
port = 5222
}
xmpp-server = {
port = 5269
}
2022-08-23 18:14:36 +02:00
}
2022-08-18 18:16:44 +02:00
}