Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The "count" value depends on resource attributes that cannot be determined #58

Closed
jbarop opened this issue Oct 27, 2020 · 6 comments
Closed

Comments

@jbarop
Copy link

jbarop commented Oct 27, 2020

Hi,

kubectl_path_documents cannot be used with variables from other resources.

For example

data "kubectl_path_documents" "alb-ingress-controller" {
  pattern = "./alb-ingress-controller/*.yaml"
  vars = {
    vpc_id = module.vpc_id
  }
}

resource "kubectl_manifest" "alb-ingress-controller" {
  count     = length(data.kubectl_path_documents.alb-ingress-controller.documents)
  yaml_body = element(data.kubectl_path_documents.alb-ingress-controller.documents, count.index)
}

Leads to the following error:

 XX:   count     = length(data.kubectl_path_documents.manifests.documents)

The "count" value depends on resource attributes that cannot be determined
until apply, so Terraform cannot predict how many instances will be created.
To work around this, use the -target argument to first apply only the
resources that the count depends on.

It would be nice if the variables could be passed to kubectl_manifest. Because in reality the count value does not depend on the vars. But terraform does not know that.

@jbarop jbarop closed this as completed Oct 27, 2020
@jbarop jbarop reopened this Oct 28, 2020
@gavinbunney
Copy link
Owner

gavinbunney commented Oct 28, 2020

This is an issue with Terraform and depending on vars from other modules; it's unable to compute the graph correctly, as it doesn't known how many documents the kubectl_path_documents data resource will produce.

Two alternatives you can use are:

1/ Hardcode the count in your kubectl_manifest call - not great but works
2/ Add another kubectl_path_documents resource which just sets your var to an empty string, then use that only for the count element. This way you still get a dynamic document count, but not depend on variables from other modules. Something like this:

data "kubectl_path_documents" "alb-ingress-controller" {
  pattern = "./alb-ingress-controller/*.yaml"
  vars = {
    vpc_id = module.vpc_id
  }
}

data "kubectl_path_documents" "alb-ingress-controller-count-hack" {
  pattern = "./alb-ingress-controller/*.yaml"
  vars = {
    vpc_id = ""
  }
}

resource "kubectl_manifest" "alb-ingress-controller" {
  count     = length(data.kubectl_path_documents.alb-ingress-controller-count-hack.documents)
  yaml_body = element(data.kubectl_path_documents.alb-ingress-controller.documents, count.index)
}

@yongzhang
Copy link

yongzhang commented Mar 8, 2021

@gavinbunney As your second hack, is this the workaround for kubectl_file_documents?

data "template_file" "k8s_metrics_server" {
  template = file("templates/metrics-server.yaml")

  vars = {
    metrics_server_image_name    = <suppose the value would be computed>
    metrics_server_image_version = <suppose the value would be computed>
  }
}

data "kubectl_file_documents" "k8s_metrics_server" {
  content = data.template_file.k8s_metrics_server.rendered
}

data "kubectl_file_documents" "k8s_metrics_server_count_hack" {
  content = file("templates/metrics-server.yaml")
}

resource "kubectl_manifest" "k8s_metrics_server" {
  count            = length(data.kubectl_file_documents.k8s_metrics_server_count_hack.documents)

  yaml_body        = element(data.kubectl_file_documents.k8s_metrics_server.documents, count.index)
  wait_for_rollout = false
}

@andysousa
Copy link

@yongzhang did you get this hack working for kubectl_file_documents?

@yongzhang
Copy link

@yongzhang did you get this hack working for kubectl_file_documents?

yes it’s ugly but works.

@hsalluri259
Copy link

hsalluri259 commented Jul 27, 2021

This is an issue with Terraform and depending on vars from other modules; it's unable to compute the graph correctly, as it doesn't known how many documents the kubectl_path_documents data resource will produce.

Two alternatives you can use are:

1/ Hardcode the count in your kubectl_manifest call - not great but works
2/ Add another kubectl_path_documents resource which just sets your var to an empty string, then use that only for the count element. This way you still get a dynamic document count, but not depend on variables from other modules. Something like this:

data "kubectl_path_documents" "alb-ingress-controller" {
  pattern = "./alb-ingress-controller/*.yaml"
  vars = {
    vpc_id = module.vpc_id
  }
}

data "kubectl_path_documents" "alb-ingress-controller-count-hack" {
  pattern = "./alb-ingress-controller/*.yaml"
  vars = {
    vpc_id = ""
  }
}

resource "kubectl_manifest" "alb-ingress-controller" {
  count     = length(data.kubectl_path_documents.alb-ingress-controller-count-hack.documents)
  yaml_body = element(data.kubectl_path_documents.alb-ingress-controller.documents, count.index)
}

I tried this for my issue, I still get the same error.

data "kubectl_path_documents" "controller_manifests" {
  pattern = "${path.module}/files/controller.yaml"
}

data "kubectl_path_documents" "controller_manifests_hack" {
  pattern = "${path.module}/files/controller.yaml"
  vars = {
    eks_cluster_name = ""
  }
}

resource "kubectl_manifest" "install_controller" {
  count     = length(data.kubectl_path_documents.controller_manifests.documents)
  yaml_body = element(data.kubectl_path_documents.controller_manifests.documents, count.index)
}

╰─ terraform apply

Error: Invalid count argument

on modules/alb_controller/main.tf line 156, in resource "kubectl_manifest" "install_controller":
156: count = length(data.kubectl_path_documents.controller_manifests.documents)

The "count" value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be created. To work around this, use the -target argument to first apply only the resources that the count depends on.

@marcinprzybysz86
Copy link

This is an issue with Terraform and depending on vars from other modules; it's unable to compute the graph correctly, as it doesn't known how many documents the kubectl_path_documents data resource will produce.

Two alternatives you can use are:

1/ Hardcode the count in your kubectl_manifest call - not great but works 2/ Add another kubectl_path_documents resource which just sets your var to an empty string, then use that only for the count element. This way you still get a dynamic document count, but not depend on variables from other modules. Something like this:

data "kubectl_path_documents" "alb-ingress-controller" {
  pattern = "./alb-ingress-controller/*.yaml"
  vars = {
    vpc_id = module.vpc_id
  }
}

data "kubectl_path_documents" "alb-ingress-controller-count-hack" {
  pattern = "./alb-ingress-controller/*.yaml"
  vars = {
    vpc_id = ""
  }
}

resource "kubectl_manifest" "alb-ingress-controller" {
  count     = length(data.kubectl_path_documents.alb-ingress-controller-count-hack.documents)
  yaml_body = element(data.kubectl_path_documents.alb-ingress-controller.documents, count.index)
}

Hi @gavinbunney
It doesn't work even if I dont need any vars.
My code is just:

data "kubectl_path_documents" "docs" {
    pattern = "${path.module}/manifests/*.yaml"
} 

resource "kubectl_manifest" "argocd" {
    count     = length(data.kubectl_path_documents.docs.documents)
    yaml_body = element(data.kubectl_path_documents.docs.documents, count.index)
    override_namespace = "argocd"
}

│ Error: Invalid count argument
│
│   on modules\helm_argocd\main.tf line 13, in resource "kubectl_manifest" "argocd":
│   13:     count     = length(data.kubectl_path_documents.docs.documents)
│
│ The "count" value depends on resource attributes that cannot be determined
│ until apply, so Terraform cannot predict how many instances will be

Any tips?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants