Search code examples
terraforminfrastructure

How to use terraform_remote_state with remote backend and prefix configuration?


I'm trying to use Terraform's terraform_remote_state from remote backend with the following configuration and getting the following error:

Error: Error refreshing state: 1 error occurred:
    * data.terraform_remote_state.network: 1 error occurred:
    * data.terraform_remote_state.network: data.terraform_remote_state.network: error loading the remote state: default state not supported
You can create a new workspace with the "workspace new" command.

Here is my Terraform files:

# terraform/aws/my-app/network/main.tf
# terraform workspace here is "stg"
terraform {
  required_version = "~> 0.11"

  required_providers {
    aws = "~ 2.11"
  }

  backend "remote" {
    hostname     = "app.terraform.io"
    organization = "my-org"

    workspaces {
      prefix = "my-app-network-"
    }
  }
}

locals {
  prefix_name = "${var.app_name}-${terraform.workspace}"

  tags = {
    app         = "${var.app_name}"
    environment = "${terraform.workspace}"
  }
}

provider "aws" {
  region = "${var.aws_region}"
}

data "aws_availability_zones" "available" {}

module "network" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "1.64.0"

  name = "${local.prefix_name}"

  cidr = "172.10.0.0/16"

  azs                 = "${slice(data.aws_availability_zones.available.names, 0, 2)}"
  public_subnets      = ["172.10.10.0/24", "172.10.11.0/24"]
  private_subnets     = ["172.10.20.0/24", "172.10.21.0/24"]
  database_subnets    = ["172.10.30.0/24", "172.10.31.0/24"]
  elasticache_subnets = ["172.10.40.0/24", "172.10.41.0/24"]

  enable_nat_gateway = true

  tags = "${local.tags}"
}


# terraform/aws/my-app/database/main.tf
# terraform workspace here is "stg"
terraform {
  required_version = "~> 0.11"

  required_providers {
    aws = "~ 2.11"
  }

  backend "remote" {
    hostname     = "app.terraform.io"
    organization = "my-ord"

    workspaces {
      prefix = "my-app-database-"
    }
  }
}

locals {
  prefix_name = "${var.app_name}-${terraform.workspace}"

  tags = {
    app         = "${var.app_name}"
    environment = "${terraform.workspace}"
  }
}

provider "aws" {
  region = "${var.aws_region}"
}

data "terraform_remote_state" "network" {
  backend = "remote"

  config {
    organization = "my-org"

    workspaces {
      prefix = "my-app-network-"
    }
  }
}

data "aws_availability_zones" "available" {}

resource "random_string" "password" {
  length  = 32
  special = false
}

resource "aws_rds_cluster" "this" {
  cluster_identifier = "${local.prefix_name}-mysql"

  availability_zones     = ["${slice(data.aws_availability_zones.available.names, 0, 2)}"]
  db_subnet_group_name   = "${data.terraform_remote_state.network.database_subnet_group}"

  database_name   = "database"
  master_username = "user"
  master_password = "${random_string.password.result}"

  backup_retention_period = 5
  preferred_backup_window = "07:00-09:00"
  skip_final_snapshot     = true

  tags = "${local.tags}"
}

Solution

  • If you're using Terraform Cloud service, and your Execution Mode is set to Remote, then the terraform.workspace variable won't be initialized properly. A temp fix - set Execution Mode to Local, and then the terraform.workspace will be initialized properly.

    This is a known bug: https://github.com/hashicorp/terraform/issues/22802 Hopefully it will be fixed soon.