Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

data source terraform_remote_state with workspaces

Tags:

terraform

I'm running terraform v0.14.8 with a non-default terraform workspace.

I have an Atlantis server that handles my plans and applies.

When I clone my repo locally and run my plans I get errors about my datasource. I don't quite understand why as I don't get these errors on my Atlantis server which I believe performs the same operations. The Atlantis server also uses tf v0.14.8.

My terraform:

data "terraform_remote_state" "route53" {
  backend = "s3"

  config = {
    key      = "web/terraform.tfstate"
    region   = "us-west-2"
    bucket   = "prod-terraform"
    role_arn = "arn:aws:iam::xxxxxxxxxx:role/atlantis"
}

Before I run my local plan, i switch the workspace

terraform workspace select web

# in addition I also tried
export TF_WORKSPACE=web

My plan

teraform plan
...

Error: Unable to find remote state

  on provider.tf line 46, in data "terraform_remote_state" "route53":
  46: data "terraform_remote_state" "route53" {

No stored state was found for the given workspace in the given backend.

I could easily edit my "key" with env: and things will work, but I'm trying to figure out how to do this without making that adjustment, seeing that my Atlantis server just works.

data "terraform_remote_state" "route53" {
  backend = "s3"

  config = {
    key      = "env:/web/web/terraform.tfstate"
    region   = "us-west-2"
    bucket   = "prod-terraform"
    role_arn = "arn:aws:iam::xxxxxxxxxx:role/atlantis"
}
like image 956
sebastian Avatar asked Jan 30 '26 16:01

sebastian


1 Answers

Your question seems to imply some confusion over which of these backends the web workspace is selected for. Running terraform workspace select web selects the web workspace from the backend of the current configuration (the directory where you are running Terraform), but I suspect your intent is to select the web backend from the backend you've configured in data "terraform_remote_state" instead.

If so, you can do that by setting the workspace argument in the data source configuration:

data "terraform_remote_state" "route53" {
  backend   = "s3"
  workspace = "web"

  config = {
    key      = "web/terraform.tfstate"
    region   = "us-west-2"
    bucket   = "prod-terraform"
    role_arn = "arn:aws:iam::xxxxxxxxxx:role/atlantis"
  }
}
like image 59
Martin Atkins Avatar answered Feb 01 '26 08:02

Martin Atkins



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!