Reputation: 171
Terraform version: "1.3.7"
Terraform fails with A provider configuration reference must not be given in quotes.
error when trying to provide providers to child module being called inside a for loop.
The dynamic provider definition is done like below:
resource "local_file" "cluster_provider" {
filename = "${path.module}/cluster-provider.tf"
file_permission = "666"
content = <<-EOT
%{for index, cluster in local.config.cluster_list}
# Init cluster config for helm and kubernetes providers for each cluster
data "cloud_container_cluster_config" "cluster_config_c${index + 1}" {
cluster_name_id = module.k8s_cluster[cluster_${index + 1}].cluster_id
resource_group_id = module.k8s_cluster[cluster_${index + 1}].resource_group_id
}
# Helm provider
provider "helm" {
alias = "helm_cluster_${index + 1}"
kubernetes {
host = data.cloud_container_cluster_config.cluster_config_c${index + 1}.host
token = data.cloud_container_cluster_config.cluster_config_c${index + 1}.token
}
}
# Kubernetes provider
provider "kubernetes" {
alias = "kubernetes_cluster_${index + 1}"
host = data.cloud_container_cluster_config.cluster_config_c${index + 1}.host
token = data.cloud_container_cluster_config.cluster_config_c${index + 1}.token
}
%{endfor}
EOT
}
The above defined and configured providers are passed to the child module as shown below:
module "k8s_cluster" {
for_each = {
for index, cluster in local.config.cluster_list :
"cluster_${index + 1}" => cluster
}
providers = {
kubernetes = "kubernetes.kubernetes_cluster_${index + 1}" # Error is thrown here
helm = "helm.helm_cluster_${index + 1}"
}
source = "./cluster-child-module"
cloud_account_key = var.account_key
resource_group_id = local.resource_group_id
region = var.location
cluster_name = var.cluster_name
... (other module defined attributes not shown here)
}
Also, based upon findings I could understand that string interpolation is not allowed when defining provider alias as discussed in the HashiCorp GitHub issue: https://github.com/hashicorp/terraform/issues/14273.
Upvotes: 0
Views: 311