我想要的只是使用开放 API yaml 文档中定义的 API 操作来部署我的 Azure API 网关。我的下面的代码部署了 Windows 函数应用程序和 API 网关,而且我还可以看到 API 中列出了我所需的操作。
问题是当我导入开放 API 文档时,我无法更新单独的 API 操作后端,该后端应该是我的 Azure 函数应用程序 URL。
resource "azurerm_resource_group" "windows-function-rg" {
name = var.resource_group_name
location = "west europe"
tags = {
"Environment" = "Dev"
"CostResponsible" = "IT"
"projectName" = "crm-cost-estimate"
"aks-managed-cluster-name" = "unknown"
}
}
resource "azurerm_storage_account" "windows-storage-account" {
name = var.storage_account_name
resource_group_name = azurerm_resource_group.windows-function-rg.name
location = azurerm_resource_group.windows-function-rg.location
account_tier = "Standard"
account_replication_type = "LRS"
tags = {
"Environment" = "Dev"
"CostResponsible" = "IT"
"projectName" = "crm-cost-estimate"
"aks-managed-cluster-name" = "unknown"
}
}
resource "azurerm_service_plan" "windows-service-plan" {
name = var.service_plan_name
resource_group_name = azurerm_resource_group.windows-function-rg.name
location = azurerm_resource_group.windows-function-rg.location
os_type = "Windows"
sku_name = "Y1"
tags = {
"Environment" = "Dev"
"CostResponsible" = "IT"
"projectName" = "crm-cost-estimate"
"aks-managed-cluster-name" = "unknown"
}
}
resource "azurerm_application_insights" "windows-application-insights" {
name = "application-insights-${var.azurerm_windows_function_app}"
location = azurerm_resource_group.windows-function-rg.location
resource_group_name = azurerm_resource_group.windows-function-rg.name
application_type = "Node.JS"
tags = {
"Environment" = "Dev"
"CostResponsible" = "IT"
"projectName" = "crm-cost-estimate"
"aks-managed-cluster-name" = "unknown"
}
depends_on = [ azurerm_resource_group.windows-function-rg ]
}
resource "azurerm_windows_function_app" "windows-python-linux-function-app" {
name = var.azurerm_windows_function_app
resource_group_name = azurerm_resource_group.windows-function-rg.name
location = azurerm_resource_group.windows-function-rg.location
service_plan_id = azurerm_service_plan.windows-service-plan.id
storage_account_name = azurerm_storage_account.windows-storage-account.name
storage_account_access_key = azurerm_storage_account.windows-storage-account.primary_access_key
https_only = true
site_config {
application_insights_key = azurerm_application_insights.windows-application-insights.instrumentation_key
application_insights_connection_string = azurerm_application_insights.windows-application-insights.connection_string
application_stack {
node_version = "~18" #FUNCTIONS_WORKER_RUNTIME
}
}
app_settings = {
"APPINSIGHTS_INSTRUMENTATIONKEY" = "${azurerm_application_insights.windows-application-insights.instrumentation_key}"
}
tags = {
"Environment" = "Dev"
"CostResponsible" = "IT"
"projectName" = "crm-cost-estimate"
"aks-managed-cluster-name" = "unknown"
}
}
resource "azurerm_api_management" "sharatapim" {
name = "sharattes-apim-func-001"
location = azurerm_resource_group.windows-function-rg.location
resource_group_name = azurerm_resource_group.windows-function-rg.name
publisher_name = "sharat"
publisher_email = "[email protected]"
sku_name = "Consumption_0"
}
resource "azurerm_api_management_backend" "sharatapibackend" {
name = "sharattes-apim-backend-001"
resource_group_name = azurerm_resource_group.windows-function-rg.name
api_management_name = azurerm_api_management.sharatapim.name
protocol = "http"
url = "https://my-function-app-url/api"
depends_on = [azurerm_resource_group.windows-function-rg,
azurerm_api_management.sharatapim,
azurerm_windows_function_app.windows-python-linux-function-app]
credentials {
header = {
"x-functions-key" = "{{sharattes-apim-backend-001}}"
}
}
}
data "azurerm_function_app_host_keys" "functionkeys" {
name = azurerm_windows_function_app.windows-python-linux-function-app.name
resource_group_name = azurerm_resource_group.windows-function-rg.name
}
resource "azurerm_api_management_api" "sharatapimgmntapi" {
name = "sharattes-import-api-001"
resource_group_name = azurerm_resource_group.windows-function-rg.name
api_management_name = azurerm_api_management.sharatapim.name
revision = "1"
display_name = "sharat-latestAPI"
path = ""
protocols = ["https"]
import {
content_format = "openapi"
content_value = file("${path.module}/ExtranetAPIopenapi.yaml")
}
depends_on = [azurerm_windows_function_app.windows-python-linux-function-app]
}
resource "azurerm_api_management_api_operation" "health" {
api_management_name = azurerm_api_management.sharatapim.name
api_name = azurerm_api_management_api.sharatapimgmntapi.name
display_name = "health"
method = "GET"
operation_id = "healthy"
resource_group_name = azurerm_resource_group.windows-function-rg.name
url_template = "/health"
response {
status_code = 200
}
response {
status_code = 500
}
}
resource "azurerm_api_management_api_operation_policy" "example" {
api_name = azurerm_api_management_api.sharatapimgmntapi.name
api_management_name = azurerm_api_management.sharatapim.name
operation_id = "healthy"
resource_group_name = azurerm_resource_group.windows-function-rg.name
xml_content = <<XML
<policies>
<inbound>
<base />
<set-backend-service id="apim-generated-policy" backend-id="sharattes-functionapp-001" />
</inbound>
</policies>
XML
depends_on = [
azurerm_windows_function_app.windows-python-linux-function-app
]
}
错误信息:
│ 错误:创建/更新操作(订阅:“xxxxxxxxxxxxxxxxxx” │ 资源组名称:“sharattes-rg-test-001” │ 服务名称:“sharattes-apim-func-001” │ API:“sharattes-import-api-001” │ 操作:“健康”):意外状态 400,错误:ValidationError:一个或多个字段包含不正确的值: │ │ 与 azurerm_api_management_api_operation.health, │ 在 main.tf 第 215 行,资源“azurerm_api_management_api_operation”“health”中: │ 215:资源“azurerm_api_management_api_operation”“健康”{ │ ╵ ╷
│ 错误:创建或更新操作(订阅:“xxxxxxxxx” │ 资源组名称:“sharattes-rg-test-001” │ 服务名称:“sharattes-apim-func-001” │ API:“sharattes-import-api-001” │ 操作:“健康”):意外状态 400,错误:ValidationError:未找到具有指定标识符的实体 │ │ 使用 azurerm_api_management_api_operation_policy.example, │ 在 main.tf 第 252 行,资源“azurerm_api_management_api_operation_policy”“example”中: │ 252:资源“azurerm_api_management_api_operation_policy”“示例”{ │ ╵ PS C:\Users\sbhaskar\Desktop esting_api>
尝试通过 Terraform 更新 Azure API Gateway 操作后端时出现问题:
我对您的代码进行了以下更改,并能够成功部署它。
政策内容中的标识符未正确给出。将您的政策内容修改为
<set-backend-service backend-id="sharattes-apim-backend-001jjj" />
。并相应地做了一些其他更改以满足您的要求。
provider "azurerm" {
features {
}
}
data "azurerm_resource_group" "windows-function-rg" {
name = "xxx"
}
data "azurerm_storage_account" "windows-storage-account" {
name = "functionsappjah"
resource_group_name = data.azurerm_resource_group.windows-function-rg.name
}
resource "azurerm_service_plan" "windows-service-plan" {
name = "apikurk"
resource_group_name = data.azurerm_resource_group.windows-function-rg.name
location = data.azurerm_resource_group.windows-function-rg.location
os_type = "Windows"
sku_name = "Y1"
}
resource "azurerm_application_insights" "windows-application-insights" {
name = "application-insights-jah"
location = data.azurerm_resource_group.windows-function-rg.location
resource_group_name = data.azurerm_resource_group.windows-function-rg.name
application_type = "Node.JS"
depends_on = [ data.azurerm_resource_group.windows-function-rg ]
}
resource "azurerm_windows_function_app" "windows-python-linux-function-app" {
name = "kurkfuncj"
resource_group_name = data.azurerm_resource_group.windows-function-rg.name
location = data.azurerm_resource_group.windows-function-rg.location
service_plan_id = azurerm_service_plan.windows-service-plan.id
storage_account_name = data.azurerm_storage_account.windows-storage-account.name
storage_account_access_key = data.azurerm_storage_account.windows-storage-account.primary_access_key
https_only = true
site_config {
application_insights_key = azurerm_application_insights.windows-application-insights.instrumentation_key
application_insights_connection_string = azurerm_application_insights.windows-application-insights.connection_string
application_stack {
node_version = "~18" #FUNCTIONS_WORKER_RUNTIME
}
}
app_settings = {
"APPINSIGHTS_INSTRUMENTATIONKEY" = "${azurerm_application_insights.windows-application-insights.instrumentation_key}"
}
}
resource "azurerm_api_management" "sharatapim" {
name = "sharattes-apim-func-001jjj"
location = data.azurerm_resource_group.windows-function-rg.location
resource_group_name = data.azurerm_resource_group.windows-function-rg.name
publisher_name = "sharat"
publisher_email = "[email protected]"
sku_name = "Consumption_0"
}
resource "azurerm_api_management_backend" "sharatapibackend" {
name = "sharattes-apim-backend-001jjj"
resource_group_name = data.azurerm_resource_group.windows-function-rg.name
api_management_name = azurerm_api_management.sharatapim.name
protocol = "http"
url = "https://my-function-app-url/api"
depends_on = [data.azurerm_resource_group.windows-function-rg,
azurerm_api_management.sharatapim,
azurerm_windows_function_app.windows-python-linux-function-app]
credentials {
header = {
"x-functions-key" = "[sharattes-apim-backend-001]"
}
}
}
data "azurerm_function_app_host_keys" "functionkeys" {
name = azurerm_windows_function_app.windows-python-linux-function-app.name
resource_group_name = data.azurerm_resource_group.windows-function-rg.name
}
resource "azurerm_api_management_api" "sharatapimgmntapi" {
name = "sharattes-import-api-001"
resource_group_name = data.azurerm_resource_group.windows-function-rg.name
api_management_name = azurerm_api_management.sharatapim.name
revision = "1"
display_name = "sharat-latestAPI"
path = ""
protocols = ["https"]
depends_on = [azurerm_windows_function_app.windows-python-linux-function-app]
}
resource "azurerm_api_management_api_operation" "health" {
api_management_name = azurerm_api_management.sharatapim.name
api_name = azurerm_api_management_api.sharatapimgmntapi.name
display_name = "health"
method = "GET"
operation_id = "healthy"
resource_group_name = data.azurerm_resource_group.windows-function-rg.name
url_template = "/health"
response {
status_code = 200
}
response {
status_code = 500
}
}
resource "azurerm_api_management_api_operation_policy" "example" {
api_name = azurerm_api_management_api.sharatapimgmntapi.name
api_management_name = azurerm_api_management.sharatapim.name
operation_id = azurerm_api_management_api_operation.health.operation_id
resource_group_name = data.azurerm_resource_group.windows-function-rg.name
xml_content = <<XML
<policies>
<inbound>
<set-backend-service backend-id="sharattes-apim-backend-001jjj" />
</inbound>
</policies>
XML
depends_on = [
azurerm_windows_function_app.windows-python-linux-function-app
]
}
部署成功: