#
# AWS
# ElastiCache redis server, accessible by the cloudapp and cloudhttp servers.
#

provider "aws" {
  region = "us-west-2"
}

variable "network_name" {
  type    = string
  default = "dev"
}

variable "fleet_name" {
  type    = string
  default = "dev"
}

variable "name" {
  type    = string
  default = "redis"
}

variable "node_type" {
  type    = string
  default = "cache.t2.micro"
}

variable "api_security_group_id" {
  type    = string
  default = ""
}

variable "admin_api_security_group_id" {
  type    = string
  default = ""
}

variable "cloud_api_security_group_id" {
  type    = string
  default = ""
}

variable "cloud_websocket_security_group_id" {
  type    = string
  default = ""
}

variable "replicas_per_node_group" {
  type    = string
  default = "1"
}

variable "num_node_groups" {
  type    = string
  default = "1"
}

#variable "key_name" {
#    type = string
#}


locals {
  redis_name       = "${var.network_name}-${var.fleet_name}-redis"
  redis_cluster_id = "${var.network_name}-${var.fleet_name}-redis"
}

# Elasticache server cluster for redis
resource "aws_elasticache_replication_group" "redis" {
  replication_group_id       = local.redis_cluster_id
  description                = local.redis_cluster_id
  engine                     = "redis"
  engine_version             = "6.x"
  automatic_failover_enabled = true
  node_type                  = var.node_type
  parameter_group_name       = "arch5-redis6x-cluster-nov2020"

  # Cluster settings
  replicas_per_node_group = var.replicas_per_node_group
  num_node_groups         = var.num_node_groups

  tags = {
    Name = "${var.network_name}_${var.fleet_name} ${local.redis_name}"
  }

  security_group_ids = ["${aws_security_group.redis_security_group.id}"]

  at_rest_encryption_enabled = true
  transit_encryption_enabled = true
}

# Security group for accessing elasticache.
resource "aws_security_group" "redis_security_group" {
  name        = "${local.redis_name}_security_group"
  description = "${local.redis_name} Internal"
  vpc_id      = "vpc-6284a71b"

  # Jenkins server access
  ingress {
    from_port       = 6379
    to_port         = 6379
    protocol        = "tcp"
    security_groups = ["sg-0577fa74bb0447c98"]
  }

  # Cloud servers that can access this cluster are passed in from modules.
  ingress {
    from_port       = 6379
    to_port         = 6379
    protocol        = "tcp"
    security_groups = ["${var.api_security_group_id}", "${var.admin_api_security_group_id}", "${var.cloud_api_security_group_id}", "${var.cloud_websocket_security_group_id}"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }

  tags = {
    Name = "[${var.network_name}_${var.fleet_name}] ${local.redis_name}_security_group"
  }
}

#
# Outputs - configuration endpoint for the cluster.
#
output "configuration_endpoint_address" {
  value = aws_elasticache_replication_group.redis.configuration_endpoint_address
}
