azure 将Terraform与azapi_resource提供程序一起使用时,我无法创建松弛连接,返回“ApiNotFound”

hpxqektj  于 2023-03-31  发布在  其他
关注(0)|答案(2)|浏览(161)

我尝试将ARM模板转换为Terraform,首先创建Microsoft.Web/connections资源到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"
                    }
                ]
            }
    })
}

我在输出中得到错误“ApiNotFound”,并创建了资源组和集成帐户,但仅此而已。x1c 0d1x

5lhxktic

5lhxktic1#

我检查了以下代码:

main.tf

resource "azapi_resource" "slackIntegrationAccount" {
    type = "Microsoft.Logic/integrationAccounts@2016-06-01"
    name = "kavya-slackIntegration"
    parent_id = data.azurerm_resource_group.example.id
    location =  data.azurerm_resource_group.example.location
    body = jsonencode({
            "sku": {
                "name": "Free"
            },
            "properties": {
                "state": "Enabled"
            }
    })
}

resource "azapi_resource" "slackConnection" {
    type = "Microsoft.Web/connections@2016-06-01"
    name = "kavya-slackConnection"
    parent_id = data.azurerm_resource_group.example.id
    location =  data.azurerm_resource_group.example.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": "kavya-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/kavya-slackConnection/icon.png"
                    "brandColor": "#78D4B6",
                    "id": "/subscriptions/${data.azurerm_subscription.current.subscription_id}/providers/Microsoft.Web/locations/${data.azurerm_resource_group.example.location}/managedApis/kavya-slackConnection"
                    "type": "Microsoft.Web/locations/managedApis"
                },
                "testLinks": [
                    {
                        "requestUri": "https://management.azure.com:443/subscriptions/${data.azurerm_subscription.current.subscription_id}/resourceGroups/${data.azurerm_resource_group.example.location}/providers/Microsoft.Web/connections/kavya-slackConnection/extensions/proxy/conversations.list?api-version=2016-06-01"
                        "method": "get"
                    }
                ]
            }
    })
}

我收到错误

RESPONSE 404: 404 Not Found
│ ERROR CODE: ApiNotFound
│ --------------------------------------------------------------------------------
│ {
│   "error": {
│     "code": "ApiNotFound",
│     "message": "The API 'kavya-slackConnection' could not be found."
│   }
│ }
│ --------------------------------------------------------------------------------
│
│
│   with azapi_resource.slackConnection,
│   on main.tf line 191, in resource "azapi_resource" "slackConnection":
│  191: resource "azapi_resource" "slackConnection" {

当你在Terraform中定义一个自定义API连接时,它似乎并没有在Azure中创建API连接。
您可能需要使用Azure门户文档手动在Azure中创建自定义API连接资源,或者使用PowerShell或Azure CLI以编程方式创建。
在我的例子中,我使用资源"azurerm_api_connection"创建了API连接:
使用下面的ARM模板,尝试在terraform中创建ARM模板部署:

示例:

data "azurerm_managed_api" "example" {
  name     = "servicebus"
  location = data.azurerm_resource_group.example.location
}

resource "azurerm_servicebus_namespace" "example" {
  name                = "acctestsbn-conn-example"
  location            = data.azurerm_resource_group.example.location
  resource_group_name = data.azurerm_resource_group.example.name
  sku                 = "Basic"
}

resource "azurerm_api_connection" "example" {
  name                = "kavya-slackConnection"
  resource_group_name = data.azurerm_resource_group.example.name
  managed_api_id      = data.azurerm_managed_api.example.id
  display_name        = "kavya-slackConnection"

  parameter_values = {
    connectionString = azurerm_servicebus_namespace.example.default_primary_connection_string
  }

  tags = {
    Hello = "World"
  }

  lifecycle {
    
    ignore_changes = [parameter_values]
  }
}
  • 这里我包括“善良”:“V1”,设置“connectionState”:“已启用”,

摘录:

"kind": "V1",
  "properties": {
        "displayName": "kavya-slackConnection",
        "authenticatedUser": {},
        "overallStatus": "Connected",
        "statuses": [
            {
                "status": "Connected"
            }
        ],
        "connectionState": "Enabled",

main.tf:

resource "azurerm_resource_group_template_deployment" "example" {
  name                = "exmpldeployment"
  resource_group_name = data.azurerm_resource_group.example.name
  deployment_mode     = "Incremental"
  template_content =  <<TEMPLATE
{   
  "$schema": "https://schema.management.azure.com/schemas/2018-05-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
    "variables": {},
    "resources": [
        {
  "type": "Microsoft.Web/connections",
  "apiVersion": "2016-06-01",
  "name": "string",
  "location": "eastus",
  "kind": "V1",
  "properties": {
        "displayName": "kavya-slackConnection",
        "authenticatedUser": {},
        "overallStatus": "Connected",
        "statuses": [
            {
                "status": "Connected"
            }
        ],
        "connectionState": "Enabled",
        "parameterValues": {},
        "customParameterValues": {},
        "createdTime": "2023-03-29T15:07:04.2976682Z",
        "changedTime": "2023-03-29T15:07:04.2976682Z",
        "api": {
            "name": "servicebus",
            "displayName": "Service Bus",
            "description": "Connect to Azure Service Bus to send and receive messages. You can perform actions such as send to queue, send to topic, receive from queue, receive from subscription, etc.",
            "iconUri": "https://connectoricons-prod.azureedge.net/releases/v1.0.1627/1.0.1627.3238/servicebus/icon.png",
            "brandColor": "#c4d5ff",
            "category": "Standard",
            "id": "/subscriptions/xxx/providers/Microsoft.Web/locations/eastus/managedApis/servicebus",
            "type": "Microsoft.Web/locations/managedApis"
        },
        "testLinks": [],
        "testRequests": []
    }
    }
    ],
    "outputs": {
      "exampleOutput": {
        "type": "string",
        "value": "someoutput"
      }
    }
}
TEMPLATE

  // NOTE: whilst we show an inline template here, we recommend
  // sourcing this from a file for readability/editor support
}

参考:Unable to connect the API connection to the logic App via ARM template in terraform - Stack Overflow by @Ansuman Bal

krugob8w

krugob8w2#

正如@kavyaS提到的,使用azurerm_managed_API而不是azapi_resource。下面是一个工作片段:

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 = "pfl-production-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"
            }
    })
}

data "azurerm_managed_api" "slack" {
    name = "Slack"
    location = local.resource_location
}

resource "azurerm_api_connection" "slackConnection" {
    name = "${local.name}-slackConnection"
    resource_group_name  = azurerm_resource_group.group.name
    display_name = "Slack"
    managed_api_id = data.azurerm_managed_api.slack.id
}

相关问题