Skip to content

Commit

Permalink
Add deployment configuration for Stun server
Browse files Browse the repository at this point in the history
  • Loading branch information
klejejs committed Sep 12, 2024
1 parent 9c4fc6b commit 3059f1a
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 0 deletions.
11 changes: 11 additions & 0 deletions stun_server/dns.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
data "cloudflare_zone" "dns_zone" {
name = var.domain_name
}

resource "cloudflare_record" "instance_dns" {
zone_id = data.cloudflare_zone.dns_zone.id
name = "" # TODO: Add the subdomain
content = data.aws_network_interface.stun_server_interface.association[0].public_ip
type = "A"
proxied = true
}
87 changes: 87 additions & 0 deletions stun_server/ecs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
resource "aws_ecs_service" "stun-server" {
name = "stun-server"

cluster = data.tfe_outputs.infrastructure.values.ecs_cluster
task_definition = aws_ecs_task_definition.stun-server.arn
count = 1
desired_count = 1

deployment_minimum_healthy_percent = 100
deployment_maximum_percent = 200
health_check_grace_period_seconds = 90
launch_type = "FARGATE"

# Required to fetch the public IP address of the ECS service
enable_ecs_managed_tags = true
wait_for_steady_state = true

network_configuration {
assign_public_ip = true
security_groups = [aws_security_group.stun_sg.id]
subnets = [
data.tfe_outputs.infrastructure.values.public_subnets[0],
data.tfe_outputs.infrastructure.values.public_subnets[1]
]
}
}

data "aws_network_interface" "stun_server_interface" {
filter {
name = "tag:aws:ecs:serviceName"
values = [aws_ecs_service.stun-server.name]
}
}

resource "aws_cloudwatch_log_group" "aws_logs" {
name = "/ecs/stun-server"
retention_in_days = 14
}

resource "aws_ecs_task_definition" "stun-server" {
family = "stun-server"

count = 1
cpu = 2048
memory = 4096

execution_role_arn = aws_iam_role.ecs_task_execution_role.arn
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]

runtime_platform {
operating_system_family = "LINUX"
cpu_architecture = "ARM64"
}

container_definitions = jsonencode([
{
name = "stun-server"
image = "ghcr.io/home-assistant/stun:${var.image_tag}"
cpu = 2048
memory = 4096
essential = true

portMappings = [
{
containerPort = 3478
hostPort = 3478
protocol = "tcp"
},
{
containerPort = 3478
hostPort = 3478
protocol = "udp"
}
],

logConfiguration = {
logDriver = "awslogs"
options = {
"awslogs-group" = "/ecs/stun-server"
"awslogs-region" = "us-east-1"
"awslogs-stream-prefix" = "ecs"
}
}
}
])
}
18 changes: 18 additions & 0 deletions stun_server/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
terraform {
cloud {
organization = "home_assistant"

workspaces {
name = "infrastructure"
}
}
}

provider "aws" {
region = "us-east-1"
}

data "tfe_outputs" "infrastructure" {
organization = "home_assistant"
workspace = "infrastructure"
}
26 changes: 26 additions & 0 deletions stun_server/network.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
resource "aws_security_group" "stun_sg" {
vpc_id = data.tfe_outputs.infrastructure.values["us-east-1"].network_id

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
description = "Allow STUN traffic TCP"
from_port = 3478
to_port = 3478
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
description = "Allow STUN traffic UDF"
from_port = 3478
to_port = 3478
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
}
}
4 changes: 4 additions & 0 deletions stun_server/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "stun_server_ip" {
description = "The public IP address of the stun server"
value = data.aws_network_interface.stun_server_interface.association[0].public_ip
}
9 changes: 9 additions & 0 deletions stun_server/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
variable "domain_name" {
description = "The base domain name"
type = string
}

variable "image_tag" {
description = "Version of the Stun server to deploy"
type = string
}
19 changes: 19 additions & 0 deletions stun_server/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
terraform {

required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}

tfe = {
source = "hashicorp/tfe"
version = "~> 0.58.0"
}

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

0 comments on commit 3059f1a

Please sign in to comment.