Specify name of the environment variable for kubernetes pod in terraform with env_from

9/18/2019

I want o run a .net core application in a kubernetes cluster which is provided via terraform. Application connects to the database and reads the connection string from config like this:

services.AddDbContext<OrderContext>
            (options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

in terraform I have:

resource "kubernetes_secret" "api_config" {
  metadata {
    name = "api-config"
  }

  data = {        
    sql-connection = "....."
  }
}

resource "kubernetes_pod" "api" {
  metadata {
    name = "api"
    labels = {
      app = "api"
    }
  }

  spec {
    container {
      image = "......"
      name  = "......"

      env_from {
        #name = "SQLAZURECONNSTR_DefaultConnection"
        secret_ref {
          name = "sql-connection"
        }
      }

      port {
        container_port = 80
      }
    }
  }
}

As you see there is a gotcha. In order to provide Connection string section I need to name environment variable with a prefix "SQLAZURECONNSTR_", but I cannot specify the secret name with underscores. And I cannot specify the name of the environment variable if I want to use secrets.

What are my option if I do not want to use "env" and want to keep using secrets and I do not want to read the connection string from some other variable?

-- JleruOHeP
.net-core
kubernetes
terraform

0 Answers