【问题标题】:Terraform : depends_on argument not creating the specified resource firstTerraform:depends_on 参数不首先创建指定的资源
【发布时间】:2021-04-17 01:34:29
【问题描述】:

我想将 terraform 状态文件推送到 github 存储库。 Terraform 中的文件功能无法读取 .tfstate 文件,因此我需要先将其扩展名更改为 .txt。现在为了自动化它,我创建了一个空资源,它有一个配置程序来运行命令以将 tfstate 文件复制为同一目录中的 txt 文件。我遇到了这个“depends_on”参数,它可以让你指定在运行当前资源之前是否需要先制作特定资源。但是,它不起作用,当文件功能需要它时,我立即收到“terraform.txt”文件不退出的错误。

provider "github" {
  token = "TOKEN"
  owner = "USERNAME"
}

resource "null_resource" "tfstate_to_txt" {
  provisioner "local-exec" {
    command = "copy terraform.tfstate terraform.txt"
  }
}

resource "github_repository_file" "state_push" {
  repository = "TerraformStates"
  file       = "terraform.tfstate"
  content    = file("terraform.txt")

  depends_on = [null_resource.tfstate_to_txt]
}

【问题讨论】:

  • 将状态文件添加到 git repo(希望不是公开的)不是一个好习惯。你能澄清你想达到什么目标吗?也许有更好的方法?
  • @Marcin 是的,这是一个私人仓库。我只想在将某些内容推送到 git 之前在 terraform 中运行一个脚本。它可以是任何文件,不一定是 tfstate。

标签: terraform


【解决方案1】:

The documentation for the file function 解释了这种行为:

此功能只能用于在 Terraform 运行开始时磁盘上已存在的文件。函数不参与依赖关系图,因此此函数不能用于在 Terraform 操作期间动态生成的文件。我们不建议在 Terraform 配置中使用动态本地文件,但在极少数情况下,您可以使用 the local_file data source 读取文件,同时尊重资源依赖关系。

本段还包括关于如何获得所需结果的建议:使用来自the hashicorp/local providerlocal_file 数据源,将文件作为资源操作(在应用阶段)而不是作为配置加载:

resource "null_resource" "tfstate_to_txt" {
  triggers = {
    source_file = "terraform.tfstate"
    dest_file   = "terraform.txt"
  }

  provisioner "local-exec" {
    command = "copy ${self.triggers.source_file} ${self.triggers.dest_file}"
  }
}

data "local_file" "state" {
  filename = null_resource.tfstate_to_txt.triggers.dest_file
}

resource "github_repository_file" "state_push" {
  repository = "TerraformStates"
  file       = "terraform.tfstate"
  content    = data.local_file.state.content
}

请注意,虽然上面应该得到您询问的操作顺序,但在 Terraform 运行时读取 terraform.tfstate 文件是一件非常不寻常的事情,并且很可能导致未定义的行为,因为 Terraform 可以重复更新该文件在整个terraform apply 的不可预知时刻。

如果您的意图是让 Terraform 将状态保存在远程系统而不是本地磁盘上,通常的实现方式是配置 remote state,这将导致 Terraform 仅保持状态 远程,根本不使用本地的terraform.tfstate 文件。

【讨论】:

    【解决方案2】:

    depends_on 并不能真正与 null_resource.provisioner 一起使用。

    这里有一个可以帮助您的解决方法:

       resource "null_resource" "tfstate_to_txt" {
            provisioner "local-exec" {
            command = "copy terraform.tfstate terraform.txt"
             }
           }
            
       resource "null_resource" "delay" {
            provisioner "local-exec" {
                command = "sleep 20"
             }
            triggers = {
                "before" = null_resource.tfstate_to_txt.id
             }
            }
      resource "github_repository_file" "state_push" {
            repository = "TerraformStates"
            file       = "terraform.tfstate"
            content    = file("terraform.txt")
            depends_on = ["null_resource.delay"]
           }       
    
    
       
    

    如果复制命令需要更多时间,延迟空资源将确保资源 2 在第一个之后运行,只需将睡眠更改为更高的数字

    【讨论】:

    • 谢谢。谢谢为我工作,但一直更新睡眠直到 60
    猜你喜欢
    • 1970-01-01
    • 2021-11-04
    • 2014-09-30
    • 2013-02-16
    • 2018-09-18
    • 2015-08-26
    • 2013-01-31
    • 2022-09-22
    • 1970-01-01
    相关资源
    最近更新 更多