Terraform 模块内的执行顺序

Ved*_*ore 4 consul terraform

我对 terraform 脚本中模块的执行顺序有疑问。我已经提出了源代码库的问题。https://github.com/hashicorp/terraform/issues/18143

任何人都可以在这里或 GitHub 上帮我解决这个问题吗?

任何帮助将不胜感激。

谢谢!

小智 7

执行不会等待“vpc”模块完成,而仅等待值“module.vpc.vpc_id”的可用性。为此,执行 aws_vpc 资源就足够了。因此,您实际上并没有告诉 TerraForm 也等待 consul_keys 资源。

要解决此问题,您必须将 consul_keys 资源的依赖项添加到其他模块。这可以通过以下方式实现:

  1. 在其他模块(datacenter 或 var.name>)中使用 consul_keys 导出的值
  2. 将依赖 consul_keys 的资源转储到同一文件中。

遗憾的是,目前还没有很好的解决方案,但模块依赖关系正在研究中。

编辑: 作为将所有资源转储到同一文件中的示例:

这不起作用,因为没有模块依赖项:

module "vpc" {
    ...
}

module "other" {
 depends_on=["module.vpc"]
}
Run Code Online (Sandbox Code Playgroud)

vpc模块文件:

resource "aws_instance" "vpc_res1" {
    ...
}

resource "consul_keys" "my_keys" {
    ...
}
Run Code Online (Sandbox Code Playgroud)

其他模块文件:

resource "aws_instance" "other_res1" {
    ...
}

resource "aws_instance" "other_res2" {
    ...
}
Run Code Online (Sandbox Code Playgroud)

将所有内容放在同一个文件中是可行的。您还可以将“vpc_res1”资源保留在单独的模块中:

resource "consul_keys" "my_keys" {
    ...
}

resource "aws_instance" "other_res1" {
    depends_on = ["consul_keys.my_keys"]
}

resource "aws_instance" "other_res2" {
    depends_on = ["consul_keys.my_keys"]
}
Run Code Online (Sandbox Code Playgroud)