Iterate over list of list of maps in terraform

岁酱吖の 提交于 2020-05-13 06:32:06

问题


Consider I have a variable that is a list of list of maps.

Example:

    processes = [
      [
       {start_cmd: "a-server-start", attribute2:"type_a"}, 
       {start_cmd: "a-worker-start", attribute2:"type_b"}
       {start_cmd: "a--different-worker-start", attribute2:"type_c"}
      ],
      [
       {start_cmd: "b-server-start", attribute2:"type_a"},
       {start_cmd: "b-worker-start", attribute2:"type_b"}
      ]
    ]

In each iteration, I need to take out the array of maps, then iterate over that array and take out the values of the map. How do I achieve this in terraform?

I have considered having two counts and doing some arithmetic to trick terraform into performing a lookalike nested iteration Check reference here. But in our case the number of maps in the inner array can vary.

Also we are currently using the 0.11 terraform version but dont mind using the alpha 0.12 version of terraform if it is possible to achieve this in that version.

Edit:

Added how I would use this variable:

resource “create_application” “applications” {
  // Create a resource for every array in the variable processes. 2 in this case
  name        = ""              
  migration_command = "" 

  proc {                
    // For every map create this attribute for the resource.
    name    = ““                
    init_command   = “a-server-start”                   
    type   = “server”                
  }                                    
}                                      

Not sure if this clears up the requirement. Please do ask if it is still not clear.


回答1:


Using terraform 0.12.x

locals {
  processes = [
    [
      { start_cmd: "a-server-start", type: "type_a", name: "inglorious bastards" },
      { start_cmd: "a-worker-start", type: "type_b", name: "kill bill" },
      { start_cmd: "a--different-worker-start", type: "type_c", name: "pulp fiction" },
    ],
    [
      { start_cmd: "b-server-start", type: "type_a", name: "inglorious bastards" },
      { start_cmd: "b-worker-start", type: "type_b", name: "kill bill" },
    ]
  ]
}

# just an example
data "archive_file" "applications" {
  count = length(local.processes)

  type = "zip"

  output_path = "applications.zip"

  dynamic "source" {
    for_each = local.processes[count.index]

    content {
      content  = source.value.type
      filename = source.value.name
    }
  }
}
$ terraform apply
data.archive_file.applications[0]: Refreshing state...
data.archive_file.applications[1]: Refreshing state...

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

If a create_application resource existed, it can be modeled like so

resource "create_application" "applications" {
  count             = length(local.processes)
  name              = ""
  migration_command = "" 

  dynamic "proc" {
    for_each = local.processes[count.index]

    content {
      name         = proc.value.name
      init_command = proc.value.start_cmd
      type         = proc.value.type
    }
  }
}


来源:https://stackoverflow.com/questions/55487972/iterate-over-list-of-list-of-maps-in-terraform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!