terraform_remote_state for gcs backend is not workspace-aware.

Created on 22 Jan 2018  ยท  10Comments  ยท  Source: hashicorp/terraform

Terraform Version

Terraform v0.11.2

  • provider.google v1.5.0

Terraform Configuration Files

When using terraform workspace select staging, setting the backend in common.tf like this:

terraform {
  backend "gcs" {
    project = "app"
    bucket  = "my-bucket"
    prefix  = "terraform/state"
  }
}

output "my_value" {
  value = "something"
}

Creates gs://app/my-bucket/terraform/state/staging.tfstate as expected

When trying to reference that in another location, using the same workspace:

data "terraform_remote_state" "common" {
  backend = "gcs"

  config {
    project = "app"
    bucket  = "my-bucket"
    prefix  = "terraform/state"
  }
}

resource "some_resource" "name" {
  val = "${data.terraform_remote_state.common.my_value}"
}

There is an error: Resource 'data.terraform_remote_state.common' does not have attribute 'my_value'.

Expected Behavior

This output should have been found and correctly referenced.

Actual Behavior

The value cannot be found.

Additional Context

This can be fixed by using the full path, instead of the prefix. For example:

data "terraform_remote_state" "common" {
  backend = "gcs"

  config {
    bucket = "my-bucket"
    path   = "terraform/state/${terraform.workspace}.tfstate"
  }
}

resource "some_resource" "name" {
  val = "${data.terraform_remote_state.common.my_value}"
}

works as expected. However, path is deprecated, and prefix should be used instead.

backengcs documentation

Most helpful comment

The documentation on this is really poor: https://www.terraform.io/docs/providers/terraform/d/remote_state.html

Nowhere on this page does it say to configure a workspace.

All 10 comments

(Using a name parameter in config{} set to "${terraform.workspace}" doesn't work either.)

Hi @majelbstoat,

The workspace has to be explicitly passed to the terraform_remote_state data type, in order for it to be possible to access a different remote workspace name.

data "terraform_remote_state" "common" {
  backend = "gcs"
  workspace = "${terraform.workspace}"

  config {
    project = "app"
    bucket  = "my-bucket"
    prefix  = "terraform/state"
  }
}

The teraform-provider specific docs however do need to be updated to reflect the change from environment to workspace.

Fantastic, thanks so much. Will check tonight and confirm.

Edit: works! Keeping this open for the documentation issue.

Tried changing from environment to workspace but it doesn't work:

Config:

data "terraform_remote_state" "network" {
  backend       = "s3"
  workspace   = "${terraform.workspace}"
  config {
    bucket = "${var.state_bucket}"
    key    = "terraform.tfstate"
    region = "${var.region}"
  }
}

There is a state file in s3 but it's an empty state. Locally state files are fine.

Hi @downspot,

What exactly doesn't work? You mention that there's only an empty state file in S3, so there is nothing for the data source to fetch.

This issue if about the feature in the gcs backend, so please feel free to open a new issue with the information to reproduce the problem, along with the version of terraform used.

Same here.
Getting remote state from s3 is not workspace-aware.
Setting full path to key works.

Doesn't work:

data "terraform_remote_state" "vpc" {
  backend = "s3"

  workspace = "${terraform.workspace}"

  config {
    bucket = "X"

    # key    = "networking/${terraform.workspace}/terraform.tfstate"
    key = "terraform.tfstate"

    workspace_key_prefix = " networking"
    region  = "eu-west-1"
  }
}

Work's:

data "terraform_remote_state" "vpc" {
  backend = "s3"

  # workspace = "${terraform.workspace}"

  config {
    bucket = "X"

    key    = "networking/${terraform.workspace}/terraform.tfstate"
    # key = "terraform.tfstate"

    # workspace_key_prefix = " networking"
    region  = "eu-west-1"
  }
}

@midN I'm able to reproduce it with s3 as well. Should this be documented as a separate issue? btw... Thank you for the workaround!

The documentation on this is really poor: https://www.terraform.io/docs/providers/terraform/d/remote_state.html

Nowhere on this page does it say to configure a workspace.

I am going to close this issue; both the remote backend and documentation have changed quite a bit since the original issue was opened.

If there is still a question, I recommend the the community forum, where there are far more people available to help. If there is a bug or you would like to make a feature request, please open a new issue and fill out the template.
Thanks!

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings