简体   繁体   English

Terraform - 新的 Azure 资源组的“对象已在 Terraform 之外更改”

[英]Terraform - 'Objects have changed outside of Terraform' for a new Azure resource group

I have been experimenting with terraform with the following basic configuration file for creating a resource group...我一直在尝试使用 terraform 使用以下基本配置文件来创建资源组...

resource "azurerm_resource_group" "myrg" {
  name = "MyResourceGroup"
  location = "westeurope"
}

output resource_group_details {
  value = azurerm_resource_group.myrg
}
  • First terraform plan - 1 resource will be created第一个terraform plan - 将创建 1 个资源
  • First terraform apply - 1 resource created首先terraform apply - 创建了 1 个资源
  • Second terraform plan (with no changes made to the configuration file) - Objects have changed outside of Terraform (See below)第二个terraform plan (未对配置文件进行更改) - Objects have changed outside of Terraform (见下文)
  • Second terraform apply - Objects have changed outside of Terraform , 0 added/changed/detroyed第二个terraform apply - Objects have changed outside of Terraform ,添加/更改/销毁 0
  • Third terraform plan (with no changes made to the configuration file) - No changes. Your infrastructure matches the configuration.第三个terraform plan (未对配置文件进行更改)- 未No changes. Your infrastructure matches the configuration. No changes. Your infrastructure matches the configuration.
Note: Objects have changed outside of Terraform

Terraform detected the following changes made outside of Terraform since the last "terraform apply" which may have
affected this plan:

  # azurerm_resource_group.myrg has changed
  ~ resource "azurerm_resource_group" "myrg" {
        id       = "/subscriptions/176f2ee3-d0a2-476d-9106-43cad1f63f16/resourceGroups/MyResourceGroup"
        name     = "MyResourceGroup"
      + tags     = {}
        # (1 unchanged attribute hidden)
    }

Based on what I've tried to find about this online, it looks like this warning is because Azure adds an empty tag array to a resource group during creation.根据我试图在网上找到的内容,看起来这个警告是因为 Azure 在创建过程中向资源组添加了一个空标签数组。 Then when terraform compares the now existing resource with the configuration file and state, it's now warning you there is a difference.然后当 terraform 将现有资源与配置文件和 state 进行比较时,它现在警告您存在差异。 I'm not quite sure how terraform reconciles this on the third terraform plan though....我不太确定 terraform 如何在第三个terraform plan中协调这一点......

What should be the workflow here?这里的工作流程应该是什么? Particularly when thinking about CI?特别是在考虑 CI 时?

It appears to just be noise to be informed of the existence of an empty, optional attribute that I haven't defined.被告知存在我尚未定义的空的可选属性似乎只是噪音。

I've looked at -refresh=false but it looks like this could suppress a genuine change that has occurred on your infrastructure that you may want to be notified about.我查看了-refresh=false ,但看起来这可能会抑制您的基础架构上发生的真正更改,您可能希望收到通知。 When using -refresh-only on the second terraform plan and apply it just outputs the same noise as above.当在第二个terraform plan上使用-refresh-onlyapply它时,它只会输出与上述相同的噪声。

Indeed, what you have observed here is a small bug in the Azure provider where it is being inconsistent between the object it returns during apply and the object it returns during refresh.实际上,您在此处观察到的是 Azure 提供程序中的一个小错误,它在应用期间返回的 object 与刷新期间返回的 object 之间不一致。

It is typically okay for a provider to insert a default value for an argument that wasn't set, which is what seems to be happening here, but a provider ought to be consistent in doing so in all of its results: in the initial plan, in the new state created after apply, and in the refreshed state created on the next plan.提供者通常可以为未设置的参数插入默认值,这似乎是这里发生的事情,但提供者应该在其所有结果中保持一致:在初始计划中,在新的state创建后申请,并在刷新的state上创建下图。 Terraform produces this message in particular if the refreshed state created on the next plan is different than the new state that was created by the previous apply. Terraform 会产生此消息,特别是如果在下一个计划中创建的刷新的 state 与上一个应用创建的新 state 不同。


One way to hide the bug would be to explicitly set the tags argument to the default value that the provider's refresh step is inserting:隐藏错误的一种方法是将tags参数显式设置为提供程序的刷新步骤插入的默认值:

resource "azurerm_resource_group" "myrg" {
  name     = "MyResourceGroup"
  location = "westeurope"

  tags = {}
}

As long as the provider logic doesn't treat an empty map as special during plan and apply (which I'm not sure about), this should hopefully cause the initial result to agree with the refreshed result and thus avoid showing an incorrect "changed outside of Terraform" note.只要提供程序逻辑在计划和应用期间不将空 map 视为特殊(我不确定),这应该有望导致初始结果与刷新结果一致,从而避免显示不正确的“更改Terraform 之外”的注释。


Another separate answer is to change your output value to return more specific attributes of the resource group, so that Terraform can see that the end result doesn't depend on the tags attribute.另一个单独的答案是更改您的 output 值以返回资源组的更具体的属性,以便 Terraform 可以看到最终结果不依赖于tags属性。

Terraform shows this message in an attempt to explain why the resource_group_details output value is also planned to change, and so Terraform shouldn't show the message if it can be sure that your output value won't be affected by the change to the tags. Terraform shows this message in an attempt to explain why the resource_group_details output value is also planned to change, and so Terraform shouldn't show the message if it can be sure that your output value won't be affected by the change to the tags. For example:例如:

output "resource_group_details" {
  value = {
    name     = azurerm_resource_group.myrg.name
    location = azurerm_resource_group.myrg.location
    # (...and any other attributes you want to export,
    # as long as you don't refer to "tags".)
  }
}

Note that this rule generally applies to anything in your configuration that may directly or indirectly refer to the tags attribute, so if there's more to your configuration that you didn't show here then you should also make sure that nothing else refers to tags .请注意,此规则通常适用于您的配置中可能直接或间接引用tags属性的任何内容,因此,如果您的配置中有更多内容未在此处显示,那么您还应确保没有其他内容引用tags

Terraform's analysis of this is not fully precise, so if it isn't sure that there aren't any references to tags then it will still show the note just in case . Terraform 对此的分析并不完全准确,因此如果不确定没有任何对tags的引用,它仍会显示注释以防万一 Therefore you would need to stick to relatively simple expressions that refer directly to individual attributes of azurerm_resource_group.myrg , and avoid using expressions which do dynamic work with the entire resource object which would prevent Terraform's analysis from proving that the tags attribute is unused.因此,您需要坚持使用直接引用azurerm_resource_group.myrg的各个属性的相对简单的表达式,并避免使用对整个资源 object 进行动态工作的表达式,这将阻止 Terraform 的分析证明tags属性未使用。

Upgrade to terraform v1.2 and this informational output should disappear.升级到 terraform v1.2,此信息 output 应该会消失。

This 'Note: Objects have changed outside of Terraform' feature was introduced a little before Terraform v1.0, and was supposed to help understand some of the changes suggested in the plan that may come from changes outside of Terraform.在 Terraform v1.0 之前引入了这个“注意:对象已在 Terraform 之外更改”功能,旨在帮助理解计划中建议的一些更改,这些更改可能来自 Terraform 之外的更改。

But in real life, this feature makes a lot of noises and confusions, and most of the time it can be ignored entirely.但在现实生活中,这个功能会产生很多噪音和混乱,而且大多数时候可以完全忽略它。 In Terraform v1.2 it is now hidden, unless it really is linked to a change suggested in the plan.在 Terraform v1.2 中,它现在被隐藏了,除非它确实与计划中建议的更改相关联。

https://support.hashicorp.com/hc/en-us/articles/4405950960147-New-Feature-Objects-have-changed-outside-of-Terraform- https://support.hashicorp.com/hc/en-us/articles/4405950960147-New-Feature-Objects-have-changed-outside-of-Terraform-

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM