refactor(cloudflare-tunnel): use cloudflare dns generic module
This commit is contained in:
@@ -26,16 +26,21 @@ resource "cloudflare_zero_trust_tunnel_cloudflared" "this" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
locals {
|
locals {
|
||||||
|
// Filter services to only include those that should be published via tunnel
|
||||||
|
tunnel_services = [
|
||||||
|
for service in var.service_definitions :
|
||||||
|
service if length(service.subdomains) > 0 && (service.publish_via == "tunnel" || service.publish_via == "both")
|
||||||
|
]
|
||||||
|
|
||||||
// Transform service definitions into ingress rules format, only for services with ingress_enabled
|
// Transform service definitions into ingress rules format, only for services with ingress_enabled
|
||||||
service_ingress_rules = flatten([
|
service_ingress_rules = flatten([
|
||||||
for service in var.service_definitions :
|
for service in local.tunnel_services :
|
||||||
// Only process services with subdomains AND where ingress is enabled (or default to true for backward compatibility)
|
[
|
||||||
(length(service.subdomains) > 0) ? [
|
|
||||||
for subdomain in service.subdomains : {
|
for subdomain in service.subdomains : {
|
||||||
hostname = "${subdomain}.${var.domain}"
|
hostname = "${subdomain}.${var.domain}"
|
||||||
service = service.endpoint
|
service = service.endpoint
|
||||||
}
|
}
|
||||||
] : []
|
]
|
||||||
])
|
])
|
||||||
|
|
||||||
// Combine manual ingress rules and service-generated ones
|
// Combine manual ingress rules and service-generated ones
|
||||||
@@ -67,21 +72,18 @@ resource "cloudflare_zero_trust_tunnel_cloudflared_config" "this" {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create DNS record for each service
|
module "dns_records" {
|
||||||
resource "cloudflare_record" "service" {
|
source = "../../10-services-generic/cloudflare-dns"
|
||||||
for_each = {
|
|
||||||
for rule in local.all_ingress_rules : rule.hostname => rule
|
|
||||||
if rule.hostname != null && rule.hostname != ""
|
|
||||||
}
|
|
||||||
|
|
||||||
zone_id = var.cloudflare_zone_id
|
zone_id = var.cloudflare_zone_id
|
||||||
name = split(".", each.value.hostname)[0] // Extract subdomain
|
hostnames = [
|
||||||
content = "${cloudflare_zero_trust_tunnel_cloudflared.this.id}.cfargotunnel.com"
|
for rule in local.all_ingress_rules :
|
||||||
type = "CNAME"
|
rule.hostname if rule.hostname != null && rule.hostname != ""
|
||||||
|
]
|
||||||
|
target_content = "${cloudflare_zero_trust_tunnel_cloudflared.this.id}.cfargotunnel.com"
|
||||||
|
record_type = "CNAME"
|
||||||
proxied = true
|
proxied = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set up the Docker container
|
|
||||||
locals {
|
locals {
|
||||||
container_name = var.container_name != "" ? var.container_name : "cloudflared-${var.tunnel_name}"
|
container_name = var.container_name != "" ? var.container_name : "cloudflared-${var.tunnel_name}"
|
||||||
image_tag = var.image_tag != "" ? var.image_tag : "latest"
|
image_tag = var.image_tag != "" ? var.image_tag : "latest"
|
||||||
|
|||||||
@@ -21,11 +21,6 @@ output "cname_target" {
|
|||||||
value = "${cloudflare_zero_trust_tunnel_cloudflared.this.id}.cfargotunnel.com"
|
value = "${cloudflare_zero_trust_tunnel_cloudflared.this.id}.cfargotunnel.com"
|
||||||
}
|
}
|
||||||
|
|
||||||
output "dns_records" {
|
|
||||||
description = "Map of created DNS records"
|
|
||||||
value = { for k, v in cloudflare_record.service : k => v.hostname }
|
|
||||||
}
|
|
||||||
|
|
||||||
output "container_name" {
|
output "container_name" {
|
||||||
description = "The name of the Cloudflared tunnel container"
|
description = "The name of the Cloudflared tunnel container"
|
||||||
value = module.cloudflared.container_name
|
value = module.cloudflared.container_name
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ variable "service_definitions" {
|
|||||||
primary_port = number
|
primary_port = number
|
||||||
endpoint = string
|
endpoint = string
|
||||||
subdomains = optional(list(string), [])
|
subdomains = optional(list(string), [])
|
||||||
|
publish_via = optional(string)
|
||||||
}))
|
}))
|
||||||
default = []
|
default = []
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user