[英]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_zones
被azurerm_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_subnet
、 azurerm_kubernetes_cluster
、 azurerm_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_app
和azurerm_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 注册表以获取特定提供程序版本的特定资源属性的更新,并相应地进行操作。
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:
例如:
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
可以显示更改,尽管他不应该这样做。
force replacement
attribute accurately and understand why terraform detects changes.force replacement
属性,了解 terraform 检测到变化的原因。 It's seems be obvious but it doesn't have to.state
file and run terraform plan
again.state
文件中的相关属性中,然后再次运行terraform plan
。 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 .尝试找出资源更改或忽略此时发生在具有 资源定位的另一个模块中的错误。
---> !! ---> !! 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
。
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 。
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 配置模块中应用受影响资源的更改,然后才能按照迁移指南中的说明导入它们。
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.必须使用新的提供程序重新导入它们。
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.