Terraform kubernetes_config_map --from-env-file

8/11/2019

I am creating a kubernetes configMap using '--from-env-file' option to store the file contents as environment variables.

kubectl create configmap env --from-env-file=env.properties -n namespace

When I create a terraform resource as below, the created configMap contains a file, not environment variables.

resource "kubernetes_config_map" "env" {
  metadata {
    name = "env-config"
    namespace = var.namespace
  }
  data = {
    "env.properties"   = "${file("${path.module}/env.properties")}"
  }
}

How to create configMap with file content as environment variables using terraform-kubernetes-provider resource ?

-- user691197
configmap
kubernetes
terraform
terraform-provider-kubernetes

1 Answer

8/12/2019

If env.properties looks like this:

$ cat env.properties
enemies=aliens
lives=3
allowed="true"

Then kubectl create configmap env --from-env-file=env.properties -n namespace would result in something like this:

apiVersion: v1
kind: ConfigMap
metadata:
  name: env
  namespace: namespace
data:
  allowed: '"true"'
  enemies: aliens
  lives: "3"

But what you're doing with Terraform would result in something more like this:

apiVersion: v1
kind: ConfigMap
metadata:
  name: env
  namespace: namespace
data:
  env.properties: |
    enemies=aliens
    lives=3
    allowed="true"

Based on the Terraform docs it appears that what you're looking for, i.e. some native support for --from-env-file behaviour within the Terraform provider, is not possible.

The ConfigMap format that you get doing it the Terraform way could still be useful, you might just have to change how you're pulling the data from the ConfigMap into your pods/deployments. If you can share more details, and even a simplified/sanitized example of your pods/deployments where you're consuming the config map, it may be possible to describe how to change those to make use of the different style of ConfigMap. See more here.

-- Amit Kumar Gupta
Source: StackOverflow