简体   繁体   English

Terraform AzureRM 不断修改 API 使用默认端点的代理配置进行管理

[英]Terraform AzureRM Continually Modifying API Management with Proxy Configuration for Default Endpoint

We are terraforming our Azure API Management instance.我们正在改造我们的 Azure API 管理实例。

...
resource "azurerm_api_management" "apim" {
  name                = "the-apim"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

...

  hostname_configuration {
    proxy {
      host_name = "the-apim.azure-api.net"
      negotiate_client_certificate = true
    }
  }
}
...

We need to include the hostname_configuration block so that we can switch negotiate_client_certificate to true for the default endpoint.我们需要包含hostname_configuration块,以便我们可以将默认端点的negotiate_client_certificate切换为 true。

This does the job, however every time Terraform runs it plans to modify the APIM instance by adding the hostname_configuration block again:这完成了工作,但是每次 Terraform 运行时,它都计划通过再次添加hostname_configuration块来修改 APIM 实例:


      + hostname_configuration {

          + proxy {
              + host_name                    = "the-apim.azure-api.net"
              + negotiate_client_certificate = true
            }
        }

Is there a way to prevent this from happening?有没有办法防止这种情况发生? In the portal I can see this value is set to true.在门户中,我可以看到此值设置为 true。

I suggest you try to pair with lifecycle > ignore_changes .我建议您尝试搭配lifecycle > 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. ignore_changes 功能旨在在创建资源时使用对未来可能更改的数据的引用,但不应在创建后影响所述资源。 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.在极少数情况下,远程 object 的设置会被 Terraform 之外的进程修改,然后 Terraform 将尝试在下一次运行时“修复”。 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.为了使 Terraform 与单独的进程共享单个 object 的管理职责,ignore_changes 元参数指定资源属性,Terraform 在计划更新关联的远程 object 时应忽略这些资源属性。

In your case, the hostname_configuration is considered a "nested block" or "attribute as block" in Terraform. So the usage of ignore_changes is not so straightforward (you can't just add the property name, as you would do if you wanted to ignore changes in your resource_group_name for example, which is directly a property).在您的情况下, hostname_configuration在 Terraform 中被视为“嵌套块”或“属性作为块”。因此ignore_changes的使用不是那么简单(您不能只添加属性名称,如果您想这样做的话例如,忽略resource_group_name中的更改,它直接是一个属性)。 From an issue in GitHub back from 2018 , it seems you could use the TypeSet hash of the nested block to add to an ignore sections.2018 年 GitHub 中的一个问题来看,您似乎可以使用嵌套块的 TypeSet hash 添加到忽略部分。

Even though I can't test this, my suggestion for you:尽管我无法对此进行测试,但我对您的建议是:

  1. deploy your azurerm_api_management resource normally with the hostname_configuration block使用hostname_configuration块正常部署azurerm_api_management资源
  2. check the state file from your resource and get the typeset hash of the hostname_configuration part;从您的资源中检查 state 文件并获取hostname_configuration部分的排版 hash; should be similar to hostname_configuration.XXXXXX应该类似于hostname_configuration.XXXXXX
  3. add an ignore_changes section passing the above添加一个传递上述内容的ignore_changes部分
resource "azurerm_api_management" "apim" {
  # ...

  lifecycle {
    ignore_changes = [
      "hostname_configuration.XXXXXX",
    ]
  }
}

Sometimes such issues occur due to issues in the provider.有时此类问题是由于提供程序中的问题而发生的。 Probably it is not storing the configuration in the state file or not retrieving the stored state for this block.可能它没有将配置存储在 state 文件中,或者没有为该块检索存储的 state。 Try upgrading the provider to the latest available provider and see if it sorts the issue.尝试将提供程序升级到最新的可用提供程序,看看它是否能解决问题。

If that does not solve it, you can try defining this configuration as a separate resource.如果这不能解决问题,您可以尝试将此配置定义为单独的资源。 As per the terraform documentation: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/api_management根据 terraform 文档: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/api_management

It's possible to define Custom Domains both within the azurerm_api_management resource via the hostname_configurations block and by using the azurerm_api_management_custom_domain resource.可以通过 hostname_configurations 块和使用 azurerm_api_management_custom_domain 资源在 azurerm_api_management 资源中定义自定义域。 However it's not possible to use both methods to manage Custom Domains within an API Management Service, since there'll be conflicts.但是,不可能在 API 管理服务中使用这两种方法来管理自定义域,因为会发生冲突。

So Please try removing that hostname_configuration block and add it as separate resource as per this documentation: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/api_management_custom_domain因此,请尝试删除该 hostname_configuration 块,并根据此文档将其添加为单独的资源: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/api_management_custom_domain

This will most likely fix the issue.这很可能会解决问题。

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

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