1

I'm using EFS as a CSI driver in a k8s cluster.
I would like to use Terraform to create a PV that will use the efs storage class. I verified that I can create the PV "manually".

I would like to automate the PV creation with Terraform, using the kubernetes_persistent_volume resource. The resource offers many persistent volume sources options, among them AWS EBS which works well. However I couldn't find a suitable one for EFS, does anyone has an idea?

balaganAtomi
  • 550
  • 8
  • 13

2 Answers2

1

You can do this using newer version of the kubernetes provider, combined with installing the csi driver (as in Shashank's answer)

example

resource "kubernetes_persistent_volume" "pv_example" {
  provider = "kubernetes.localkubernetes"
  metadata {
    name = local.efs_name
  }
  spec {
    access_modes = ["ReadWriteMany"]
    capacity = {
      storage = "50Gi"
    }
    persistent_volume_source {
        csi {
          driver = "efs.csi.aws.com"
          volume_handle = EXISTING_EFS_VOLUME
        }
    }
  }
}

here I have defined

provider "kubernetes" {
  alias = "localkubernetes"
  ...
  version                = "= 1.13.3"
}

because terraform also have a kubernetes provider (with older version) from a module I am using, and so I needed to explicitly give a provider with a new enough version. (Sorry couldn't track down exact version it was added)

GeorgeWilson
  • 562
  • 6
  • 17
0

You can enable EFS storage class like this using terraform. Also refer to EFS provider.

variable cluster_interpreter {
  type    = list(string)
  default = ["/bin/sh", "-c"]
}

variable class {
  type    = string
  default = "kubectl apply -k 'github.com/kubernetes-sigs/aws-efs-csi-driver/deploy/kubernetes/overlays/stable/?ref=master'"
}

resource "null_resource" "storage" {

  provisioner "local-exec" {
    command     = var.class
    interpreter = var.cluster_interpreter
  }
}
Shashank Sinha
  • 492
  • 2
  • 7