gpt4 book ai didi

地形 : depends_on argument not creating the specified resource first

转载 作者:行者123 更新时间:2023-12-04 16:25:20 24 4
gpt4 key购买 nike

我想将 terraform 状态文件推送到 github 存储库。 Terraform 中的 file 函数无法读取 .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]
}

最佳答案

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

This function can be used only with files that already exist on disk at the beginning of a Terraform run. Functions do not participate in the dependency graph, so this function cannot be used with files that are generated dynamically during a Terraform operation. We do not recommend using dynamic local files in Terraform configurations, but in rare situations where this is necessary you can use the local_file data source to read files while respecting resource dependencies.


本段还包括有关如何获得所需结果的建议:使用 local_file数据源,来自 the hashicorp/local provider , 将文件作为资源操作(在应用阶段)而不是作为配置加载的一部分读取:
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.tfstate在 Terraform 运行时创建文件是一件非常不寻常的事情,并且很可能导致未定义的行为,因为 Terraform 可以在整个 terraform apply 中在不可预测的时刻重复更新该文件。 .
如果您的目的是让 Terraform 将状态保存在远程系统而不是本地磁盘上,通常的实现方法是配置 remote state ,这将导致 Terraform 仅远程保持状态,而不使用本地 terraform.tfstate文件。

关于地形 : depends_on argument not creating the specified resource first,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65678208/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com