title: Terraform loop in resource to merge map
body: <p>I'm trying to dynamically set the contents of a dictionary within a resource.
  In my case I want to set per Lambda specific environment variables. I really don't want to modify the
  local.lambda structure in any way, but I can't quite work out the syntax to iterate over the
  local.lambda dictionary, lookup the extra vars and then their values dynamically. Any ideas on how I can achieve this?</p>
  <p>Here's my locals data</p>
  <pre><code>locals {
    lambdas = {
      func1 = &quot;myfunc-${local.env}&quot;
      func2 = &quot;myfunc-${local.env}&quot;
      func3 = &quot;myfunc-${local.env}&quot;
    }
  
  
    envvars = {
      myenv1 = &quot;apple&quot;
      myenv2 = &quot;pear&quot;
      myenv3 = &quot;orange&quot;
    }
  
    extra_envvars = {
      myenv4 = &quot;lettuce&quot;
      myenv5 = &quot;carrot&quot;
      myenv6 = &quot;turnip&quot;
    }
  
    assign_extra_vars = {
      func1 = [ myenv4, myenv5 ]
      func3 = [ myenv6 ]
    }
  }
  </code></pre>
  <p>Here's my Terraform resource with a little block of pseudo code which hopefully explain what I'm trying to achieve better than my broken Terraform.</p>
  <pre><code>resource &quot;aws_lambda_function&quot; &quot;task&quot; {
    for_each      = local.lambdas
  
    environment&quot; {
      // Pseudo code
      my_extra = map {}
      for env_var in local.assign_extra_vars[each.key] : {
        my_extra[env_var] = lookup(local.extra_envvars, env_var, &quot;&quot;)
      }
      // end pseudo code
  
      variables = merge(envvars, my_extra)
   
    }
  }
  </code></pre>
  <p>Thanks in advance for any reply.</p>