resource "aws_efs_file_system" "jsm-efs" {
  creation_token                  = "jsm-efs"
  throughput_mode                 = "provisioned"
  provisioned_throughput_in_mibps = 30
  encrypted                       = true

  tags = {
    owner   = "liuy@pingcap.com"
    context = "jsm-customer-service-app"
  }
}

resource "aws_efs_file_system" "confluence-efs" {
  creation_token                  = "confluence-efs"
  throughput_mode                 = "provisioned"
  provisioned_throughput_in_mibps = 15
  encrypted                       = true

  tags = {
    owner   = "liuy@pingcap.com"
    context = "jsm-customer-service-app"
  }
}

resource "aws_efs_mount_target" "jsm-efs" {
  count           = length(module.vpc.private_subnets)
  file_system_id  = aws_efs_file_system.jsm-efs.id
  subnet_id       = module.vpc.private_subnets[count.index]
  security_groups = ["${aws_security_group.allow_efs_mounts.id}"]
}

resource "aws_efs_mount_target" "confluence-efs" {
  count           = length(module.vpc.private_subnets)
  file_system_id  = aws_efs_file_system.confluence-efs.id
  subnet_id       = module.vpc.private_subnets[count.index]
  security_groups = ["${aws_security_group.allow_efs_mounts.id}"]
}

resource "aws_security_group" "allow_efs_mounts" {
  name        = "allow_efs_mount"
  description = "Allow EFS inbound traffic"
  vpc_id      = module.vpc.vpc_id

  ingress {
    from_port = 0
    to_port   = 0
    protocol  = "-1"
    self      = true
  }

  ingress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["${var.vpc_cidr}"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}
