Resource does not have attribute

Resolve a Terraform data source issue

Janosch Maier
CrashtestSec
2 min readNov 29, 2018

--

At Crashtest Security we provision our infrastructure using Terraform. Therefore we are able to recreate whole Kubernetes clusters within minutes. We use our Terraform setup also to integrate some external tools such as Vault within our cluster.

Vault needs to the Kubernetes API to work properly. Therefore, we create a Kubernetes service account using Terraform and provision Vault with its JWT token.

When you implement this naively, you will end up creating a Kubernetes service account, getting its secret (using this provider data source) and then trying to use the secret's token as input for the Vault configuration. However any access to data.kubernetes_secret.vault_tokenreviewer.data.token will fail, if the service account in Kubernetes is not existing yet. You will receive an error message like resource data.kubernetes_secret_vault_tokenreviewer does not have attribute data.token. This appears, because the data attribute is a map, which does not contain any value until the service account is created.

This issue can be circumvented using the lookup function in Terraform. So instead of accessing data.kubernetes_secret.vault_tokenreviewer.data.token, you access lookup(data.kubernetes_secret.vault_tokenreviewer.data, "token", ""). So when the secret does not exist yet during the planning phase, it won't break the plan. During the apply phase the value is calculated correctly.

The following plan runs smoothly then:

Sources:

--

--