【问题标题】:Terraform Azurerm backend writing ok but not readingTerraform Azurerm 后端写入正常但无法读取
【发布时间】:2021-07-22 08:09:59
【问题描述】:

我正在尝试在 Azure 上设置一个简单的 Terraform 后端。我能写,但似乎阅读并没有真正起作用。例如,我尝试添加一个名为test_aazurerm_resource_group,然后添加terraform initterraform apply,它已正确存储在Azure 上的存储桶中。

我修改了我的代码并将我的资源名称更改为 test_b 然后 terraform initterraform apply 和 terraform 销毁了我的 test_a 并添加了我的 test_b 资源。 “Apply complete! Resources: 1 added, 0 changed, 1 destroyed.”。可能是什么问题?我可以看到,每当我运行我的terraform init 命令时,它仍然会生成一个.terraform 文件夹,里面有一个terraform.tfstate

ma​​in.tf

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~>2.0"
    }
    azuread = {
      source  = "hashicorp/azuread"
      version = "~>1.0"
    }
  }
}

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "test_a" {
  name     = "testing-resources"
  location = "West Europe"
}

backend_config.tf

terraform {
  required_version = ">= 0.12.6"
  
  backend "azurerm" {
    subscription_id      = "a095c3dd-xxx"
    resource_group_name  = "terraform"
    storage_account_name = "terraform963"
    container_name       = "tfstate"
    key                  = "terraform.tfstate"
  }
}

terraform.tfstate(存储在 Azure 中)

{
  "version": 4,
  "terraform_version": "0.14.3",
  "serial": 4,
  "lineage": "1fcb0f9a-7c26-xxx",
  "outputs": {},
  "resources": [
    {
      "mode": "managed",
      "type": "azurerm_resource_group",
      "name": "test_b",
      "provider": "provider[\"registry.terraform.io/hashicorp/azurerm\"]",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "id": "/subscriptions/a095c3dd-xxx/resourceGroups/testing-resources",
            "location": "westeurope",
            "name": "testing-resources",
            "tags": null,
            "timeouts": null
          },
          "sensitive_attributes": [],
          "private": "eyJlMmJmYjczxxx="
        }
      ]
    }
  ]
}

【问题讨论】:

    标签: azure terraform terraform-provider-azure


    【解决方案1】:

    Terraform 使用此状态来创建计划并对您的基础架构进行更改。在任何操作之前,Terraform 都会进行刷新以使用真实基础设施更新状态。在这种情况下,您只需更改资源名称并保留现有的资源组名称。 Terraform 将要求将import 现有的基础设施纳入状态。

    警告:Terraform 期望它管理的每个远程对象都会 只绑定一个资源地址,通常由 Terraform 本身已经创建了所有对象。如果您导入现有的 对象导入 Terraform,注意将每个远程对象导入到 只有一个 Terraform 资源地址。

    您将使用命令terraform import azurerm_resource_group.test_b <existingResourceGroupID> 导入状态。导入现有基础架构后,terraform 将尝试根据最新状态添加资源 azurerm_resource_group.test_b

    【讨论】:

    • 感谢您的回答!有什么方法可以在写入之前导入tfstate.tf 文件吗?我的意思是你的解决方案有效,但如果我确实有 100 个资源并且我不知道它们的名字
    • "The current implementation of Terraform import can only import resources into the state. It does not generate configuration. Because of this, prior to running terraform import it is necessary to write manually a resource configuration block for the resource, to which the imported object will be mapped."如果你有100个资源,你必须一个一个地导入每个资源,因为命令目前一次只能导入一个资源。
    • 您可以看看这个工具terraformer,它可以帮助您基于现有基础架构生成 tf/json 和 tfstate 文件。更多信息请参考here
    • 谢谢,现在一切正常;D
    猜你喜欢
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 2017-06-27
    • 2022-06-17
    • 2019-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多