是否可以将terraform远程状态包装到模块中并从状态输出变量?

时间:2019-07-26 17:00:07

标签: terraform terraform-remote-state

在我的Terraform代码中,我经常有多个“ terraform_remote_state”来从不同路径(网络,安全组)获取远程状态。因此,为了避免像这样的多个块:

}

我想我会定义一个这样的模块:

data "terraform_remote_state" "network" {
  backend = "artifactory"

  config {
    username = "${var.remote_state_config["user"]}"
    password = "${var.remote_state_config["password"]}"
    url      = "${var.remote_state_config["url"]}"
    repo     = "${var.remote_state_config["repo"]}"
    subpath  = "the_path"
  }
}

data "terraform_remote_state" "security_group" {
  ...
}

以后要像这样使用它:

variable remote_state_config {
  type = "map"
}

data "terraform_remote_state" "network" {
  backend = "artifactory"

  config {
    username = "${var.remote_state_config["user"]}"
    password = "${var.remote_state_config["password"]}"
    url      = "${var.remote_state_config["url"]}"
    repo     = "${var.remote_state_config["repo"]}"
    subpath  = "the_path"
  }
}

output "private_subnets" {
  value = "${data.terraform_remote_state.network.private_subnets}"
}

output "vpc_id" {
  value = "${data.terraform_remote_state.network.vpc_id}"

但是运行会导致如下错误消息:

module "remote-network" {
  source              = "../../remote-state-network"
  remote_state_config = "${var.remote_state_conf}"
}
...
  vpc_id = "${module.remote-network.vpc_id}"
...

我正在使用Terraform 0.11.3

1 个答案:

答案 0 :(得分:0)

这是因为您在此处引用的远程状态(data "terraform_remote_state" "network")没有名称为output的{​​{1}}。错误消息中明确指出了BTW。

称为vpc_id的状态文件的相应Terraform代码应具有以下定义:

network