Skip to content

Commit fee45fc

Browse files
feat: Add domain_name variable and Caddy reverse proxy configuration for TLS support
1 parent ff06601 commit fee45fc

File tree

6 files changed

+49
-1
lines changed

6 files changed

+49
-1
lines changed

infra/terraform/media_relay/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ This module provisions everything required to host the relay on a single EC2 ins
55
## What gets created
66

77
- Security group exposing TCP `8554`, `1935`, `8888`, `8889`, `9998`, `9999` and UDP `8200` to the CIDR list you supply.
8+
- When `domain_name` is set, TCP `80` and `443` are also opened for the TLS proxy.
89
- EC2 instance (default `t3.small`) running in the chosen subnet/VPC.
910
- User data script that installs Docker and runs Mediamtx with the configuration rendered from Terraform inputs.
11+
- Optional Caddy reverse proxy that fetches Let's Encrypt certificates for the supplied domain and fronts the Mediamtx HTTP/WebRTC endpoints.
1012
- Elastic IP associated with the instance for a stable ingress point.
1113
- If you supply `existing_eip_allocation_id`, the module reuses that Elastic IP instead of allocating a new one.
1214

@@ -24,6 +26,7 @@ Key variables:
2426
| `key_name` | EC2 key pair used for SSH access | `orion` |
2527
| `existing_eip_allocation_id` | Allocation ID of an existing Elastic IP to reuse | `null` |
2628
| `mediamtx_version` | Container tag pulled from Docker Hub | `1.15.3` |
29+
| `domain_name` | FQDN used to request a Let's Encrypt certificate (enables the Caddy TLS proxy) | `null` |
2730
| `tags` | Extra tags applied to every resource | `{}` |
2831

2932
## Scaling & updates
@@ -55,6 +58,7 @@ publish_pass = "change-me"
5558
viewer_user = "any"
5659
viewer_pass = ""
5760
allowed_cidrs = ["203.0.113.0/24", "198.51.100.10/32"]
61+
#domain_name = "stream.example.com"
5862
#existing_eip_allocation_id = "eipalloc-0123456789abcdef0"
5963
```
6064

infra/terraform/media_relay/main.tf

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ data "aws_eip" "existing" {
3636

3737
locals {
3838
subnet_id = var.subnet_id != null ? var.subnet_id : data.aws_subnets.selected.ids[0]
39-
tcp_ports = [8554, 1935, 8888, 8889, 9998, 9999]
39+
base_tcp_ports = [8554, 1935, 8888, 8889, 9998, 9999]
40+
extra_http_ports = var.domain_name != null ? [80, 443] : []
41+
tcp_ports = concat(local.base_tcp_ports, local.extra_http_ports)
4042
udp_ports = [8200]
4143
port_rules = concat(
4244
[for p in local.tcp_ports : {
@@ -77,9 +79,15 @@ locals {
7779
viewer_pass = var.viewer_pass
7880
})
7981

82+
caddy_config = var.domain_name != null ? templatefile("${path.module}/templates/Caddyfile.tpl", {
83+
domain_name = var.domain_name
84+
}) : ""
85+
8086
user_data = templatefile("${path.module}/templates/user_data.sh.tpl", {
8187
mediamtx_config = local.mediamtx_config
8288
mediamtx_version = var.mediamtx_version
89+
domain_name = var.domain_name != null ? var.domain_name : ""
90+
caddy_config = local.caddy_config
8391
})
8492
}
8593

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
${domain_name} {
2+
encode gzip
3+
4+
@hls path /hls*
5+
reverse_proxy @hls http://127.0.0.1:8888
6+
7+
reverse_proxy * http://127.0.0.1:8889
8+
}

infra/terraform/media_relay/templates/user_data.sh.tpl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,24 @@ fi
3131
-p 8200:8200/udp \
3232
-v /opt/mediamtx/mediamtx.yml:/mediamtx.yml:ro \
3333
bluenviron/mediamtx:${mediamtx_version}
34+
35+
%{ if domain_name != "" }
36+
mkdir -p /opt/caddy /opt/caddy/data /opt/caddy/config
37+
cat <<'EOF' >/opt/caddy/Caddyfile
38+
${caddy_config}
39+
EOF
40+
41+
/usr/bin/docker pull caddy:2
42+
if /usr/bin/docker ps -a --format '{{.Names}}' | grep -q '^caddy$'; then
43+
/usr/bin/docker rm -f caddy
44+
fi
45+
46+
/usr/bin/docker run -d \
47+
--name caddy \
48+
--restart unless-stopped \
49+
--network host \
50+
-v /opt/caddy/Caddyfile:/etc/caddy/Caddyfile:ro \
51+
-v /opt/caddy/data:/data \
52+
-v /opt/caddy/config:/config \
53+
caddy:2
54+
%{ endif }

infra/terraform/media_relay/terraform.tfvars.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ publish_pass = "password"
55
viewer_user = "any"
66
viewer_pass = ""
77
allowed_cidrs = ["0.0.0.0/0"]
8+
#domain_name = "rtsp.02labs.me"
89
instance_type = "t3.small"
910
#key_name = "orion"
1011
#existing_eip_allocation_id = "eipalloc-0123456789abcdef0"

infra/terraform/media_relay/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,9 @@ variable "log_level" {
8787
type = string
8888
default = "info"
8989
}
90+
91+
variable "domain_name" {
92+
description = "Fully qualified domain name to secure with Let's Encrypt (enables the Caddy reverse proxy)"
93+
type = string
94+
default = null
95+
}

0 commit comments

Comments
 (0)