【问题标题】:Terraform remote state issue for Azureblob in 0.12.x0.12.x 中 Azureblob 的 Terraform 远程状态问题
【发布时间】:2020-04-30 20:58:18
【问题描述】:

我正在使用 Azure 提供程序并将 terraform 状态存储在 Azure blob 存储中。为此使用下面的代码 sn-p。

data "terraform_remote_state" "xxxxxx" {
  backend = "azurerm"

  config = {
    container_name       = "terraform-state"
    resource_group_name = "${var.remote_state_resource_group}"
    storage_account_name = "${var.remote_state_storage_account}"
    access_key           = "${var.remote_state_credentials}"
    key                  = "${var.cluster_name}-k8s-worker"
  }

  defaults = {}
}

如果我使用最新版本的 terraform 版本 0.12.x 运行上述代码,则会失败并出现以下错误。但是使用 0.11.x 运行相同的代码,它按预期工作。

Error message:

    Error: Unable to find remote state

  on example2.tf line 20, in data "terraform_remote_state" "xxxxxx":
  20: data "terraform_remote_state" "xxxxxx" {

  No stored state was found for the given workspace in the given backend.

任何人在使用 Azure blob 存储的 terraform 0.12.x 中都面临过类似的问题。

【问题讨论】:

  • 您需要提供有关您使用的 terraform 代码的更多信息。
  • 您的问题有什么更新吗?我的回答能帮你解决吗?如果是,请采纳。
  • 什么原因不回复也不接受?

标签: azure terraform azure-blob-storage terraform-provider-azure


【解决方案1】:

我认为可能的原因在这里:

  1. 使用错误的存储帐户
  2. 使用错误的容器名称
  3. 使用了错误的密钥

以上所有原因都会导致您遇到的错误。并且远程状态在 terraform 版本 0.12.x 中运行良好。

【讨论】:

  • 您好 Chrles,我正在使用相同的存储帐户、容器名称和密钥。它适用于 terraform 0.11.x 版本,不适用于 0.12.x 版本。我使用的是 terraform 0.12.12 版本,提供程序代码如下。```provider "azurerm" { version = "~> 1.35" }
  • @DeekshitKumar 能否显示更多关于您用于存储状态的存储的消息,例如存储名称、容器名称和状态文件名。此外,关于您在远程状态下使用的所有变量的 Terraform 代码。
  • @DeekshitKumar 这个问题有什么更新吗?如果您不积极回复,我认为您无法从 SO 那里得到解决方案。
  • 抱歉延迟回复。
  • @DeekshitKumar 那么现在情况如何?你用我的回答解决问题了吗?如果它适合你,请接受它。
【解决方案2】:

当我有一个将状态存储在 azurerm 中的 terraform 配置,然后我想在另一个 terraform 配置中将该状态用作远程 azurerm 数据源时,我遇到了这个问题。

特别是当第一个配置使用 terraform 工作空间时会出现此问题。 azurerm 后端默默地在 blob 键的末尾附加 env:${terraform.workspace} 形式的后缀。您必须在数据源中明确纠正这一点。

如果第一个配置的后端是这样的:

terraform {
    backend "azurerm" {
        resource_group_name  = "rg-myapp"
        storage_account_name = "myappterraform"
        container_name       = "tfstate"
        key                  = "myapp.tfstate"
    }
}

第二个配置的数据源必须是这样的:

data "terraform_remote_state" "myapp" {
    backend = "azurerm"
    config = {
        resource_group_name  = "rg-myapp"
        storage_account_name = "myappterraform"
        container_name       = "tfstate"
        key                  = "myapp.tfstateenv:${terraform.workspace}"
    }
}

【讨论】:

    猜你喜欢
    • 2016-11-24
    • 2016-12-22
    • 2021-12-11
    • 2019-07-28
    • 2021-10-23
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多