0

In terraform version 1.1.9 am facing the below issue while doing terraform apply. Help me to fix how this for_each can be done without error.

rke_nodes values sample will be :

# Outputs
output "rancher_nodes" {
  value = [
        for instance in flatten([[aws_instance.node_all], [aws_instance.node_master], [aws_instance.node_worker]]): {
    public_ip  = instance.public_ip
    private_ip = instance.private_ip
    hostname   = instance.id
    user       = var.node_username
    roles      = split(",", instance.tags.K8sRoles)
    ssh_key    = file(var.ssh_key_file)
    }
  ]
  sensitive = true
}

I have variable.tf :

variable "rke_nodes" {
  type = list(object({
    public_ip = string
    private_ip = string
    hostname = string
    roles = list(string)
    user = string
    ssh_key = string
  }))
  description = "Node info to install RKE cluster"
}

main.tf :

# Provision RKE cluster on provided infrastructure
resource "rke_cluster" "rancher_cluster" {
  cluster_name = var.rke.cluster_name

  dynamic nodes {
    for_each = var.rke_nodes
    content {
      address           = nodes.value.public_ip
      internal_address  = nodes.value.private_ip
      hostname_override = nodes.value.hostname
      user              = nodes.value.user
      role              = nodes.value.roles
      ssh_key           = nodes.value.ssh_key
    }
  }
  upgrade_strategy {
    drain                        = false
    max_unavailable_controlplane = "1"
    max_unavailable_worker       = "10%"
  }

  kubernetes_version = var.rke.kubernetes_version

}

I got error when terraform apply :

╷
│ Error: Invalid dynamic for_each value
│
│   on .terraform/modules/rke-cluster/main.tf line 6, in resource "rke_cluster" "rancher_cluster":
│    6:     for_each = var.rke_nodes
│     ├────────────────
│     │ var.rke_nodes has a sensitive value
│
│ Cannot use a list of object value in for_each. An iterable collection is required.

Actual Value when apply it can be list in sometimes:

- nodes {
    - address           = "65.2.140.68" -> null
    - hostname_override = "i-0d5bf5f22fb84f5d4" -> null
    - internal_address  = "10.30.8.120" -> null
    - labels            = {} -> null
    - role              = [
        - "controlplane",
        - "etcd",
        - "worker",
      ] -> null
    - ssh_agent_auth    = false -> null
    - ssh_key           = (sensitive value)
    - user              = (sensitive value)
  }
sethu ram
  • 23
  • 1
  • 6

1 Answers1

1

You don't need index. It just should be:

  for_each = var.rke_nodes

Note: This works only for dynamic blocks. If you use for_each in resource blocks, this form of for_each (list of maps) will not work.

Marcin
  • 215,873
  • 14
  • 235
  • 294
  • I don't think that would work as you can only for_each a map or set of strings but they are providing a list of objects. – Jake Nelson Jul 07 '22 at 02:38
  • @JakeNelson That's not the case. In dynamic blocks these restrictions do not apply. – Marcin Jul 07 '22 at 02:39
  • Interesting, I'll have to go and do my homework! – Jake Nelson Jul 07 '22 at 02:40
  • @JakeNelson Yes, I think its a good idea. – Marcin Jul 07 '22 at 02:44
  • 1
    I've removed my answer as yours seems correct and mine wasn't. I've tried to undo my downvote of your answer but it won't let me until your answer is edited for some reason! Sorry about that. Edit: Fixed my vote. Sorry again, Marcin! – Jake Nelson Jul 07 '22 at 04:42
  • 1
    @JakeNelson No problem. I updated the answer. I think now you will be able to un-downvoate it, if you want. – Marcin Jul 07 '22 at 04:45
  • Used : for_each = var.rke_nodes Getting same error : Error: Invalid dynamic for_each value Cannot use a list of object value in for_each. An iterable collection is required. – sethu ram Jul 07 '22 at 05:58
  • @sethuram Please update your code with new version of it, also provide actual value of `rke_nodes` that you are using. – Marcin Jul 07 '22 at 06:09
  • @sethuram What is the actual value of `var.rke_nodes` that you use? – Marcin Jul 07 '22 at 06:55
  • @sethuram I mean the **actual** value `rke_nodes`. Not its definition. – Marcin Jul 07 '22 at 07:54
  • @sethuram What you provided is not `rke_nodes` as it does not conform to its `type`. Maybe someone else will be able to help you, as I see my comments go nowhere. – Marcin Jul 07 '22 at 08:52