【问题标题】:How do I add additional APIs to existing Azure API Manager using Terraform?如何使用 Terraform 将其他 API 添加到现有 Azure API 管理器?
【发布时间】:2021-01-14 20:44:26
【问题描述】:

Terraform 新手,正如问题所述,我想将其他 API 添加到现有 API Manager 实例。下面是 Terraform 的示例,它工作正常。但我想添加第二个 api。是否绝对需要将其内联在此 Terraform 脚本中?我问的原因是,如果我的团队开发“示例 api”,而第二个团队开发“awesomeexample api”,他们能否拥有仅用于存储库中 api 资源的 Terraform 代码?我需要通过 Powershell 或 GraphAPI 获取 API Manager 的名称吗? 编辑: 现在,当我添加一个额外的 api 资源时,它会破坏第一个。我猜这是因为 Terraform 状态......但这不是我想要的行为。我只是想独立于 api 管理器继续添加 api 或更改它们。

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "West Europe"
}

resource "azurerm_api_management" "example" {
  name                = "example-apim"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  publisher_name      = "My Company"
  publisher_email     = "company@terraform.io"

  sku_name = "Developer_1"
}

resource "azurerm_api_management_api" "example" {
  name                = "example-api"
  resource_group_name = azurerm_resource_group.example.name
  api_management_name = azurerm_api_management.example.name
  revision            = "1"
  display_name        = "Example API"
  path                = "example"
  protocols           = ["https"]

  import {
    content_format = "swagger-link-json"
    content_value  = "http://conferenceapi.azurewebsites.net/?format=json"
  }
}

【问题讨论】:

  • terraform plan 说什么?

标签: terraform azure-api-management


【解决方案1】:

我,我一直将每个 API 放在它自己的 tf 文件中。

就像您的示例 api 一样,我会创建一个 api-example.tf 文件,将 azurerm_api_management_api 块剪切/粘贴到其中。

terraform init/plan/apply 应该仍然可以正常工作。

要在此 API Mgmt 实例中添加新 API,只需将 api-example.tf 复制到 api-newapi.tf,根据需要进行编辑(名称/显示名称/路径/导入和 poof,新 api。

我不喜欢对多个不同的资源使用相同的实例名称。在这里,您使用了 3 次示例,一个 RG、APIM 和 APIM_API。它会变得令人困惑,并且可能会在您添加一个也称为“示例”的新 API 时中断。我使用 thisrg、ex-apim 和 example-api(因为您的原始 API 称为 example)。你的 awsomeexample api 我会调用 awesome-api 或类似的东西来区分 example-api 和 awesomeexample-api

【讨论】:

    【解决方案2】:

    刚刚遇到了类似的事情,导致我来到这里。

    我们的一位开发人员通过修改我们的 terraform 文件、计划并应用它来添加他的资源。通过这样做,他修改了远程状态,但他从未提交他的更改。因此,当我们要从 master 应用 terraform 文件时,本地计划的状态与远程中的不匹配。

    但在你的情况下,这很难说。如果您在同一文件中定义两个 API 发生冲突(例如 2x 资源 "azurerm_api_management_api" "example"),可能会导致您出现问题。

    【讨论】:

      猜你喜欢
      • 2017-08-19
      • 2020-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-16
      • 1970-01-01
      相关资源
      最近更新 更多