Terraform 地形模块结构

Terraform 地形模块结构,terraform,Terraform,我有一个所有.tf文件的平面结构,并希望迁移到基于文件夹(即模块)的设置中,以便代码更清晰 例如,我已将实例和弹性IP(eip)定义移动到单独的文件夹中 /terraform ../instance ../instance.tf ../eip ../eip.tf 在我的实例中.tf: resource "aws_instance" "rancher-node-production" {} module "instance" { source = "../instance

我有一个所有
.tf
文件的平面结构,并希望迁移到基于文件夹(即
模块
)的设置中,以便代码更清晰

例如,我已将实例和弹性IP(eip)定义移动到单独的文件夹中

/terraform
 ../instance
   ../instance.tf
 ../eip
    ../eip.tf
在我的
实例中.tf

resource "aws_instance" "rancher-node-production" {}
module "instance" {
  source = "../instance"
}


resource "aws_eip" "rancher-node-production-eip" {
  instance = "${module.instance.rancher-node-production.id}"
在my
eip.tf
中:

resource "aws_instance" "rancher-node-production" {}
module "instance" {
  source = "../instance"
}


resource "aws_eip" "rancher-node-production-eip" {
  instance = "${module.instance.rancher-node-production.id}"
但是在运行
地形平面图时

错误:资源“aws_eip.rancher node production eip”配置:“rancher node production.id”不是模块“instance”的有效输出


将模块视为无法“触及”的黑匣子。要从模块中获取数据,该模块需要使用
输出导出该数据。因此,在本例中,您需要将
rancher节点生产
id声明为
实例
模块的输出

如果您查看得到的错误,这正是它所说的:
rancher node production.id
不是模块的有效输出(因为您从未将其定义为输出)

不管怎样,这就是它的样子

# instance.tf
resource "aws_instance" "rancher-node-production" {}

output "rancher-node-production" {
    value = {
        id = "${aws_instance.rancher-node-production.id}"
    }
}
希望能帮你解决这个问题