Background:
In Azure API Management service, we can import Azure Logic App as a new API manually in Azure Portal. As referenced in this document about Import a Logic App as an API
However, sometimes we wish to automate the import process of Azure Logic App to Azure API Management service (APIM). For instance, DevOps engineers wish to create a pipeline to automate the import process of the Azure Function app to APIM.
You may notice that there isn’t any PowerShell command or Rest API available for us to use to automatic the import process at present. Usually, the command APIM supports is to import swagger file or WSDL file.
There is a certain workaround available now which enables us to achieve the automation process. Firstly, we need to create an open API definition file for the targeted Azure Logic 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 Logic App to APIM automatically, with the steps below:
Pre-requirements:
Steps:
1.Importing an Azure Logic 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-logic-app-as-api
After importing, I can see the Logic App in the API list, named with haileylogicapp
2.Generating an Open API definition file for future imports.
First of all, I will need to export/download an OpenAPI definition file for my existing Logic App in APIM, then I could modify the contents inside and save it for future automated importing processes.
In my case, I downloaded an OpenAPI v3 JSON.
The original JSON file I have downloaded:
For example, I have made the following modification in Json file as highlighted in red.
3.Importing the new Logic App to APIM by using command Import-AzApiManagementApi
As I already have an OpenAPI file with my new Logic 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\haileylogicapp2.json" -Path $myPath
After running the command, I can see my new Logic App (haileylogicapp2) in APIM:
4.Adding the authorization for the Logic APP in APIM with the Named Value
As Azure Logic App needs an access key for HTTP requests, we will need to add access key information in the request URL from the APIM side. In order to do that, we need a named value to store the access key of my new Logic App(haileylogicapp2).
I got the whole URL for my new Logic App:
To add the named value, I run this New-AzApiManagementNamedValue cmdlet.
Example below:
New-AzApiManagementNamedValue -Context $ApiMgmtContext -NamedValueId “haileylogicapp2-key” -Name “haileylogicapp2-key” -Value “-WcznMep_1tZpqQrg8AdOmM8NfjbJB3UnvomN_pH8sc”
5.Adding a backend service in APIM using New-AzApiManagementBackend
I need add the new Logic App into my APIM Backends.
To achieve this, I can use the PowerShell command New-AzApiManagementBackend
My example below:
$apimContext = New-AzApiManagementContext -ResourceGroupName "APIMtest" -ServiceName "coolhailey"
$backend = New-AzApiManagementBackend -Context $apimContext -BackendId haileylogicapp2 -Url 'https://prod-43.westus.logic.azure.com:443/workflows/0c4108c775c34bf3ad7513440b89a419/triggers’ -Protocol http -Title "haileylogicapp2" -Description "my new Logic App 2"
After run the above commands, I can see a new backend created, named with haileylogicapp2.
6.Setting the backend service and request URL with the inbound policy
I will need to set the logic app as the backend in the APIM inbound policy. At the same time, I need to append the authorization token to the request URL.
To achieve that, I can use the set-backend-service and rewrite-uri:
<set-backend-service backend-id="haileylogicapp2"/>
<rewrite-uri template="/request/paths/invoke/?api-version=2016-10-01&sp=/triggers/request/run&sv=1.0&sig={{haileylogicapp2-key}}" />
Then I ran the Set-AzApiManagementPolicy cmdlet. Example below:
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:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.