Terraform 状态与外部变化同步

Terraform state sync with external changes

我在 Azure 中使用 Terraform 管理我的基础结构。但是,在某些情况下,当其他服务也在更改基础架构时,状态可能会不同步。

例如,我让 terraform 创建了一个应用程序网关。但我还有一个启用了 AGIC 的 AKS 集群,它在应用程序网关内部动态 updates/changes 规则、侦听器等。因此,如果 terraform 在 AGIC 进行一些更改后重新运行,terraform 不知道并希望重置为它知道的默认配置。

也许这是不可能的,但是有没有一种自动同步两者的方法?每次都必须进入 terraform 配置并手动添加 AGIC 所做的更改是不可行的。在这一点上,是否值得使用 terraform 管理应用程序网关?

如果您真的想使用 Terraform 创建 Application Gateway,您应该对将由 AKS 修改的属性使用生命周期元参数 ignore_changes。这并不完美,但至少他们可以分担责任而不会互相覆盖。

The ignore_changes feature is intended to be used when a resource is created with references to data that may change in the future, but should not affect said resource after its creation. In some rare cases, settings of a remote object are modified by processes outside of Terraform, which Terraform would then attempt to "fix" on the next run. In order to make Terraform share management responsibilities of a single object with a separate process, the ignore_changes meta-argument specifies resource attributes that Terraform should ignore when planning updates to the associated remote object.

例如:

resource "azurerm_application_gateway" "example" {
  ...

  lifecycle {
    ignore_changes = [
      http_listener,
      request_routing_rule,
      backend_http_settings
    ]
  }

  ...
}