title: Terraform — how can I properly do a string interpolation in this code?
body: <p>My goal is to have this output:</p>
  <pre><code>DB_PASSWORD=&quot;secret1&quot;
  REDIS_PASSWORD=&quot;secret1&quot;
  </code></pre>
  <p>But I can't make it work and I'm getting this error during <code>terraform plan</code>.</p>
  <pre><code>Error: Invalid index
  ...
          random_password.app_secrets is object with 2 attributes
  
    The given key does not identify an element in this collection value.
  </code></pre>
  <p>Could you please help me how to fix this issue? Here's how my code looks like.</p>
  <pre><code>locals {
    require_new_secrets = toset([
      &quot;db&quot;,
      &quot;redis&quot;
    ])
  }
  
  resource &quot;random_password&quot; &quot;app_secrets&quot; {
    for_each = local.require_new_secrets
    length   = 16
    special  = false
  }
  
  resource &quot;aws_secretsmanager_secret&quot; &quot;app_secrets&quot; {
    name_prefix             = &quot;app-secrets-&quot;
    recovery_window_in_days = 7
    tags                    = var.tags
  }
  
  resource &quot;aws_secretsmanager_secret_version&quot; &quot;app_secrets&quot; {
    secret_id     = aws_secretsmanager_secret.app_secrets.id
    secret_string = &lt;&lt;-EOF
      %{for x in local.require_new_secrets}
      ${upper(&quot;{x}_PASSWORD&quot;)}=${random_password.app_secrets[&quot;{x}&quot;].result}
      %{endfor}
    EOF
  }
  </code></pre>
  <p>Thanks in advance!</p>