首页 > 解决方案 > Terraform 模块内的执行顺序

问题描述

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

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

任何帮助将不胜感激。

谢谢!

标签: terraformconsul

解决方案


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

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

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

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

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

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

module "vpc" {
    ...
}

module "other" {
 depends_on=["module.vpc"]
}

vpc 模块文件:

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

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

其他模块文件:

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

resource "aws_instance" "other_res2" {
    ...
}

将所有内容放在同一个文件中是可行的。您还可以将“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"]
}

推荐阅读