You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为azurerm_api_management_api的content_value指定本地文件路径?

在Terraform的azurerm_api_management_api中引用本地Swagger文件

我正在用Terraform的azurerm_api_management_api资源创建Azure API,现有代码如下:

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"
  }
}

但需求是把content_value指向本地test-input文件夹下的JSON格式Swagger文件,而官方文档显示content_value似乎只支持URL,请问该怎么实现?


要引用本地Swagger文件,只需要调整两个关键参数即可实现:

  • 将content_format从swagger-link-json改为swagger-json(现在直接传入Swagger内容而非URL链接)
  • 使用Terraform内置的file()函数读取本地文件内容,作为content_value的值

修改后的代码示例:

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-json"
    # 替换为你的本地Swagger文件名,path.module表示当前模块所在目录
    content_value  = file("${path.module}/test-input/your-swagger-file.json")
  }
}

注意事项

  • 确保文件路径正确:${path.module}会定位到当前Terraform模块的根目录,若在根模块下也可直接用相对路径如./test-input/your-swagger-file.json
  • 确认本地JSON文件格式合法无语法错误,否则Terraform读取或Azure导入时会报错
  • 若Swagger文件体积较大,可改用filebase64()函数将内容编码为Base64,对应content_format改为swagger-json-base64

内容的提问来源于stack exchange,提问作者hitman126

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 04:42:49