1

I got question is there a way to import azure funtion app to api managment api using terraform. https://learn.microsoft.com/en-us/azure/api-management/import-function-app-as-api

below is link to terraform resource but I do not see funtion app in import section

A import block supports the following: content_format - (Required) The format of the content from which the API Definition should be imported. possible values are: openapi, openapi+json, openapi+json-link, openapi-link, swagger-json, swagger-link- json, wadl-link-json, wadl-xml, wsdl and wsdl-link.

https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/api_management_api

Is this possible?

import-function-app-as-api

tebe20
  • 11
  • 2
  • I think it should not be a terraform responsibility to do such a thing. Import apis into APIM is a release process, while Terraform is for IaC – Thiago Custodio Feb 03 '23 at 14:51

1 Answers1

0

Try below terraform code to import function app as an api with Azure api management. I added the function App Url under import block and was able to deployed successfully.

main.tf:

provider "azurerm"{
features{}
}

resource "azurerm_resource_group" "example" {
  name     = "<resource name>"
  location = "West Europe"
}

resource "azurerm_storage_account" "example" {
  name                     = "xxxxstorageaccount"
  resource_group_name      = azurerm_resource_group.example.name
  location                 = azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

resource "azurerm_app_service_plan" "example" {
  name                = "azure-functions-test-service-plan"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  sku {
    tier = "Standard"
    size = "S1"
  }
}

resource "azurerm_function_app" "example" {
  name                       = "xxxfunctionapp"
  location                   = azurerm_resource_group.example.location
  resource_group_name        = azurerm_resource_group.example.name
  app_service_plan_id        = azurerm_app_service_plan.example.id
  storage_account_name       = azurerm_storage_account.example.name
  storage_account_access_key = azurerm_storage_account.example.primary_access_key
}

resource "azurerm_api_management" "example" {
  name                = "xxxxapim"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  publisher_name      = "xxx"
  publisher_email     = "xxx@terraform.io"

  sku_name = "Developer_1"
}

resource "azurerm_api_management_api" "example" {
  name                = "xxxapi"
  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  = azurerm_function_app.example.name
  }
}

terraform plan:

enter image description here

terraform apply:

enter image description here

Deployed in Portal:

enter image description here

Reference: Terraform registry

Jahnavi
  • 3,076
  • 1
  • 3
  • 10
  • Why does your plan show only one resource to be deployed? I don't think your solution actually links the API exposed by the function app, and the APIM. I tested, and it fails. – ccoutinho Mar 24 '23 at 10:35