Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
Deployment invalid Terraform + Kubernetes: spec.template.spec.containers[0].envFrom: Invalid value: ""

I'm experimenting with terraform to deploy k8s resources.

I created a mongodb deployment

provider "kubernetes" {
  config_context = "kubernetes-admin@kubernetes"
}

resource "kubernetes_namespace" "demo-namespace" {
  metadata {
    name = "my-demo-namespace"
  }
}
// mongodb
resource "kubernetes_deployment" "mongodb" {
  metadata {
    name = "mongodb"
    namespace = kubernetes_namespace.demo-namespace.metadata[0].name
    labels = {
      app = "mongodb"
    }
  }

  spec {
    replicas = 1

    selector {
      match_labels = {
        app = "mongodb"
      }
    }

    template {
      metadata {
        labels = {
          app = "mongodb"
        }
      }

      spec {
        container {
          image = "mongo"
          name  = "mongodb"

          env_from { 
            secret_ref {
              name = kubernetes_secret.scrt-mongodb.metadata[0].name
            }
            config_map_ref {
              name = kubernetes_config_map.cm-mongodb.metadata[0].name  
            }
          }

          resources {
            limits {
              cpu    = "500m"
              memory = "1Gi"
            }
            requests {
              cpu    = "150m"
              memory = "256Mi"
            }
          }

          liveness_probe {
            exec {
              command =  ["bash", "-c", "mongo -u $MONGO_INITDB_ROOT_USERNAME -p $MONGO_INITDB_ROOT_PASSWORD --eval db.adminCommand(\"ping\")"]
            }
            initial_delay_seconds = 3
            period_seconds        = 1
          }
        }
      }
    }
  }
}

// mongodb configmap
resource "kubernetes_config_map" "cm-mongodb" {
  metadata {
    name = "cm-mongodb"
    namespace = kubernetes_namespace.demo-namespace.metadata.0.name
  }

  // improve creds with secret
  data = {
    MONGO_INITDB_DATABASE = "movies"
  }
}

// monbodb secret
resource "kubernetes_secret" "scrt-mongodb" {
  metadata {
    name = "mongodb-creds"
  }

  data = {
    MONGO_INITDB_ROOT_USERNAME = "root-user"
    MONGO_INITDB_ROOT_PASSWORD = "secret"
  }

  type = "opaque"
}

This fails with:

kubernetes_config_map.cm-mongodb: Creation complete after 0s [id=my-demo-namespace/cm-mongodb]
kubernetes_deployment.mongodb: Creating...

Error: Failed to create deployment: Deployment.apps "mongodb" is invalid: spec.template.spec.containers[0].envFrom: Invalid value: "": may not have more than one field specified at a time

  on template.tf line 12, in resource "kubernetes_deployment" "mongodb":
  12: resource "kubernetes_deployment" "mongodb" {

What is wrong here?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You missed this line:

namespace = kubernetes_namespace.demo-namespace.metadata.0.name

You did not define the resource in the desired namespace so terraform failed to "find" the desired value.


// monbodb secret
resource "kubernetes_secret" "scrt-mongodb" {
  metadata {
    name = "mongodb-creds"
    # -------------------------------------------------------------
    # -------------------------------------------------------------
    # Add the namespace here
    namespace = kubernetes_namespace.demo-namespace.metadata.0.name
    # -------------------------------------------------------------
    # -------------------------------------------------------------
    }

  data = {
    MONGO_INITDB_ROOT_USERNAME = "root-user"
    MONGO_INITDB_ROOT_PASSWORD = "secret"
  }

  type = "opaque"
}

enter image description here

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!