Kay
Kay

Reputation: 19660

terraform when using data sources no stored state was found for the given workspace in the given backend

I am using terraform to setup an aws resource that relies on output from an earlier resource which has its own state. I am making use of data source in terraform to try and achieve this https://www.terraform.io/docs/language/data-sources/index.html

State

I have two seperate terraform init/states. One networking and one eks. The state is stored on s3 and i make use of workspaces.

s3://myorg-terraform-state/env:/prod/networking/terraform.tfstate
s3://myorg-terraform-state/env:/prod/eks/terraform.tfstate

networking/output.tf

I have a networking terraform that outputs the following.

output "vpc_id" {
  description = "The ID of the VPC"
  value       = module.vpc.vpc_id
}

output "private_subnets" {
  description = "List of IDs of private subnets"
  value       = module.vpc.private_subnets
}

output "worker_group_mgmt_one_id" {
  description = "The id of worker_group_mgmt_one"
  value       = aws_security_group.worker_group_mgmt_one.id
}

output "worker_group_mgmt_two_id" {
  description = "The id of worker_group_mgmt_two"
  value       = aws_security_group.worker_group_mgmt_two.id
}

EKS requires the above variables so i create a data source to be able to access them.

eks/data.tf

data "terraform_remote_state" "networking" {
    backend = "s3"
    config = {
        bucket = "myorg-terraform-state"
        key    = "networking/terraform.tfstate"
        region =  "eu-west-2"
    }
}

eks/main.tf

I then use this datasource in eks/main.tf

...
data.terraform_remote_state.networking.outputs.private_subnets
...

Problem

However when i run the following command i get an error despite the fact that the state exists on s3 and after checking it i can see the outputs.

terraform workspace select prod
terraform plan -var-file=prod.tfvars

Error: Unable to find remote state

on data.tf line 1, in data "terraform_remote_state" "networking":
1: data "terraform_remote_state" "networking" {

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

Folder Structure

├── networking
│   ├── backend.tf
│   ├── main.tf
│   ├── output.tf
│   ├── prod.tfvars
│   ├── provider.tf
│   └── variables.tf
├── eks
│   ├── backend.tf
│   ├── data.tf
│   ├── main.tf
│   ├── output.tf
│   ├── prod.tfvars
│   ├── provider.tf
│   └── variables.tf
├── README.md


Upvotes: 5

Views: 15091

Answers (1)

Kay
Kay

Reputation: 19660

I found the solution.

The terraform state for networking is stored in the following path env:/prod/networking/terraform.tfstate

You can see i an missing the following prefix in my data config.

"env:/prod"
data "terraform_remote_state" "networking" {
    backend = "s3"
    config = {
        bucket = "myorg-terraform-state"
        key    = "env://${local.workspace}/networking/terraform.tfstate"
        region =  "eu-west-2"
    }
}

Upvotes: 5

Related Questions