module "default_tags" {
  source             = "git@github.com:theorchard/terraform-default-tags.git//?ref=2.0.0"
  environment        = var.environment
  application_family = var.application_family
  service_name       = var.service_name
  team_name          = var.team_name
}

provider "aws" {
  region = var.aws_region

  default_tags {
    tags = module.default_tags.tags
  }
}

# Terraform backends cannot contain interpolations
terraform {
  backend "s3" {
    bucket  = "qa-permissions-platform-terraform-state"
    key     = "qa/elasticache/terraform.tfstate"
    region  = "us-east-1"
    encrypt = "true"
  }
}

data "aws_route53_zone" "route53_zone" {
  name = var.domain_name
}

module "vpc_info" {
  source = "git@github.com:theorchard/terraform-vpc-info.git?ref=1.0.0"

  environment = var.environment
}

module "elasticache" {
  source = "git@github.com:theorchard/terraform-elasticache.git//?ref=2.3.2"

  environment                = var.environment
  service_name               = var.service_name
  application_family         = var.application_family
  cache_engine               = "redis"
  redis_engine_version       = "7.0"
  cache_parameter_group_name = "default.redis7"
  cache_subnet_group_name    = "${var.environment}-elasticache-subnet-group"
  cache_node_type            = "cache.t4g.micro"
  cache_node_count           = var.elasticache_node_count
  vpc_id                     = module.vpc_info.vpc_id
  route53_zone_id            = data.aws_route53_zone.route53_zone.zone_id
}


data "aws_ec2_managed_prefix_list" "orchard_prod_private" {
  name = "prod-orcd-private-subnet-prefix-list"
}
# This rule will allow Jenkins in the orcd account to communicate with elasticache
resource "aws_security_group_rule" "allow_orchard_prod_private" {
  type              = "ingress"
  from_port         = 6379
  to_port           = 6379
  protocol          = "TCP"
  security_group_id = module.elasticache.cache_security_group_id
  prefix_list_ids = [
    data.aws_ec2_managed_prefix_list.orchard_prod_private.id
  ]
}
