<p>How do I load a configMap into an environment variable?</p>

<p><strong>Things I've done</strong></p>

<p>Kubernetes <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#define-container-environment-variables-using-configmap-data" rel="nofollow noreferrer">documentation</a> describes just this scenario, and following it:</p>

<p>I've actually set up my <code>configMap</code> through Terraform with this:</p>

<pre><code>resource "kubernetes_config_map" "production_database_host" {
  metadata {
    name = "production-database-host"
  }

  data {
    connection_name = "${google_sql_database_instance.master.connection_name}"
  }
}
</code></pre>

<p>But via Kubernetes, it would look like this:</p>

<pre><code>apiVersion: v1
data:
  connection_name: this_string_is_redacted
kind: ConfigMap
metadata:
  creationTimestamp: 2018-10-12T05:49:49Z
  name: production-database-host
  namespace: default
  resourceVersion: "316273"
  selfLink: /api/v1/namespaces/default/configmaps/production-database-host
  uid: a1c06423-cde2-11e8-b615-42010a800235
</code></pre>

<p>(Fetched by running <code>kubectl get configmap production-database-host -o yaml</code>)</p>

<p>Now, I also have a working container, in a deployment, where I added an environment variable like so:</p>

<pre><code>env:
 - name: INSTANCE_CONNECTION_NAME
   valueFrom:
     configMapKeyRef:
       name: production-database-host
       key: connection_name
</code></pre>

<p>However, applying this config gives me:</p>

<pre><code>$ kubectl apply -f .

error: error converting YAML to JSON: yaml: line 39: did not find expected key
</code></pre>

<p>What am I doing wrong here? Why won't this simply load <code>this_string_is_redacted</code> into the <code>INSTANCE_CONNECTION_NAME</code> environment variable?</p>

<p><strong>Edit:</strong> All the source for my infrastructure is in this <a href="https://github.com/amingilani/infrastructure" rel="nofollow noreferrer">repo</a>. The Terraform files are applied first, they create the Kubnernetes cluster and add the configMap. Then I apply the Kubernetes config.</p>

Amin Shah Gilani
<p>It was a formatting issue, unfortunately the block:</p>

<pre><code>env:
 - name: INSTANCE_CONNECTION_NAME
   valueFrom:
     configMapKeyRef:
       name: production-database-host
       key: connection_name
</code></pre>

<p>Was indented one space more than I should have been. Everything else works fine.</p>
