在我的 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}"
...
但是运行会导致如下错误消息:
module.remote-network.output.vpc_id: Resource 'data.terraform_remote_state.network' does not have attribute 'vpc_id' for variable 'data.terraform_remote_state.network.vpc_id'
我正在使用 terraform 0.11.3