0

I am using map(object) for my terraform module to implement Kubernete cluster. The requirement is to use map(object) variable type.

This is the variable I am using:

variable "user_node_pool" {
  type = map(object({
    mode              = string
    vm_size           = string
    user_max_pods     = number
    user_os_size      = number
    node_os           = string
    user_auto_scaling = bool
    user_min_count    = number
    user_max_count    = number

  }))

}

If I use below values as my input it works, but we don't want to pass values like this. We want to pass the values as a single line parameter:

user_node_pools = {
    pool2 = {
      mode            = "User"
      vm_size         = "Standard_D2_v3"
      user_max_pods   = 110
      user_os_size    = 128
      node_os         = "Windows"
      user_auto_scaling = true
      user_min_count  = 1
      user_max_count  = 1
    } }

Unfortunately the requirement is to pass on this value as a single line parameter because of few environmental dependencies. Something like below, but this is failing because of "Missing newline after argument" error.

user_node_pools = "{"pool2" : { "mode" : "User", "vm_size" : "Standard_D2_v3", "user_max_pods" : 110, "user_os_size" : 128, "node_os" : "Windows", "user_auto_scaling" : "true", "user_min_count" : 1, "user_max_count" : 1}}"

I am getting below error while using this syntax:

enter image description here

My ask here is:

Please let me know how can I pass on the map(object) variable inputs as a single line. What is the correct syntax for that?

  • So it needs to be JSON? – Marko E Mar 23 '23 at 07:50
  • Yes that's correct. I am pretty new to this. But that's what I can think of based on what they are doing. – Susheel Bhatt Mar 23 '23 at 07:52
  • 1
    Can you try using `jsonencode(var.user_node_pools)` wherever you are using that variable? Bare in mind that the argument requires a variable of a certain type, so it might not work either way. – Marko E Mar 23 '23 at 07:54
  • Tried that. Still gives same error. Added this in both my parent and child module. – Susheel Bhatt Mar 23 '23 at 07:57
  • Can you share the resource where you are trying to use this? – Marko E Mar 23 '23 at 08:03
  • https://github.com/bhatts11/Azure_Kubernetes_Service_Multiple_NodePool. Created a git repo for this. – Susheel Bhatt Mar 23 '23 at 08:04
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/252714/discussion-between-susheel-bhatt-and-marko-e). – Susheel Bhatt Mar 23 '23 at 08:07
  • I was able to fix this. I was facing this issue because of additional " in the value. It worked after using these values: `user_node_pools = {"pool2" : { "mode" : "User", "vm_size" : "Standard_D2_v3", "user_max_pods" : 110, "user_os_size" : 128, "node_os" : "Windows", "user_auto_scaling" : "true", "user_min_count" : 1, "user_max_count" : 1}}` – Susheel Bhatt Mar 23 '23 at 09:49

1 Answers1

0

Pass it in as a string and then decode the string into a local variable.

Or just don't do complex data structures in root nodules and use a simple string for the path to the config file you want to pass in. Likely more maintainable of an option.

Generally I think it is good practice to avoid complex types as input variables in ROOT modules. If you must, keep it simple, draw a line and if you are having to make too many concessions (i.e., like being opinionated about the formatting of your input values) cut bait and use a different solution because it's no longer an input variable but actual config.

Azure Terraformer
  • 1,648
  • 1
  • 14
  • 33