Thursday, July 29, 2021

Import Azure Function App to Azure API Management programmatically

 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 

hailey_ding_0-1627547163353.png

 

 

 

  1. 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. 

 

hailey_ding_1-1627547163445.png

 

 

 

  • 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 

hailey_ding_2-1627547163432.png

 

 

 

  • Modify the JSON file with the information: 

hailey_ding_3-1627547163454.png

 

 

  

 

  1. 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: 

hailey_ding_4-1627547163442.png

 

 

 

  1. 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. 

hailey_ding_5-1627547163458.png

 

 

 

Example below 

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

 

hailey_ding_6-1627547163460.png

 

 

 

  1. 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 

hailey_ding_7-1627547163405.png

 

 

 

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. 

hailey_ding_8-1627547163463.png

 

 

  1. 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: 

hailey_ding_9-1627547163465.png

 

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

hailey_ding_10-1627547163470.png

 

 

 

 

  1. Testing my changes 

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

Send a test call and get a success 200: 

hailey_ding_11-1627547163475.png

 

 

 

Posted at https://sl.advdat.com/3fa0hOd