Terraform模块内的执行顺序

时间:2018-05-30 10:04:26

标签: terraform consul

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

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

任何帮助都将受到高度赞赏。

谢谢!

1 个答案:

答案 0 :(得分:1)

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

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

  1. 使用其他模块中consul_keys导出的值(数据中心或var.name>)
  2. 将依赖consul_keys的资源转储到同一个文件中。
  3. 可悲的是,目前还没有很好的解决方案,但正在处理模块依赖性。

    修改 作为在同一文件中转储所有资源的示例:

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

    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 = ["resource.my_keys"]
    }
    
    resource "aws_instance" "other_res2" {
        depends_on = ["resource.my_keys"]
    }