diff --git a/deploy/aws-ecs/.gitignore b/deploy/aws-ecs/.gitignore new file mode 100644 index 00000000..a82a3490 --- /dev/null +++ b/deploy/aws-ecs/.gitignore @@ -0,0 +1,2 @@ +.terraform* +terraform.tfstate* \ No newline at end of file diff --git a/deploy/aws-ecs/config.tf b/deploy/aws-ecs/config.tf new file mode 100644 index 00000000..e9ab9b39 --- /dev/null +++ b/deploy/aws-ecs/config.tf @@ -0,0 +1,47 @@ +locals { + livekit_config = { + port = var.http_port + rtc = { + port_range_start = var.udp_port_start + port_range_end = var.udp_port_end + } + development = true + keys = var.api_keys + redis = { + address = var.redis_address + } + } + + port_mapping = concat([{ + containerPort = var.http_port + protocol = "tcp" + }], [ + for p in range(var.udp_port_start, var.udp_port_end): { + containerPort = p + protocol = "udp" + } + ]) + + task_config = [{ + name = "livekit" + image = "livekit/livekit-server:networktest" + cpu = 1024 + memory = 1024 + essential = true + environment = [ + { + name = "LIVEKIT_CONFIG" + value = yamlencode(local.livekit_config) + } + ] + logConfiguration = { + logDriver = "awslogs" + options = { + "awslogs-region" = var.region + "awslogs-group" = "livekit" + "awslogs-stream-prefix" = var.name + } + }, + portMappings = local.port_mapping + }] +} \ No newline at end of file diff --git a/deploy/aws-ecs/ecs_cluster.tf b/deploy/aws-ecs/ecs_cluster.tf new file mode 100644 index 00000000..e48733dc --- /dev/null +++ b/deploy/aws-ecs/ecs_cluster.tf @@ -0,0 +1,26 @@ +data "aws_ami" "ecs_ami" { + most_recent = true + owners = ["amazon"] + + filter { + name = "name" + values = ["amzn-ami-*-amazon-ecs-optimized"] + } +} + +module "app_ecs_cluster" { + source = "trussworks/ecs-cluster/aws" + + name = "livekit" + environment = var.name + + image_id = data.aws_ami.ecs_ami.image_id + instance_type = var.instance_type + + vpc_id = var.vpc_id + subnet_ids = var.subnet_ids + security_group_ids = concat(var.security_groups, [aws_security_group.main.id]) + desired_capacity = var.nodes + max_size = var.max_nodes + min_size = var.min_nodes +} diff --git a/deploy/aws-ecs/ecs_task.tf b/deploy/aws-ecs/ecs_task.tf new file mode 100644 index 00000000..9f50ec97 --- /dev/null +++ b/deploy/aws-ecs/ecs_task.tf @@ -0,0 +1,42 @@ +resource "aws_ecs_task_definition" "livekit" { + family = "service" + container_definitions = jsonencode(local.task_config) + network_mode = "host" + execution_role_arn = aws_iam_role.ecs_role.arn +} + +resource "aws_ecs_service" "livekit" { + name = "livekit-${var.name}" + cluster = module.app_ecs_cluster.ecs_cluster_arn + task_definition = aws_ecs_task_definition.livekit.arn + desired_count = var.nodes + force_new_deployment = true + launch_type = "EC2" + + placement_constraints { + // one instance per node + type = "distinctInstance" + } + + ordered_placement_strategy { + type = "spread" + field = "instanceId" + } + + // load balancer for TCP port + load_balancer { + target_group_arn = aws_lb_target_group.main.arn + container_name = "livekit" + container_port = var.http_port + } + + // lifecycle { + // ignore_changes = [desired_count] + // } +} + +resource "aws_cloudwatch_log_group" "livekit" { + name = "livekit" + + retention_in_days = 7 +} diff --git a/deploy/aws-ecs/example.tfvars b/deploy/aws-ecs/example.tfvars new file mode 100644 index 00000000..b59fe400 --- /dev/null +++ b/deploy/aws-ecs/example.tfvars @@ -0,0 +1,42 @@ +# name of the livekit cluster, resources will be called `livekit-${name}` +name = "demo" + +# type of instance to use +instance_type = "t3.small" + +# limits to the number of nodes to run +max_nodes = 2 + +# minimum number of nodes to run +min_nodes = 1 + +# initially use this number of nodes +nodes = 1 + +# VPC to create the cluster in +vpc_id = "" + +# List of subnet IDs to create the cluster in +subnet_ids = [] + +# region to use, must match AWS_REGION environment variable +region = "us-east-1" + +# additional security groups to attach the cluster to. +# include security group of the redis instance to allow access +security_groups = [ + "" +] + +# Livekit configuration + +# address and port to redis instance +redis_address = "" + +# list of API keys and secrets +api_keys = { + "key" = "secret" +} + +udp_port_start = 9000 +udp_port_end = 9100 diff --git a/deploy/aws-ecs/iam.tf b/deploy/aws-ecs/iam.tf new file mode 100644 index 00000000..c9fc5aaa --- /dev/null +++ b/deploy/aws-ecs/iam.tf @@ -0,0 +1,26 @@ + +// role for ECS execution +resource "aws_iam_role" "ecs_role" { + name = "livekit-${var.name}-execution" + + assume_role_policy = < terraform apply` + +Enter a name for the cluster, as well as the subnet ids that Redis should be created in diff --git a/deploy/aws-ecs/redis/main.tf b/deploy/aws-ecs/redis/main.tf new file mode 100644 index 00000000..7ec52203 --- /dev/null +++ b/deploy/aws-ecs/redis/main.tf @@ -0,0 +1,33 @@ +# +# The following variables need to be set: +# AWS_ACCESS_KEY_ID (or reads from ~/.aws/credentials) +# AWS_SECRET_ACCESS_KEY (or reads from ~/.aws/credentials) +# AWS_REGION +# +provider "aws" { +} + +# type of instance to deploy on +# see: https://aws.amazon.com/elasticache/pricing/ +variable "instance_type" { + type = string + default = "cache.t3.micro" +} + +# name of the cluster +variable "cluster_id" { + type = string +} + +# launch in the following subnet ids +variable "subnet_ids" { + type = list(string) +} + +variable "security_groups" { + type = list(string) +} + +output "address" { + value = "${aws_elasticache_cluster.main.cache_nodes[0].address}:${aws_elasticache_cluster.main.cache_nodes[0].port}" +} \ No newline at end of file diff --git a/deploy/aws-ecs/redis/redis.tf b/deploy/aws-ecs/redis/redis.tf new file mode 100644 index 00000000..e090735b --- /dev/null +++ b/deploy/aws-ecs/redis/redis.tf @@ -0,0 +1,18 @@ +resource "aws_elasticache_cluster" "main" { + cluster_id = var.cluster_id + engine = "redis" + node_type = var.instance_type + num_cache_nodes = 1 + parameter_group_name = "default.redis6.x" + # strangely terraform requires you to change to a specific version when + # modifying the resource. + engine_version = "6.x" + subnet_group_name = aws_elasticache_subnet_group.main.name + security_group_ids = var.security_groups + port = 6379 +} + +resource "aws_elasticache_subnet_group" "main" { + name = var.cluster_id + subnet_ids = var.subnet_ids +} diff --git a/go.mod b/go.mod index 2ffb767d..04ed0908 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.15 require ( github.com/bep/debounce v1.2.0 + github.com/fatih/hclfmt v0.0.0-20181010234132-374cd0fd2a55 // indirect github.com/go-redis/redis/v8 v8.4.8 github.com/golang/protobuf v1.4.3 github.com/google/wire v0.4.0 diff --git a/go.sum b/go.sum index 3ca79cd0..283514c6 100644 --- a/go.sum +++ b/go.sum @@ -99,6 +99,8 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= +github.com/fatih/hclfmt v0.0.0-20181010234132-374cd0fd2a55 h1:FucIiafNzNNvdfp8BzKYDPXMCAhJaBsNjqiPdPJcieA= +github.com/fatih/hclfmt v0.0.0-20181010234132-374cd0fd2a55/go.mod h1:O9mb/7h6jenxXEB9aeff3D8kXpoBKCU2ASH1OflMFjM= github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db/go.mod h1:7dvUGVsVBjqR7JHJk0brhHOZYGmfBYOrK0ZhYMEtBr4= github.com/franela/goreq v0.0.0-20171204163338-bcd34c9993f8/go.mod h1:ZhphrRTfi2rbfLwlschooIH4+wKKDR4Pdxhh+TRoA20= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= @@ -208,6 +210,7 @@ github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09 github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ= diff --git a/pkg/config/config.go b/pkg/config/config.go index b5e69552..02ec0f03 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -51,8 +51,8 @@ func NewConfig(confString string) (*Config, error) { conf := &Config{ Port: 7880, RTC: RTCConfig{ - ICEPortRangeStart: 8000, - ICEPortRangeEnd: 10000, + ICEPortRangeStart: 9000, + ICEPortRangeEnd: 9200, StunServers: []string{ "stun.l.google.com:19302", "stun1.l.google.com:19302", diff --git a/pkg/service/rtcservice.go b/pkg/service/rtcservice.go index 70704c50..cbc6414a 100644 --- a/pkg/service/rtcservice.go +++ b/pkg/service/rtcservice.go @@ -55,6 +55,7 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) { // require a claim if claims == nil || claims.Video == nil { handleError(w, http.StatusUnauthorized, rtc.ErrPermissionDenied.Error()) + return } identity := claims.Identity @@ -170,22 +171,3 @@ func (s *RTCService) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } } - -type errStruct struct { - StatusCode int `json:"statusCode"` - Error string `json:"error"` - Message string `json:"message,omitempty"` -} - -func jsonError(code int, error ...string) errStruct { - es := errStruct{ - StatusCode: code, - } - if len(error) > 0 { - es.Error = error[0] - } - if len(error) > 1 { - es.Message = error[1] - } - return es -} diff --git a/pkg/service/server.go b/pkg/service/server.go index 41d8eb09..8f8533b4 100644 --- a/pkg/service/server.go +++ b/pkg/service/server.go @@ -59,6 +59,7 @@ func NewLivekitServer(conf *config.Config, mux := http.NewServeMux() mux.Handle(s.roomServer.PathPrefix(), s.roomServer) mux.Handle("/rtc", rtcService) + mux.HandleFunc("/", s.healthCheck) s.httpServer = &http.Server{ Addr: fmt.Sprintf(":%d", conf.Port), @@ -141,6 +142,10 @@ func (s *LivekitServer) Stop() { } } +func (s *LivekitServer) healthCheck(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) +} + // worker to perform periodic tasks per node func (s *LivekitServer) backgroundWorker() { roomTicker := time.NewTicker(30 * time.Second)