Provider in submodule

Hi there,

I’m running into an issue for which I have scoured the entirety of the internet to no avail, so I’m hoping I could help some help here.

Some context

I am creating a module to be used by multiple other people. This module will be pushed to a registry and versioned like any other modules.

This module does 2 main things: it creates an EKS cluster, and, adds a Kubernetes namespace (using a kubernetes_namespace resource) to the newly created EKS cluster.

What this module creates is basically a “pre-configured EKS cluster”.

The user code

main.tf

module "my_preconfigured_eks_cluster" {
    count = 1
    source = "../preconfigured-eks"
}

The module code

Here’s the code for this module (which is basically a copy/paste of the official documenation https://registry.terraform.io/modules/terraform-aws-modules/eks/aws/latest#usage-example ):

main.tf:

#### First Part
## Create an EKS Cluster

module "preconfigured_cluster" {
  source          = "terraform-aws-modules/eks/aws"
  cluster_name    = "preconfigured_cluster"
  cluster_version = "1.17"
  subnets         = ["subnet-abcde012", "subnet-bcde012a", "subnet-fghi345a"]
  vpc_id          = "vpc-1234556abcdef"

  worker_groups = [
    {
      instance_type = "m4.large"
      asg_max_size  = 5
    }
  ]
}

#### Second part
## The EKS cluster is created, now, connect to it and create a namespace called "my-namespace"

## Make some data queries to get the information required to connect to a Kubernetes cluster
data "aws_eks_cluster" "cluster" {
  name = module.preconfigured_cluster.cluster_id
}

data "aws_eks_cluster_auth" "cluster" {
  name = module.preconfigured_cluster.cluster_id
}

## Now configure a `kubernetes` provider to connect to the newly created EKS cluster
provider "kubernetes" {
  host                   = data.aws_eks_cluster.cluster.endpoint
  cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority.0.data)
  token                  = data.aws_eks_cluster_auth.cluster.token
  load_config_file       = false
  version                = "~> 1.9"
}

## Finally, create the namespace `my-namespace`
resource "kubernetes_namespace" "my_namespace" {
  metadata {
    name = "my-namespace"
  }
}

The issue

When my users run terraform init on their user code, they get the following error:

Error: Module does not support count

  on main.tf line 2, in module "my_preconfigured_eks_cluster":
   2:     count = 1

Module "my_preconfigured_eks_cluster" cannot be used with count because it
contains a nested provider configuration for "kubernetes", at
../preconfigured-eks/main.tf:32,10-22.

This module can be made compatible with count by changing it to receive all of
its provider configurations from the calling module, by using the "providers"
argument in the calling module block.

This error is very well documented here: https://www.terraform.io/docs/modules/providers.html

My question

The official documentation is pretty clear in the fact that it does not support submodules declaring providers unless the parent module uses a providers configuration block.

I wonder however if my use case could be supported somehow?

I have a “Child Module”, that creates an EKS cluster and that needs to subsequently talk to that cluster. The only way for my “Child Module” to talk to this EKS cluster is by using a kubernetes provider.

The fact that my submodule is using a kubernetes provider does not need to be known by the “Parent module”, it would also not make much sense for the parent to provide that provider configuration anyway.

So, is there any way a “child module” can declare a provider as “local”. That is, a provider that is only known and used by the child module, and whose existence and use does not need to be known by the “Parent Module”?

Thanks much for your help.

1 Like

Ditto. I’m also running into this pattern where a kubernetes submodule should not require the parent be aware of the kubernetes provider.