Blog Post

Azure PaaS Blog
4 MIN READ

Import Azure Function App to Azure API Management programmatically

hailey_ding's avatar
hailey_ding
Icon for Microsoft rankMicrosoft
Jul 29, 2021

 Background: 

 

In Azure API Management service, we can import Azure Function Apps as new APIs or appending them to existing APIs manually in Azure PortalAs referenced in this document about importing an Azure function App as an API in Azure API Management. 

 

However, sometimes we wish to automate the import process of Azure Function App to Azure API Management service (APIM). For instanceDevOps engineers wishes to create a pipeline to automate the import process of Azure Function app to APIM. 

 

You may notice that there isn’t any PowerShell command or Rest API available to import Azure Function app into APIM at present. Usually, the command APIM supports is to import swagger file or WSDL file. 

 

There is a certain work around available now which enables us to achieve the automation process. Firstly we need to create an open API definition file for the targeted Azure Function app, and then we can import this swagger file into our APIM using PowerShell command Import-AzApiManagementApi 

 

In this blog, we will walk through the details on importing Function App to APIM automatically, with the steps below: 

  • Importing an Azure Function App to Azure API Management manually 
  • Generating an Open API definition file for the future imports 
  • Importing the new Function App to APIM by using command Import-AzApiManagementApi 
  • Adding the authorization for the Function APP in APIM with the Named Value    
  • Adding a backend service in APIM using New-AzApiManagementBackend  
  • Setting the backend service with the inbound policy   
  • Testing the new API imported from Function App 

 

Pre-requirements: 

Steps: 

1. Importing an Azure Function App to Azure API Management manually  in Azure Portal 

Follow the steps in this document: https://docs.microsoft.com/en-us/azure/api-management/import-function-app-as-api#add-new-api-from-azure-function-app 

 

After importing, I can see the function App in the API list, named with haileyfunctionapp 

 

 

 

2. Generating an Open API definition file for the future imports 

First of allI will need to export/download an OpenAPI definition file for my existing function App in APIMthen I could modify the contents inside and save it for future automated importing process. 

 

  • Export the OpenAPI file of my function App(haileyfunctionapp) 

In my case, I downloaded an OpenAPI v3 JSON. 

 

  • Then modify the OpenAPI JSON file. A few parts needed to be changed as below: 

the Paths:  this should be the path for my function. I should be able to find this path in my function App setting page 

Modify the JSON file with the Path information: 

 

3. Importing the new Function App to APIM by using command Import-AzApiManagementApi   

 

As I already have an OpenAPI file with my new Function App configurations, now I am ready to import it. I will use Import-AzApiManagementApi command. 

 

My example below 

Import-AzApiManagementApi -Context $ApiMgmtContext -SpecificationFormat "OpenApiJson"  -SpecificationPath "MYLOCALFILEPATH\haileyfunctionapp3.json" -Path $myPath 

 

After running the command, I am able to see my new Function App (haileyfunctionapp3in APIM: 

4. Adding the authorization for the Function APP in APIM with the Named Value  

As Azure function App needs a host key for all the HTTP requests, we will need to pass the host key in the header from APIM side. In order to do that, we need a named value to store the host key of my new Function App(haileyfunctionapp3). 

 

  • Navigate to the Function App, and get the host key. 

Example below 

New-AzApiManagementNamedValue -Context $ApiMgmtContext -NamedValueId “functionapptest3-key”  -Name “functionapptest3-key”  -Value $functionAPPKey 

 

 

 5. Adding a backend service in APIM using New-AzApiManagementBackend 

As I’ve got a named value to store my Function App key, now I can add the new Function App into my APIM Backends with the named value as the credentials. 

To achieve this, I can use the PowerShell command New-AzApiManagementBackend 

My example below: 

$apimContext = New-AzApiManagementContext -ResourceGroupName "APIMtest" -ServiceName "coolhailey" 

 

$credential = New-AzApiManagementBackendCredential -AuthorizationHeaderScheme basic -Header @{"x-functions-key" = @("{{functionapptest3-key}}")} 

 

$backend = New-AzApiManagementBackend -Context  $apimContext -BackendId haileyfunctionapp3 -Url 'https://haileyfunctionapp3.azurewebsites.net/api' -Protocol http -Title "haileyfunctionapp3"  -Credential $credential -Description "my new Function App 3" 

After run the above commands, I can see a new backend created, named with haileyfunctionapp3. 

 

 

6. Setting the backend service with the inbound policy 

I will need to set the function app as the backend in the APIM inbound policy. 

To achieve that, I can use the set-backend-service policy: 

<set-backend-service backend-id="haileyfunctionapp3"/> 

 

Then I ran the Set-AzApiManagementPolicy cmdlet. Example below: 

 

After the changes, navigate to the API and open the policy, I can see the <set-backend-service> part been added. 

7. Testing my changes 

After all these steps above, I go back and verified my import. 

Send a test call and get a success 200: 

 

 

 

Updated Jul 29, 2021
Version 2.0

2 Comments

  • adhdiwill's avatar
    adhdiwill
    Copper Contributor

    I've been looking for similar. Ideally I was looking to use ARM templates to either complete the API Import of a function app step, including creating a host key for APIM and storing this as a named value in APIM, associating that with a backend in APIM. Actually, I think when I tried the manual step, the host key was created and updated as a named value in APIM, but this wasn't actually updated in the backend for the function app. Also annoying that in the Azure Portal, where you can go to an APIM service and on the overview page get the json view of the resource, you don't get a similar view for extension resources e.g. backends, apis, products, policies, subscriptions . . etc.

     

    Anyway, I came across a series of relevant posts on github:

     

    https://github.com/Azure/azure-functions-host/issues/4570 

    https://github.com/Azure/azure-functions-host/issues/3994 

     

    Both from 2019, as a result, I found the links below for the REST API calls, if they're of any use?

     

    https://learn.microsoft.com/en-us/rest/api/appservice/web-apps/list-host-keys 

    https://learn.microsoft.com/en-us/rest/api/appservice/web-apps/create-or-update-host-secret 

  • lgp1985's avatar
    lgp1985
    Copper Contributor

    how can I programmatically retrieve that App Key of step 4? so I can have a pipeline performing it.