0

I am trying to access the Kubernetes_secret data.token attribute in terraform, but I keep on getting the error

Resource 'data.kubernetes_secret.misp_whitelist_secret' does not have attribute 'data.token' for variable 'data.kubernetes_secret.misp_whitelist_secret.data.token'

Whats the way to resolve this issue?

resource "kubernetes_service_account" "misp_whitelist_sa" {
  metadata {
    name = "misp-whitelist-sa"
  }
}

data "kubernetes_secret" "misp_whitelist_secret" {
  metadata {
    name      = "${kubernetes_service_account.misp_whitelist_sa.default_secret_name}"
    namespace = "${kubernetes_service_account.misp_whitelist_sa.metadata.0.namespace}"
  }
  depends_on = [
    "kubernetes_service_account.misp_whitelist_sa",
  ]
}

And I'm trying to access the data.token inside the terraform google_cloud_function resource

resource "google_cloudfunctions_function" "misp_whitelist_function" {
  name    = "${var.cluster}-misp-whitelist"
  ....<additional data> .....
  environment_variables = {
    CLUSTER = "${var.cluster}"
    PROJECT = "${var.project}"
    AUTH = "${data.kubernetes_secret.misp_whitelist_secret.data.token}"
  }
}

Sam-Tahir
  • 191
  • 3
  • 15

2 Answers2

1

Ok banged my head against a wall here for a really long time. The other answer is correct, but skips a crucial step.

You need to make sure that the secret declares the correct type (and also maybe specify the annotation?)

resource "kubernetes_secret" "vault" {
  metadata {
    name = "vault-token"
    annotations = {
      "kubernetes.io/service-account.name" = "vault"
    }
  }

  type = "kubernetes.io/service-account-token" // THIS!
}

Then, once you have the proper type specified, you can use the token

output "token" {
  value = kubernetes_secret.vault.data.token
}
Ryan Brink
  • 209
  • 2
  • 6
0

The correct way to access the data secret key is:

AUTH = "${data.kubernetes_secret.misp_whitelist_secret.data["token"]}"