title: Creating a dynamic secret variable block within Terraform for Cloud Run
body: <p>I'm trying to create the following block dynamically based on a list of strings</p>
  <pre><code>        env {
            name = &quot;SECRET_ENV_VAR&quot;
        value_from {
              secret_key_ref {
                name = google_secret_manager_secret.secret.secret_id
                key = &quot;1&quot;
              }
            }
          }
  </code></pre>
  <p>Based off documentation: <a href="https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloud_run_service#example-usage---cloud-run-service-secret-environment-variables" rel="nofollow noreferrer">https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloud_run_service#example-usage---cloud-run-service-secret-environment-variables</a></p>
  <p>I would like to dynamically add Secrets, and have defined the following dynamic block:</p>
  <pre><code>        dynamic &quot;env&quot; {
            for_each = toset(var.secrets)
            content {
              name = each.value
              value_from {
                secret_key_ref {
                  name = each.value
                  key  = &quot;1&quot;
                }
              }
            }
          }
  </code></pre>
  <p>Where <code>secrets</code> is a variable of type list(string)</p>
  <p>However, this throws an error: <code>Blocks of type &quot;value_from&quot; are not expected here.</code></p>
  <p>I'm not sure what I'm missing, or where I have incorrectly specified the value_from block.</p>
  <p>Could someone point me in the right direction for fixing this up?</p>
  <p>UPDATE;
  I have also tried to implement this variable as a map, as per the suggestion in the comments on this post. (<a href="https://www.terraform.io/docs/language/expressions/dynamic-blocks.html#multi-level-nested-block-structures" rel="nofollow noreferrer">https://www.terraform.io/docs/language/expressions/dynamic-blocks.html#multi-level-nested-block-structures</a>)</p>
  <pre><code>        dynamic &quot;env&quot; {
            for_each = var.secrets
            content {
              name = each.key
              dynamic &quot;value_from&quot; {
                for_each = env.value.name
                secret_key_ref {
                  name = value_from.value.name
                  key  = value_from.value.version
                }
              }
            }
          }
  </code></pre>
  <p>However, this also gives the same error.  <code>Blocks of type &quot;value_from&quot; are not expected here.</code></p>
  <p>In this example, the secrets variable is defined as a list(any) with this value:</p>
  <pre><code>secrets = [
      {
        name    = &quot;SECRET&quot;
        version = &quot;1&quot;
      }
    ]
  </code></pre>