skip to Main Content

I’m attempting to convert an ARM template to Terraform that starts of by creating a Microsoft.Web/connections resource to Slack. I keep running into an issue that has me stumped. Using the following 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"
                    }
                ]
            }
    })
}

I get the error "ApiNotFound" in the output, and the resource group and integration account are created but that is it.
enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    As @kavyaS mentioned using a azurerm_managed_api instead of azapi_resource. Here is a working snippet:

    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
    }
    

  2. I checked with following with following code:

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

    I received the error

    enter image description here

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

    When you define a custom API connection in Terraform, it seems it is not actually creating the API connection in Azure.

    one may need to create the custom API connection resource in Azure manually by using Azure portal documentation, or programmatically using PowerShell or Azure CLI.

    In my case I created api connection using resource "azurerm_api_connection":

    With the below ARM template , tried to create arm template deployment in terraform:

    example :

    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]
      }
    }
    
    • Here I included "kind": "V1", set "connectionState": "Enabled",

    snippet:

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

    enter image description here

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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search