如何使用Terraform将多个组添加到Azure API管理服务产品?

5gfr0r5j  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(100)

我想在Terraform的Azure API管理服务中为单个产品附加多个组。
Terraform注册表只允许通过其名称附加单个组。是否有任何方法来附加多个组,如开发人员,游客如屏幕截图中所示。

resource "azurerm_api_management_product_group" "example" {
  product_id          = "test_product"
  group_name          = "developers"
  api_management_name = "test-api-management"
  resource_group_name = "test-rg"
}
jtoj6r0c

jtoj6r0c1#

我同意Marcin,您可以在代码中使用for-each metadata argument来创建多个Azure APIM管理服务产品组,如下所示:

官方Terraform Document1:Document2-
My main.tf code:-

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "3.74.0"
    }
  }
}

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "siliconrg5r"
  location = "West Europe"
}

resource "azurerm_api_management" "example" {
  name                = "silicon5r-apim"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  publisher_name      = "My Company"
  publisher_email     = "[email protected]"

  sku_name = "Developer_1"
}

variable "group_names" {
  type    = list(string)
  default = ["developers", "guests"]
}

resource "azurerm_api_management_product" "example" {
  product_id            = "test-product"
  api_management_name   = azurerm_api_management.example.name
  resource_group_name   = azurerm_resource_group.example.name
  display_name          = "Test Product"
  subscription_required = true
  subscriptions_limit = 100
  approval_required     = true
  published             = true
}

resource "azurerm_api_management_product_group" "example" {
  for_each = toset(var.group_names)

  product_id          = azurerm_api_management_product.example.product_id
  group_name          = each.value
  api_management_name = azurerm_api_management.example.name
  resource_group_name = azurerm_resource_group.example.name
}

output "api_management_url" {
  value = azurerm_api_management.example.portal_url
}

输出:-

部署产品的APIM示例成功:-

已成功创建包含访客和开发人员的产品组:-

相关问题