resource "aws_efs_file_system" "wordpress-efs" {
  creation_token = "wordpress-jp-efs"
  throughput_mode = "elastic"
  provisioned_throughput_in_mibps = 20

  tags = {
    Name = "wordpress-jp-efs"
  }
}

resource "aws_efs_mount_target" "wordpress-efs" {
  count = length(module.app_vpc.private_subnets)
  file_system_id = "${aws_efs_file_system.wordpress-efs.id}"
  subnet_id      = "${module.app_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.app_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.app_cidr}"]
  }

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

