0
votes

I have a module cluster that spins up a GKE cluster and associated GCS buckets. The backend for this is a GCS bucket called cluster_tf_state_bucket

I have defined an output in this module

output "vault_storage_bucket_name" {
  value = "${google_storage_bucket.vault_storage.name}"
}

Once I run the code, I get the output

✗ terraform output
vault_storage_bucket_name = vault-storage

Now I have a another module vault and in that module I need to invoke the value of vault_storage_bucket_name from the other module that is using a different GCS backend bucket.

So in the main.tf of my vault module, Im doing something like

terraform {
  required_version = ">= 0.12.2"
}

terraform {
  backend "gcs" {
    bucket = "app_tf_state_bucket"
    prefix = "vault"
  }
}
data "terraform_remote_state" "cluster_vault" {
  backend = "gcs"
  config = {
    bucket  = "cluster_tf_state_bucket"
  }
}

module "vault" {
  source                = "../../../modules/vault"
  env                   = "test"
  region                = "europe-west5"
  storage_bucket_name   = "${data.terraform_remote_state.cluster_vault.vault_storage_bucket_name}"

But when I run this code, I get

Error: Unsupported argument

  on main.tf line 24, in module "vault":
  24:   storage_bucket_name   = "${data.terraform_remote_state.cluster_vault.vault_storage_bucket_name}"

An argument named "storage_bucket_name" is not expected here.

I resolved the above issue by adding the below to the `variables.tf file

variable "storage_bucket_name"{
  type = "string"
}

And now I run into the error

Error: Unsupported attribute

  on main.tf line 24, in module "vault":
  24:   storage_bucket_name   = "${data.terraform_remote_state.cluster_vault.vault_storage_bucket_name}"

This object has no argument, nested block, or exported attribute named
"vault_storage_bucket_name".

What am I missing here ?

1
Most likely you have not defined the storage_bucket_name variable in your vault module.Blokje5
Thanks that resolved it. But now I get This object has no argument, nested block, or exported attribute named "vault_storage_bucket_name". Would you know why ?Jason Stanley
That is a difference in how state works in 0.12: github.com/hashicorp/terraform/issues/21442Blokje5

1 Answers

1
votes

The output values from the upstream state snapshot are exposed on terraform_remote_state via an attribute called outputs, so you need to include that in your expression:

  storage_bucket_name   = data.terraform_remote_state.cluster_vault.outputs.vault_storage_bucket_name

Note the extra .outputs before .vault_storage_bucket_name. That outputs attribute is a map value, so you can potentially also use the map as a whole in expressions. For example, you might want to create a Local Value if you expect to be referring to those remote attributes a lot and want to simplify the references:

locals {
  vault_cluster = data.terraform_remote_state.cluster_vault.outputs
}

module "vault" {
  source                = "../../../modules/vault"
  env                   = "test"
  region                = "europe-west5"
  storage_bucket_name   = local.vault_cluster.vault_storage_bucket_name
}