Skip to content

Commit

Permalink
Migrate Cloudflare lb and lb pool creation to modules
Browse files Browse the repository at this point in the history
  • Loading branch information
klejejs committed Oct 14, 2024
1 parent 26730a4 commit a5d3117
Show file tree
Hide file tree
Showing 15 changed files with 167 additions and 48 deletions.
24 changes: 24 additions & 0 deletions .modules/cloudflare/load_balancer/dns.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
resource "cloudflare_load_balancer" "load_balancer" {
zone_id = data.cloudflare_zone.dns_zone.id
name = "${var.subdomain}.${var.domain_name}"
default_pool_ids = var.pool_ids
fallback_pool_id = var.pool_ids[var.default_pool_ids_index]
description = "${var.subdomain} load balancer using proximity steering policy"

proxied = false
steering_policy = "proximity"

location_strategy {
mode = "pop"
prefer_ecs = "proximity"
}
}

resource "cloudflare_load_balancer_monitor" "monitor" {
account_id = var.cloudflare_account_id
type = "tcp"
port = var.monitoring_port
interval = 60
timeout = 5
retries = 2
}
3 changes: 3 additions & 0 deletions .modules/cloudflare/load_balancer/module.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "cloudflare_zone" "dns_zone" {
name = var.domain_name
}
7 changes: 7 additions & 0 deletions .modules/cloudflare/load_balancer/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
output "load_balancer_endpoint" {
value = cloudflare_load_balancer.load_balancer.name
}

output "load_balancer_monitor_id" {
value = cloudflare_load_balancer_monitor.monitor.id
}
30 changes: 30 additions & 0 deletions .modules/cloudflare/load_balancer/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
variable "cloudflare_account_id" {
description = "Cloudflare Account Id"
type = string
}

variable "domain_name" {
description = "Domain name for the load balancer"
type = string
}

variable "subdomain" {
description = "Subdomain for the load balancer"
type = string
}

variable "pool_ids" {
description = "List of Cloudflare Load Balancer Pool Ids"
type = list(string)
}

variable "default_pool_ids_index" {
description = "Index of the default pool in the pool_ids list"
type = number
default = 0
}

variable "monitoring_port" {
description = "Port used for monitoring by the load balancer"
type = number
}
8 changes: 8 additions & 0 deletions .modules/cloudflare/load_balancer/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform {
required_providers {
cloudflare = {
source = "cloudflare/cloudflare"
version = "~> 4.0"
}
}
}
18 changes: 18 additions & 0 deletions .modules/cloudflare/load_balancer_pool/dns.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
resource "cloudflare_load_balancer_pool" "pool" {
account_id = var.cloudflare_account_id
name = "${var.pool_name}-${var.region}"
description = "${var.pool_name} pool for ${var.region}"
latitude = var.pool_latitude
longitude = var.pool_longitude
monitor = var.load_balancer_monitor_id

origins {
name = "${var.pool_name}-${var.region}-pool"
address = var.pool_endpoint
weight = 1
}

origin_steering {
policy = "random"
}
}
3 changes: 3 additions & 0 deletions .modules/cloudflare/load_balancer_pool/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "load_balancer_pool_id" {
value = cloudflare_load_balancer_pool.pool.id
}
34 changes: 34 additions & 0 deletions .modules/cloudflare/load_balancer_pool/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
variable "region" {
description = "AWS region associated with the resources of the Cloudflare Load Balancer Pool"
type = string
}

variable "cloudflare_account_id" {
description = "Cloudflare Account Id"
type = string
}

variable "pool_name" {
description = "Cloudflare Load Balancer Pool Name"
type = string
}

variable "pool_latitude" {
description = "Cloudflare Load Balancer Pool Latitude"
type = number
}

variable "pool_longitude" {
description = "Cloudflare Load Balancer Pool Longitude"
type = number
}

variable "pool_endpoint" {
description = "Cloudflare Load Balancer Pool Endpoint"
type = string
}

variable "load_balancer_monitor_id" {
description = "Cloudflare Load Balancer Monitor Id"
type = string
}
8 changes: 8 additions & 0 deletions .modules/cloudflare/load_balancer_pool/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform {
required_providers {
cloudflare = {
source = "cloudflare/cloudflare"
version = "~> 4.0"
}
}
}
24 changes: 0 additions & 24 deletions stun_server/dns.tf

This file was deleted.

20 changes: 17 additions & 3 deletions stun_server/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,28 @@ data "cloudflare_zone" "dns_zone" {
name = var.domain_name
}

module "cloudflare_load_balancer" {
source = "../.modules/cloudflare/load_balancer"

cloudflare_account_id = var.CLOUDFLARE_ACCOUNT_ID
domain_name = var.domain_name
subdomain = "stun"
pool_ids = [
module.us_east_1.cloudflare_load_balancer_pool_id,
module.eu_central_1.cloudflare_load_balancer_pool_id,
module.ap_southeast_1.cloudflare_load_balancer_pool_id,
]
monitoring_port = 3478
}

module "us_east_1" {
source = "./region"

region = "us-east-1"
cloudflare_account_id = var.CLOUDFLARE_ACCOUNT_ID
cloudflare_load_balancer_pool_latitude = 37.54129
cloudflare_load_balancer_pool_longitude = -77.43477
cloudflare_load_balancer_monitor_id = cloudflare_load_balancer_monitor.stun-server.id
cloudflare_load_balancer_monitor_id = module.cloudflare_load_balancer.load_balancer_monitor_id
image_tag = var.image_tag
}

Expand All @@ -34,7 +48,7 @@ module "eu_central_1" {
cloudflare_account_id = var.CLOUDFLARE_ACCOUNT_ID
cloudflare_load_balancer_pool_latitude = 50.1155
cloudflare_load_balancer_pool_longitude = 8.6842
cloudflare_load_balancer_monitor_id = cloudflare_load_balancer_monitor.stun-server.id
cloudflare_load_balancer_monitor_id = module.cloudflare_load_balancer.load_balancer_monitor_id
image_tag = var.image_tag
}

Expand All @@ -45,6 +59,6 @@ module "ap_southeast_1" {
cloudflare_account_id = var.CLOUDFLARE_ACCOUNT_ID
cloudflare_load_balancer_pool_latitude = 1.2897
cloudflare_load_balancer_pool_longitude = 103.8501
cloudflare_load_balancer_monitor_id = cloudflare_load_balancer_monitor.stun-server.id
cloudflare_load_balancer_monitor_id = module.cloudflare_load_balancer.load_balancer_monitor_id
image_tag = var.image_tag
}
2 changes: 1 addition & 1 deletion stun_server/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
output "endpoint" {
description = "Endpoint of the Stun server"
value = cloudflare_load_balancer.stun-server.name
value = module.cloudflare_load_balancer.load_balancer_endpoint
}
18 changes: 0 additions & 18 deletions stun_server/region/dns.tf

This file was deleted.

12 changes: 12 additions & 0 deletions stun_server/region/module.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ data "tfe_outputs" "infrastructure" {

data "aws_region" "current" {}

module "cloudflare_load_balancer_pool" {
source = "../../.modules/cloudflare/load_balancer_pool"

region = data.aws_region.current.name
cloudflare_account_id = var.cloudflare_account_id
pool_name = "stun"
pool_latitude = var.cloudflare_load_balancer_pool_latitude
pool_longitude = var.cloudflare_load_balancer_pool_longitude
pool_endpoint = aws_lb.main.dns_name
load_balancer_monitor_id = var.cloudflare_load_balancer_monitor_id
}

module "stun_server_tcp" {
source = "../../.modules/service"

Expand Down
4 changes: 2 additions & 2 deletions stun_server/region/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
output "cloudflare_load_balancer_pool_id" {
description = "The ID of the Cloudflare Load Balancer Pool"
value = cloudflare_load_balancer_pool.stun-server.id
description = "Cloudflare Load Balancer Pool Id"
value = module.cloudflare_load_balancer_pool.load_balancer_pool_id
}

0 comments on commit a5d3117

Please sign in to comment.