简体   繁体   English

terraform提供者升级后如何处理资源变化?

[英]How to handle resource changes after provider upgrade in terraform?

I am trying to upgrade the azurerm terraform provider from 2.30.0 to 3.13.0 .我正在尝试将azurerm terraform 提供程序从2.30.0升级到3.13.0 For sure there are several changes in some resources (eg resoruce name changes, renamed attributes, removed attributes, etc.).肯定有一些资源的一些变化(例如资源名称变化,重命名的属性,删除的属性等)。 I checked the Azure Resource Manager Upgrade Guide and found those changes by which our configuration is affected.我检查了Azure 资源管理器升级指南,发现了影响我们配置的那些更改。

For example in version 3.0.0 the attribute availibility_zones is replaced by zones for the azurerm_kubernetes_cluster_node_pool ressource.例如,在版本3.0.0中,属性availibility_zonesazurerm_kubernetes_cluster_node_pool资源的zones替换。 Therefore when i run terraform plan i get an error, that the attribute availibility_zones doesn't exists.因此,当我运行terraform plan时,我收到一个错误,即属性availibility_zones不存在。

I found a migration guide from deprecated resources .从 deprecated resources 中找到了迁移指南 I understood the idea of removing the resource from the state and importing it again by it's resource id, but there are also other resources like azurerm_subnet , azurerm_kubernetes_cluster , azurerm_storage_account they have resource changes, why the terraform import -var-file='./my.tfvars' [..] command fails.我理解从 state 中删除资源并通过其资源 id 再次导入它的想法,但还有其他资源,如azurerm_subnetazurerm_kubernetes_clusterazurerm_storage_account他们有资源更改,为什么terraform import -var-file='./my.tfvars' [..]命令失败。

I am not sure if it fails (only) because of the dependencies to some variables they are needed for declaring the resource properly.我不确定它是否(仅)失败,因为它们依赖于正确声明资源所需的某些变量。 Or would it also fail because of reading the .tfvars and terraform compares the read variables with the state?或者它也会因为读取.tfvars而失败,并且 terraform 将读取的变量与 state 进行比较?

Actually i need a "best practice" guide how to handle resource changes after a provider update.实际上,我需要一个“最佳实践”指南如何在提供程序更新后处理资源更改。 I dont know where to start and where to end.我不知道从哪里开始,从哪里结束。 I tried to visualize the dependencies with terraform graph and created a svg to try to figure out the order by which i have to migrate the resource changes.我尝试使用terraform graph来可视化依赖关系,并创建了一个svg来尝试找出我必须迁移资源更改的顺序。 It's unpossible to understand the relations of the whole configuration.. I could also just remove attributes from the state file they doesnt exists anymore, or rename attributes manually.无法理解整个配置的关系。我也可以从不再存在的 state 文件中删除属性,或者手动重命名属性。

So How to handle resource changes after provider upgrade in terraform?那么如何处理 terraform 中提供程序升级后的资源变化?

So How to handle resource changes after provider upgrade in terraform?那么 terraform 中提供者升级后如何处理资源变化?

I don't think deleting the state file and then importing the resource and do changes in resources attribute based on when you require to upgrade the azurerm version is a feasible solution.我不认为删除state文件然后导入资源并根据您需要升级 azurerm 版本的时间来更改资源属性是一个可行的解决方案。

Terraform Registry already given update/notes for every resource when they did some changes on their upgrading version.当 Terraform Registry 对升级版本进行一些更改时,他们已经为每个资源提供了更新/注释。 Just like below example就像下面的例子

we use azurerm_app_service for version ~2.x but for version ~3.0 and ~4.0 azurerm_linux_web_app and azurerm_windows_web_app resources instead.我们将azurerm_app_service用于 ~2.x 版,但用于 ~3.0 和 ~4.0 版azurerm_linux_web_appazurerm_windows_web_app资源。

在此处输入图像描述

Would suggest you check the terraform registry for update on particular resources attribute for specific provider version or not and do it accordingly.建议您检查 terraform 注册表以获取特定提供程序版本的特定资源属性的更新,并相应地进行操作。

General一般的

I was able to update the provider properly - i hope at least.我能够正确更新提供程序-至少我希望如此。 I would like to share my experience, maybe it would help other beginners.我想分享我的经验,也许它会帮助其他初学者。 This is not a professional guide, but just my experience that i want to share.这不是专业指南,只是我想分享的经验。

First of all you have to remove ALL resources affected by the provider upgrade and then re-import them.首先,您必须删除受提供商升级影响的所有资源,然后重新导入它们。 What does that mean?这意味着什么?

The new provider will contain divers changes on different resources.新的提供者将包含对不同资源的不同更改。 For example:例如:

  • Removed deprecated attributes (attribute is completely removed)删除了不推荐使用的属性(属性被完全删除)
  • Superseded attribute (attribute is replaced by another).被取代的属性(属性被另一个替换)。
  • Renamed attributes重命名属性
  • Superseded resources (here the resource can be deprecated or removed by the upgraded version)被取代的资源(这里的资源可以被升级版本弃用或删除)

Note笔记

The migration guide describes how you can migrate from deprecated resources, but the workflow for attribute changes is the same. 迁移指南描述了如何从已弃用的资源进行迁移,但属性更改的工作流程是相同的。 How i understood it.我是怎么理解的。 This is the only guide that i found.这是我找到的唯一指南。

terraform plan will show you one or several errors for affected resources. terraform plan将向您显示受影响资源的一个或多个错误。

If your terraform configuration is complex and huge, then you shouldn't try to remove and re-import them all at once.如果您的 terraform 配置复杂且庞大,那么您不应该尝试一次性删除并重新导入它们。 Just go step by step and fix one affected resource successively.只需 go 一步一步修复一个受影响的资源。

terraform plan can show changes although he shouldn't. terraform plan可以显示更改,尽管他不应该这样做。

  • Check the force replacement attribute accurately and understand why terraform detects changes.准确检查force replacement属性,了解 terraform 检测到变化的原因。 It's seems be obvious but it doesn't have to.这似乎很明显,但并非必须如此。
  • There can be a type change eg int -> string可以有类型更改,例如 int -> string
  • If the affected change is a kind of missing secret, then you can try to add the secret manually as the value to the related attribute in the state file and run terraform plan again.如果受影响的更改是一种丢失的秘密,那么您可以尝试手动将秘密作为值添加到state文件中的相关属性中,然后再次运行terraform plan
  • Or there can be also a bug by the provider.或者提供者也可能存在错误。 So if you can't understand the detected change try to search the issues of the provider - mostly on github.因此,如果您无法理解检测到的更改,请尝试搜索提供商的问题 - 主要在 github 上。 Don't get confused if you can't find any related issue, maybe you have found a bug.如果您找不到任何相关问题,请不要感到困惑,也许您发现了一个错误。 Then just create a new issue.然后只需创建一个新问题。

You will also face some other errors or bugs related to terraform itself.您还将面临与 terraform 本身相关的一些其他错误或错误。 You have to search for a workaround patiently, so that you can continue apply the resource changes.您必须耐心地寻找解决方法,以便您可以继续应用资源更改。

Try to figure out resource changes or to ignore an error for this moment that occurs in another module with resource targeting .尝试找出资源更改或忽略此时发生在具有 资源定位的另一个模块中的错误。

How To如何

  1. ---> !! ---> !! BACKUP YOUR STATE FILE !!备份您的 STATE 文件! <--- : You have to backup your state file before you start manipulating the state file. <--- : 在开始操作 state 文件之前,您必须备份state文件。 You will be able to restore the state of the backed state file if something goes wrong.如果出现问题,您将能够恢复支持的 state 文件的 state。 Also you can use the backed up state file for finding needed id s when you have to import the resource.当您必须导入资源时,您还可以使用备份的state文件来查找所需的id

  2. Get Affected Resource: How you can find all affected resources?获取受影响的资源:如何找到所有受影响的资源? After the upgrade the provider will not be able to parse the state file, if a resource contains changes - like i described in the question above.升级后,如果资源包含更改,提供商将无法解析state文件 - 就像我在上面的问题中描述的那样。 You will get an error for affected resources.您将收到受影响资源的错误消息。 Then you can check the changes for this affected resource in the upgrade guide of the provider - can be found in the provider register eg azurerm .然后,您可以在提供程序的升级指南中检查此受影响资源的更改 - 可以在提供程序注册中找到,例如azurerm

  3. Terraform Configuration: Now you have to apply the changes for the affected resources in the terraform configuration modules before you can import them like described in the migration guide . Terraform 配置:现在您必须在 terraform 配置模块中应用受影响资源的更改,然后才能按照迁移指南中的说明导入它们。

  4. Remove Outdated Resource : Like described in the the migration guide you have to remove the outdated resource from the state file because it contains the old format of the resource.删除过时的资源:如迁移指南中所述,您必须从state文件中删除过时的资源,因为它包含资源的旧格式。 The new provider is not able to handle these resources from the state file.新提供程序无法处理state文件中的这些资源。 They must be re-imported with the new provider.必须使用新的提供程序重新导入它们。

  5. Import Removed Resource : After you removed the resource you have to re-import it also described in the migration guide .导入已删除的资源:删除资源后,您必须重新导入它,迁移指南中也对此进行了描述。 Check the terraform import documentation for better understanding and usage.查看terraform 导入文档以更好地理解和使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 AzureRM 提供程序在 Terraform 中指定资源提供程序版本 - How to specify resource provider version in the Terraform using AzureRM provider 运行 terraform init 后无法看到 Terraform 提供程序文件 - Unable to see the Terraform provider file after running terraform init Terraform 封装提供程序无法创建自定义证书资源 - Terraform Incapsula provider fails to create custom certificate resource 如何在 terraform 上使用 azurerm 提供程序创建 appRoles - How to create appRoles with azurerm provider on terraform 如何使用 terraform 在 azure 中管理现有资源组 - How to manage existing resource group in azure with terraform 如何从另一个 Terraform 配置更新资源 - How to update resource from another Terraform configuration 如何将新资源添加到 Terraform 中的现有资源组 - How to add a new resource to an existing resource group in Terraform 如何使用 Terraform for_each 资源块和计数资源块 - How to use Terraform for_each resource block and count resource block 如何使用 terraform 资源提供程序创建 azure 机器学习资源? - How to create azure machine learning resource using terraform resource providers? 将 Terraform 的 Azure SQL 资源从“azurerm_sql_database”升级到“azurerm_mssql_database” - Upgrade Terraform's Azure SQL Resource from `azurerm_sql_database` to `azurerm_mssql_database`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM