5

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
  • 927
  • 6
  • 20
  • 38

1 Answers1

4

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
  • 17,184
  • 7
  • 46
  • 64
  • Yes, I couldn't find sufficient info in the documentation to implement --from-env-file option. But, yes I know the other way, I can modify the usage of this configMap inside my deployment. Thanks. – user691197 Aug 13 '19 at 04:49