Skip to content

Commit

Permalink
Migrate Cloudflare load balancer config to Terraform
Browse files Browse the repository at this point in the history
  • Loading branch information
klejejs committed Oct 13, 2024
1 parent 4a6a45d commit 77b8192
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 16 deletions.
24 changes: 24 additions & 0 deletions stun_server/dns.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
resource "cloudflare_load_balancer" "stun-server" {
zone_id = data.cloudflare_zone.dns_zone.id
name = "stun.${var.domain_name}"
default_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]
fallback_pool_id = module.us_east_1.cloudflare_load_balancer_pool_id
description = "Stun server load balancer using proximity steering policy"

proxied = false
steering_policy = "proximity"

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

resource "cloudflare_load_balancer_monitor" "stun-server" {
account_id = var.CLOUDFLARE_ACCOUNT_ID
type = "tcp"
port = 3478
interval = 60
timeout = 5
retries = 2
}
28 changes: 22 additions & 6 deletions stun_server/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,39 @@ provider "aws" {
region = "us-east-1"
}

data "cloudflare_zone" "dns_zone" {
name = var.domain_name
}

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

region = "us-east-1"
image_tag = var.image_tag
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
image_tag = var.image_tag
}

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

region = "eu-central-1"
image_tag = var.image_tag
region = "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
image_tag = var.image_tag
}

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

region = "ap-southeast-1"
image_tag = var.image_tag
region = "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
image_tag = var.image_tag
}
10 changes: 3 additions & 7 deletions stun_server/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
output "endpoints" {
description = "Endpoints of the Stun server"
value = {
"us-east-1" = module.us_east_1.stun_server_endpoint
"eu-central-1" = module.eu_central_1.stun_server_endpoint
"ap-southeast-1" = module.ap_southeast_1.stun_server_endpoint
}
output "endpoint" {
description = "Endpoint of the Stun server"
value = cloudflare_load_balancer.stun-server.name
}
18 changes: 18 additions & 0 deletions stun_server/region/dns.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
resource "cloudflare_load_balancer_pool" "stun-server" {
account_id = var.cloudflare_account_id
name = "stun-${data.aws_region.current.name}"
description = "Stun server pool for ${data.aws_region.current.name}"
latitude = var.cloudflare_load_balancer_pool_latitude
longitude = var.cloudflare_load_balancer_pool_longitude
monitor = var.cloudflare_load_balancer_monitor_id

origins {
name = "stun-${data.aws_region.current.name}-pool"
address = aws_lb.main.dns_name
weight = 1
}

origin_steering {
policy = "random"
}
}
6 changes: 3 additions & 3 deletions stun_server/region/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
output "stun_server_endpoint" {
description = "Endpoint of the Stun server"
value = aws_lb.main.dns_name
output "cloudflare_load_balancer_pool_id" {
description = "The ID of the Cloudflare Load Balancer Pool"
value = cloudflare_load_balancer_pool.stun-server.id
}
21 changes: 21 additions & 0 deletions stun_server/region/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@ variable "region" {
type = string
}

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

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

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

}

variable "cloudflare_load_balancer_monitor_id" {
description = "Cloudflare Load Balancer Monitor Id"
type = string
}

variable "image_tag" {
description = "Version of the Stun server to deploy"
type = string
Expand Down
10 changes: 10 additions & 0 deletions stun_server/variables.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
variable "CLOUDFLARE_ACCOUNT_ID" {
description = "Cloudflare Account Id"
type = string
}

variable "domain_name" {
description = "The base domain name"
type = string
}

variable "image_tag" {
description = "Version of the Stun server to deploy"
type = string
Expand Down
5 changes: 5 additions & 0 deletions stun_server/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@ terraform {
source = "hashicorp/aws"
version = "~> 5.0"
}

cloudflare = {
source = "cloudflare/cloudflare"
version = "~> 4.0"
}
}
}

0 comments on commit 77b8192

Please sign in to comment.