title: terraform for_each index issue, invalid index
body: <blockquote>
  <p>The error seems to be same as what is mentioned in this post
  (<a href="https://stackoverflow.com/questions/66766117/terraform-for-each-loop-invalid-index">Terraform for_each loop. Invalid index</a>);
  though answer doesnt seems to be related to what I want to do; so
  posting as new question</p>
  </blockquote>
  <pre><code>Terraform version: 0.13.6
  aws provider version is: 3.7
  </code></pre>
  <p>What Im trying to do is, use a s3 bucket creation module in multiple account. The s3 bucket config, Im keeping it local. S3 bucket name will be passed via variable (list of strings), and if the s3 bucket name/s exist in local s3_buckets variable, the config should be taking from there. for ex, for account 1, only bucket1 will be created, for account2, bucket1 and bucket 3 will be created etc.</p>
  <p>Im hoping based on the &quot;var.s3_buckets&quot; value I pass, it will find the right config from local; however, and for some reason, it looks for all values, hence erroring out for with index I think. I think Im missing something, though cant figure out what it is..</p>
  <p>The variable Im passing is</p>
  <pre><code>var.kms_keys = {&quot;bucket2&quot;:&quot;org/bucket&quot;}
  var.s3_buckets = [&quot;bucket2]
  </code></pre>
  <p>The error Im getting is :</p>
  <pre><code>Error: Invalid index
  
    on modules/../main.tf line 22, in locals:
    22:       kms_key_arn = aws_kms_key.logging_kms_keys[&quot;bucket1&quot;].arn
      |----------------
      | aws_kms_key.logging_kms_keys is object with 1 attribute &quot;bucket2&quot;
  
  The given key does not identify an element in this collection value.
  </code></pre>
  <p>Code is</p>
  <pre><code>locals {
    s3_buckets = {
      bucket1 = { 
        kms_key_arn = aws_kms_key.logging_kms_keys[&quot;bucket1&quot;].arn
      },
      bucket2 = { 
        bucket_policy = templatefile(&quot;bucket2.json&quot;,
          {
            bucket_name = &quot;bucket2&quot;
          }
        )
        kms_key_arn = aws_kms_key.logging_kms_keys[&quot;bucket2&quot;].arn
      },
      bucket3= { 
        bucket_policy = templatefile(&quot;bucket3.json&quot;,
          {
            bucket_name = &quot;bucket3&quot;
            kms_key_arn = aws_kms_key.logging_kms_keys[&quot;bucket3&quot;].arn
          }
        )
        kms_key_arn = aws_kms_key.logging_kms_keys[&quot;bucket3&quot;].arn
      }
  
    }
  }
  .
  .
  resource &quot;aws_kms_key&quot; &quot;logging_kms_keys&quot; {
    for_each = var.kms_keys
  
    description         = &quot;${each.value} KMS Key&quot;
    enable_key_rotation = true
    policy              = lookup(local.kms_policies, each.key, &quot;&quot;)
    is_enabled          = true
  }
  
  resource &quot;aws_s3_bucket&quot; &quot;logging_buckets&quot; {
    for_each = toset(var.s3_buckets)
  
    bucket        = each.key
    acl           = lookup(local.s3_buckets[each.key], &quot;acl&quot;, &quot;private&quot;)
    policy        = lookup(local.s3_buckets[each.key], &quot;bucket_policy&quot;, &quot;&quot;)
    force_destroy = false
  
    server_side_encryption_configuration {
      rule {
        apply_server_side_encryption_by_default {
          kms_master_key_id = lookup(local.s3_buckets[each.key], &quot;kms_key_arn&quot;, data.aws_kms_alias.default_kms_key.arn)
          sse_algorithm     = &quot;aws:kms&quot;
        }
        bucket_key_enabled = true
      }
    }
  
  }
  </code></pre>