#
# AWS
# Cloud Media Server - runs media server processes. No HTTPs ingress.
#

provider "aws" {
  region = "us-west-2"
}

variable "network_name" {
}

variable "fleet_name" {
}

variable "server_name" {
  type    = string
  default = "cloud-media"
}

variable "ami" {
  type = string
}

variable "server_count" {
  type    = string
  default = "1"
}

variable "instance_type" {
  type    = string
  default = "t2.micro"
}

variable "key_name" {
  type = string
}

locals {
  server_name        = var.server_name
  network_fleet_name = "${var.network_name}_${var.fleet_name}"
}

resource "aws_instance" "cloud_media" {
  count         = var.server_count
  ami           = var.ami
  instance_type = var.instance_type
  key_name      = var.key_name
  security_groups = [
    "${aws_security_group.cloud_media_security_group.name}",

    # Developer: SSH access security group
    "[Bigscreen] SSH Server Access"
  ]

  tags = {
    Name = "[${local.network_fleet_name}] ${local.server_name}"
  }

  root_block_device {
    volume_type = "gp3"
    iops        = "1000"
    throughput  = "200"
    volume_size = "100"
  }
}

# Security group for the cloudapp server itself.
resource "aws_security_group" "cloud_media_security_group" {
  name        = "${local.server_name}-security-group"
  description = "${local.server_name} Internal"
  vpc_id      = "vpc-6284a71b"

  # Jenkins server ssh support
  ingress {
    from_port       = 22
    to_port         = 22
    protocol        = "tcp"
    security_groups = ["sg-0577fa74bb0447c98"]
  }

  # UDP packets from any source.
  ingress {
    from_port   = 10000
    to_port     = 65535
    protocol    = "udp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  # SCTP
  #ingress {
  #    from_port   = 0
  #    to_port     = 0
  #    protocol    = "132"
  #    cidr_blocks = ["0.0.0.0/0"]
  #}

  # ICMP
  ingress {
    from_port   = 0
    to_port     = 0
    protocol    = "icmp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = {
    Name = "[${local.network_fleet_name}] ${local.server_name}-security-group"
  }
}
