如何将terraform_remote_state与远程后端和前缀配置一起使用?

时间:2019-05-21 18:32:35

标签: terraform infrastructure

我正在尝试通过以下配置使用terraform_remote_state后端的Terraform的remote并出现以下错误:

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.

这是我的Terraform文件:

# 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}"
}

1 个答案:

答案 0 :(得分:0)

如果您正在使用Terraform Cloud服务,并且您的执行模式设置为Remote,那么terraform.workspace变量将无法正确初始化。 临时修复程序-将“执行模式”设置为“本地”,然后将正确初始化terraform.workspace。

这是一个已知的错误:https://github.com/hashicorp/terraform/issues/22802 希望它将很快修复。