【问题标题】:Terraform backend empty stateTerraform 后端空状态
【发布时间】:2021-07-02 03:46:12
【问题描述】:

我在使用 terraform 时遇到了一种奇怪的行为。我一直在研究基础设施。我有一个后端状态配置为在 azure 的存储帐户中声明我的状态文件。直到昨天一切都很好,今天早上当我尝试更新我的基础设施时,terraform plan 的输出很奇怪,因为它试图将所有资源创建为新的,当我检查我的本地遗嘱时..它是空的。

我尝试了terraform pullterraform refresh,但没有,结果还是一样。我检查了我的远程状态,我仍然声明了所有资源。

所以我选择了计划 b,将我的远程状态复制并粘贴到我的本地项目中,然后再次运行 terraform,但是什么也没有,似乎 terraform 忽略了我本地的 terraform 状态并且不想拉远程状态。

编辑: 这是我的 terraform 后端的结构:

terraform {
  backend "azurerm" {
    resource_group_name  = "<resource-group-name>"
    storage_account_name = "<storage-name>"
    container_name       = "<container-name>"
    key                  = "terraform.tfstate"
  }
}

奇怪的是,我刚刚使用 terraform 为另一个项目创建了 8 个资源,它确实创建了所有内容并更新了我的后端状态,没有任何问题。问题仅在于旧资源。

有什么帮助吗?

【问题讨论】:

    标签: terraform


    【解决方案1】:

    如果您运行terraform workspace show,您是否在默认工作区中? 如果您在本地有 tfstate 但您不在正确的工作区 terraform 将忽略它:https://www.terraform.io/docs/language/state/workspaces.html#using-workspaces

    还能看到你的后端文件结构吗?

    编辑: 我不知道为什么它会忽略您的远程状态,但我认为您的问题是当您运行 terraform refresh 它会忽略您的本地文件,因为您有一个远程配置:

    Usage: terraform refresh [options]

    -state=path - Path to read and write the state file to. Defaults to "terraform.tfstate". Ignored when remote state is used.

    -state-out=path - Path to write updated state file. By default, the -state path will be used. Ignored when remote state is used.

    是否可以看到您的terraform state pull 的输出?

    【讨论】:

    • 您好,Haroun,非常感谢您的回复。是的,我在默认工作区中。我将更新我的帖子并添加 terraform 后端
    • 解决了伴侣。非常感谢您的时间和帮助。我不知道如何,但它开始单独工作。
    猜你喜欢
    • 2019-01-22
    • 2018-12-14
    • 2021-09-10
    • 2017-08-10
    • 2022-12-22
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2022-11-04
    相关资源
    最近更新 更多