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"不是模块"实例"

的有效输出

将模块视为无法"触及"的黑匣子。为了使数据从模块中获取,该模块需要用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}"
    }
}

希望为您修复它。

最新更新