Terraform模块结构

时间:2018-04-26 10:17:55

标签: terraform

我的所有.tf文件都是扁平结构,并希望迁移到基于文件夹(即module)的设置,以便我的代码更清晰。

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

/terraform
 ../instance
   ../instance.tf
 ../eip
    ../eip.tf

在我的instance.tf

resource "aws_instance" "rancher-node-production" {}

在我的eip.tf

module "instance" {
  source = "../instance"
}


resource "aws_eip" "rancher-node-production-eip" {
  instance = "${module.instance.rancher-node-production.id}"

但是在运行terraform plan

  

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

1 个答案:

答案 0 :(得分:0)

将模块视为无法“触及”的黑盒子。要从模块中获取数据,该模块需要使用output导出该数据。因此,在您的情况下,您需要将rancher-node-production id声明为instance模块的输出。

如果你看一下你得到的错误,那正是它所说的:rancher-node-production.id不是模块的有效输出(因为你从未将它定义为输出)。

无论如何,这就是它的样子。

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

output "rancher-node-production" {
    value = {
        id = "${aws_instance.rancher-node-production.id}"
    }
}

希望能为你修复它。

相关问题