Wednesday, August 11, 2021

Import Azure Logic App to Azure API Management programmatically

Background: 

 

In Azure API Management service, we can import Azure Logic App as a new API manually in Azure PortalAs 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 instanceDevOps 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: 

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

 

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 

hailey_ding_0-1628670057181.png

 

 

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

 

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

 

  • Export the OpenAPI file of my Logic App(haileylogicapp) 

In my case, I downloaded an OpenAPI v3 JSON. 

 

hailey_ding_1-1628670057224.png

 

 

 

The original JSON file I have downloaded: 

hailey_ding_2-1628670057184.png

 

  • Optional Modify the JSON file with my new Logic APP configuration if my new logic app has changed since the previous import. I should be able to find the information from my new logic App designer blade. 

hailey_ding_3-1628670057241.png

 

For example, I have made the following modification in Json file as highlighted in red.  

hailey_ding_4-1628670057188.png

 

                

 

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 (haileylogicapp2in APIM: 

hailey_ding_5-1628670057227.png

 

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

 

  • Navigate to the Logic App Designers page, copy the Whole URL 

hailey_ding_6-1628670057244.png

 

I got the whole URL for my new Logic App: 

https://prod-43.westus.logic.azure.com:443/workflows/0c4108c775c34bf3ad7513440b89a419/triggers/request/paths/invoke?api-version=2016-10-01&sp=%2Ftriggers%2Frequest%2Frun&sv=1.0&sig=-WcznMep_1tZpqQrg8AdOmM8NfjbJB3UnvomN_pH8sc 

 

  • Copy the value of the sig part (-WcznMep_1tZpqQrg8AdOmM8NfjbJB3UnvomN_pH8sc), and store it inside Named Value inside my APIM service. By doing so, next time if I regenerate the access keys, I could simply modify the named value without the need to change my API in APIM. 

hailey_ding_7-1628670057228.png

 

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 

 

hailey_ding_8-1628670057230.png

 

 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 

hailey_ding_9-1628670057197.png

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. 

hailey_ding_10-1628670057233.png

 

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&amp;sp=/triggers/request/run&amp;sv=1.0&amp;sig={{haileylogicapp2-key}}" /> 

 

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

hailey_ding_11-1628670057236.png

 

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: 

hailey_ding_12-1628670057239.png

 

 

 

 

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