Houssem Ben Dhaou
Houssem Ben Dhaou

Reputation: 9

How can I add an azure function to an API Management using ARM template

I'm using this ARM template to add an external Azure function (already defined in other template) to my API management but I get a validation error (resource is not defined in template). Is there any solution to automate mapping azure function with an API management?

{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {...},
"resources": [

    {
        "apiVersion": "2017-03-01",
        "name": "[parameters('apiManagementServiceName')]",
        "type": "Microsoft.ApiManagement/service",
        "location": "[parameters('location')]",
        "tags": {},
        "sku": {
            "name": "[parameters('sku')]",
            "capacity": "[parameters('skuCount')]"
        },
        "properties": {
            "publisherEmail": "[parameters('publisherEmail')]",
            "publisherName": "[parameters('publisherName')]"
        }
    },
    {
        "type": "Microsoft.ApiManagement/service/apis",
        "apiVersion": "2018-06-01-preview",
        "name": "[concat(parameters('apiManagementServiceName'), '/', parameters('indexerApp'))]",
        "dependsOn": [
            "[resourceId('Microsoft.ApiManagement/service', parameters('apiManagementServiceName'))]",
            "[resourceId(resourceGroup().name, 'Microsoft.Web/sites', parameters('indexerApp'))]"
        ],
        "properties": {
            "displayName": "[parameters('indexerApp')]",
            "apiRevision": "1",
            "description": "Import from \"[parameters('indexerApp')]\" Function App",
            "path": "[parameters('indexerApp')]",
            "protocols": [
                "https"
            ]
        }
    }
]

}

Upvotes: 0

Views: 2462

Answers (1)

Tom W
Tom W

Reputation: 5403

The resource that links directly to a Logic App, Function or other internal Azure resource is a backends (note plural):

{
  "name": "string",
  "type": "Microsoft.ApiManagement/service/backends",
  "apiVersion": "2019-01-01",
  "properties": {
    "title": "string",
    "description": "string",
    "resourceId": "string",
    "properties": {
      "serviceFabricCluster": {
        "clientCertificatethumbprint": "string",
        "maxPartitionResolutionRetries": "integer",
        "managementEndpoints": [
          "string"
        ],
        "serverCertificateThumbprints": [
          "string"
        ],
        "serverX509Names": [
          {
            "name": "string",
            "issuerCertificateThumbprint": "string"
          }
        ]
      }
    },
    "credentials": {
      "certificate": [
        "string"
      ],
      "query": {},
      "header": {},
      "authorization": {
        "scheme": "string",
        "parameter": "string"
      }
    },
    "proxy": {
      "url": "string",
      "username": "string",
      "password": "string"
    },
    "tls": {
      "validateCertificateChain": "boolean",
      "validateCertificateName": "boolean"
    },
    "url": "string",
    "protocol": "string"
  }
}

I believe that if you specify the resourceId property, the Azure infrastructure automatically wires up the other properties to your Function (or Logic App). The format of the "id" is not obvious, it expects a Management URI, the format of which is mentioned in this documentation

You then link this created backends to an operation using the operation's policy - this is a pain to express in ARM templates, but it should contain a policy expression:

<set-backend-service backend-id=\"[name of your backend]\"/>

An example of how these are linked together can be found at this linked sample. It features Service Fabric but it should be generally applicable to Functions as well.

Upvotes: 2

Related Questions