Background:
In Azure API Management service, we can import Azure Function Apps as new APIs or appending them to existing APIs manually in Azure Portal. As 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 instance, DevOps 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:
- An APIM service: Create a new Azure APIM
- A function App ready to use: Getting started with Azure Functions
Steps:
- 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
- Generating an Open API definition file for the future imports.
First of all, I will need to export/download an OpenAPI definition file for my existing function App in APIM, then 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 information:
- 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 (haileyfunctionapp3) in APIM:
- 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.
- Once we get the host key, we can run this New-AzApiManagementNamedValue cmdlet.
Example below:
New-AzApiManagementNamedValue -Context $ApiMgmtContext -NamedValueId “functionapptest3-key” -Name “functionapptest3-key” -Value $functionAPPKey
- 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.
- 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.
- Testing my changes
After all these steps above, I go back and verified my import.
Send a test call and get a success 200:
Posted at https://sl.advdat.com/3fa0hOd