Iterate over list of list of maps in terraform

前端 未结 1 1301
闹比i
闹比i 2021-01-12 17:44

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

Example:

    processes = [
      [
       {start_cmd: \"a-server-start\", attribute2:\"ty         


        
1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-12 18:43

    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
        }
      }
    }
    

    0 讨论(0)
提交回复
热议问题