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

问题描述 投票:0回答:1

我想在 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"
}
azure terraform azure-api-management
1个回答
0
投票

我同意Marcin,您可以在代码中使用

for-each metadata argument
来创建多个Azure APIM管理服务产品组,如下所示:-

官方 Terraform 文档1: 文档2-

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
}

输出:-

enter image description here

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

enter image description here

嘉宾和开发者的产品组创建成功:-

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.