Terraform 0.12 nested for loops

后端 未结 1 556
南笙
南笙 2021-02-04 03:41

I am trying to implement nested for loops using Terraform 0.12\'s new features in order to loop through AWS IAM users, each of which can have one or more policies attached. The

相关标签:
1条回答
  • 2021-02-04 04:10

    The for expression in your local value association-list is producing a list of list of lists of strings, but your references to it are treating it as a list of lists of strings.

    To get the flattened representation you wanted, you can use the flatten function, but because it would otherwise group everything into a single flat list I'd recommend making the innermost value an object instead. (That will also make the references to it clearer.)

    locals {
      association-list = flatten([
        for user in keys(var.iam-user-policy-map) : [
          for policy in var.iam-user-policy-map[user] : {
            user   = user
            policy = policy
          }
        ]
      ])
    }
    

    The result of this expression will have the following shape:

    [
      { user = "user1", policy = "policy1" },
      { user = "user1", policy = "policy2" },
      { user = "user2", policy = "policy2" },
    ]
    

    Your references to it can then be in the following form:

    user = local.association-list[count.index].user
    
    policy_arn = "arn:aws-us-gov:iam::aws:policy/${local.association-list[count.index].policy}"
    
    0 讨论(0)
提交回复
热议问题