使用Terraform的azapi_resource创建Slack连接时返回ApiNotFound错误
解决Azure Slack连接创建时的ApiNotFound错误
尝试将ARM模板转换为Terraform代码以创建Microsoft.Web/connections类型的Slack连接时,执行后返回ApiNotFound错误,仅成功创建资源组和集成账户,Slack连接创建失败。

原Terraform代码
provider "azurerm" { features {} } provider "azapi" { } locals { name = "slack-notification-test" resource_location = "South Central US" logic_app_sku = "Standard" resource_group_name = "deleteme" slack_channel_name = "critical-alerts" } data "azurerm_subscription" "current" { } resource "azurerm_resource_group" "group" { name = local.resource_group_name location = local.resource_location } resource "azapi_resource" "slackIntegrationAccount" { type = "Microsoft.Logic/integrationAccounts@2016-06-01" name = "${local.name}-slackIntegration" parent_id = azurerm_resource_group.group.id location = local.resource_location body = jsonencode({ "sku": { "name": "Free" }, "properties": { "state": "Enabled" } }) } resource "azapi_resource" "slackConnection" { type = "Microsoft.Web/connections@2016-06-01" name = "${local.name}-slackConnection" parent_id = azurerm_resource_group.group.id location = local.resource_location body = jsonencode({ "properties": { "displayName": "Slack", "statuses": [ { "status": "Connected" } ], "customParameterValues": {}, "nonSecretParameterValues": {}, "createdTime": "2022-09-20T17:10:26.9106346Z", "changedTime": "2022-09-20T17:10:37.870701Z", "api": { "name": "${local.name}-slackConnection", "displayName": "Slack", "description": "Slack is a team communication tool, that brings together all of your team communications in one place, instantly searchable and available wherever you go.", "iconUri": "https://connectoricons-prod.azureedge.net/releases/v1.0.1582/1.0.1582.2855/${local.name}-slackConnection/icon.png" "brandColor": "#78D4B6", "id": "/subscriptions/${data.azurerm_subscription.current.subscription_id}/providers/Microsoft.Web/locations/${local.resource_location}/managedApis/${local.name}-slackConnection" "type": "Microsoft.Web/locations/managedApis" }, "testLinks": [ { "requestUri": "https://management.azure.com:443/subscriptions/${data.azurerm_subscription.current.subscription_id}/resourceGroups/${local.resource_location}/providers/Microsoft.Web/connections/${local.name}-slackConnection/extensions/proxy/conversations.list?api-version=2016-06-01" "method": "get" } ] } }) }
问题根源
- 托管API名称错误:Azure中Slack的托管API固定名称为
slack,原代码错误使用了自定义连接名称${local.name}-slackConnection,导致系统无法找到对应API。 - JSON语法错误:多个字段后缺少逗号(如
iconUri、requestUri),会引发JSON解析失败。 - Test链接路径错误:
resourceGroups参数应为资源组名称local.resource_group_name,而非地域名称local.resource_location。
修正后的Terraform代码
provider "azurerm" { features {} } provider "azapi" { } locals { name = "slack-notification-test" resource_location = "South Central US" logic_app_sku = "Standard" resource_group_name = "deleteme" slack_channel_name = "critical-alerts" } data "azurerm_subscription" "current" { } resource "azurerm_resource_group" "group" { name = local.resource_group_name location = local.resource_location } resource "azapi_resource" "slackIntegrationAccount" { type = "Microsoft.Logic/integrationAccounts@2016-06-01" name = "${local.name}-slackIntegration" parent_id = azurerm_resource_group.group.id location = local.resource_location body = jsonencode({ "sku": { "name": "Free" }, "properties": { "state": "Enabled" } }) } resource "azapi_resource" "slackConnection" { type = "Microsoft.Web/connections@2016-06-01" name = "${local.name}-slackConnection" parent_id = azurerm_resource_group.group.id location = local.resource_location body = jsonencode({ "properties": { "displayName": "Slack", "statuses": [ { "status": "Connected" } ], "customParameterValues": {}, "nonSecretParameterValues": {}, "createdTime": "2022-09-20T17:10:26.9106346Z", "changedTime": "2022-09-20T17:10:37.870701Z", "api": { "name": "slack", "displayName": "Slack", "description": "Slack is a team communication tool, that brings together all of your team communications in one place, instantly searchable and available wherever you go.", "iconUri": "https://connectoricons-prod.azureedge.net/releases/v1.0.1582/1.0.1582.2855/slack/icon.png", "brandColor": "#78D4B6", "id": "/subscriptions/${data.azurerm_subscription.current.subscription_id}/providers/Microsoft.Web/locations/${local.resource_location}/managedApis/slack", "type": "Microsoft.Web/locations/managedApis" }, "testLinks": [ { "requestUri": "https://management.azure.com:443/subscriptions/${data.azurerm_subscription.current.subscription_id}/resourceGroups/${local.resource_group_name}/providers/Microsoft.Web/connections/${local.name}-slackConnection/extensions/proxy/conversations.list?api-version=2016-06-01", "method": "get" } ] } }) }
后续说明
- 应用修正后的代码后,Slack连接会成功创建,但需手动在Azure门户完成Slack的OAuth授权流程,才能正常使用连接发送消息。
- 集成账户并非Slack连接的必需资源,若无需使用可移除
azapi_resource.slackIntegrationAccount模块。
内容的提问来源于stack exchange,提问作者Zach Johnson
相关产品推荐
相关产品推荐

