Azure apim-使用```API操作''API操作后已经存在了`

发布于 2025-02-11 14:02:45 字数 2031 浏览 1 评论 0 原文

在我的Azure APIM中,我试图将API操作添加到API中,以便可以通过Terraform向其添加策略。除了 azurerm_api_management_api_operation 零件外,所有功能都很好。基本上,它出现了众所周知的Terraform错误 ID的资源已经存在..

我的Terraform的一个片段是:

    resource "azurerm_api_management_api" "business_api_v1" {
    name                = "business-api-v1"
    resource_group_name = data.azurerm_resource_group.app_rg.name
    api_management_name = module.api.name
    revision            = "1"
    display_name        = "Business API v1"
    path                = "business-data/v1"
    protocols           = ["https"]
    version             = "v1"
    version_set_id      = azurerm_api_management_api_version_set.business-api-version-set.id

    import {
        content_format = "openapi+json"
        content_value  = <<JSON
            <...content of json open API spec...>
        JSON
    }
    }
    resource "azurerm_api_management_api_operation" "search_businesses_v1" {
    operation_id        = "tf-search-businesses"
    api_name            = azurerm_api_management_api.business_api_v1.name
    api_management_name = module.api.name
    resource_group_name = data.azurerm_resource_group.app_rg.name
    display_name        = "Search Businesses"
    method              = "GET"
    url_template        = "/businesses"
    description         = "Search Businesses by name"
    }

我不知所措,为什么Terraform创建它然后给出错误:

A resource with the ID "/subscriptions/<sub_id>/resourceGroups/<resource_group>/providers/Microsoft.ApiManagement/service/abcd/apis/business-api-v1/operations/tf-search-businesses" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_api_management_api_operation" for more information.

我几次删除了我的API(&amp;基础资源),并运行了 plan> plan >和应用阶段。在应用过程中,阶段创建资源后会引发错误。 100%确定没有其他人创建它,我尝试在尝试解决此问题时多次重命名 name 属性。

有人知道可能是什么问题吗?谢谢。

In my Azure APIM, I am trying to add API Operation to my API so I can add Policies to it via Terraform. All works fine except the azurerm_api_management_api_operation part. It basically errors out saying with the well known Terraform error A resource with ID ... already exists...

A snippet of my Terraform is this:

    resource "azurerm_api_management_api" "business_api_v1" {
    name                = "business-api-v1"
    resource_group_name = data.azurerm_resource_group.app_rg.name
    api_management_name = module.api.name
    revision            = "1"
    display_name        = "Business API v1"
    path                = "business-data/v1"
    protocols           = ["https"]
    version             = "v1"
    version_set_id      = azurerm_api_management_api_version_set.business-api-version-set.id

    import {
        content_format = "openapi+json"
        content_value  = <<JSON
            <...content of json open API spec...>
        JSON
    }
    }
    resource "azurerm_api_management_api_operation" "search_businesses_v1" {
    operation_id        = "tf-search-businesses"
    api_name            = azurerm_api_management_api.business_api_v1.name
    api_management_name = module.api.name
    resource_group_name = data.azurerm_resource_group.app_rg.name
    display_name        = "Search Businesses"
    method              = "GET"
    url_template        = "/businesses"
    description         = "Search Businesses by name"
    }

I am at loss as to why Terraform creates it and then gives the error:

A resource with the ID "/subscriptions/<sub_id>/resourceGroups/<resource_group>/providers/Microsoft.ApiManagement/service/abcd/apis/business-api-v1/operations/tf-search-businesses" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_api_management_api_operation" for more information.

I have deleted my API (& underlying resource in question) several times and ran the plan and apply stages. During apply stage it throws the error after creating the resource. 100% sure no one else created it I have tried renaming the name attribute several times as I attempted to troubleshoot this.

Anyone knows what might be the issue? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦与时光遇 2025-02-18 14:02:45
  • 当将资源部署到Terraform状态文件中时
    每当创建资源时,Azure及其状态就会发生变化。
  • 正如您所说,您已通过UI IE删除了API操作;这
    门户网站,Terraform状态可能与真实状态匹配。

由于将被带到Terraform管理控制下,因此请尝试通过核对@Markus所说的现有更改来匹配Terraform的导入,并通过核对管理的访问。

terraform import <terraform_id> <azure_resource_id> 

  • 尝试删除Terraform的本地缓存和所有资源,然后再次应用更改。
  • 当Terraform在本地Terraform.tfstate中保存该州时,请尝试
    手动更改后端。
  • 如果您尝试更改资源参考的名称
    您的Terraform状态文件。

可以使用 Terraform状态MV 完成此

  • 操作
    实际一个,您可以使用

Terraform State rm {resource}

类似问题:

  1. terraform-provider-azurerm/issues.github
  • As terraform state file is created when the resource is deployed to
    azure and its state changes whenever resources are created .
  • So as you said you have deleted the api operation via the UI i.e; the
    portal, the terraform state may have not matched with the real state.

As they are to be brought under terraform management control , do try to terraform import to match the existing changes as said by @markus by checking with the access from management.

terraform import <terraform_id> <azure_resource_id> 

or

  • Try deleting Terraform's local cache and all the resources and apply changes again .
  • As terraform saved the state in local terraform.tfstate , try to
    manually change the backend.
  • If you are tryting to change the name of a resource reference within
    your Terraform state file.

It can be done using terraform state mv

  • If you wanted to remove resource created later but want to keep the
    actual one ,you can use

terraform state rm {name of resource}

Similar issues:

  1. terraform-provider-azurerm/issues.github
  2. terraform apply in Azure DevOps fails with "resource with the ID
    already exists" - DEV Community
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文