Close

Deploy Service on API Management

My colleague Eldert Grootenboer has written an excellent series of blog posts on deploying Azure API Management via Visual Studio Team Services (VSTS). This is a short summary.

API Management can be deployed in three steps.

  • Create API Management instance via ARM deployment.
  • Create users, groups and products via ARM deployment.
  • Create virtual service that you want to expose via API Management.

For now I have performed the two first steps manually, because they are one time operations. I’m not using a specific Product, but I’m just using the Starter product. After performing the first two steps, let’s turn our attention to the third step. Creating or updating API’s can be done directly in the ARM template or it can be done in the Azure Portal. I used the Azure Portal, because that seems an easier approach to me.

Next steps:

  • You can download a tool named the ARM Template Creator at this download link.
  • Compile the ARM Template Creator solution.
  • Create a PowerShell script that uses the ARM Template Creator to generate an ARM template for the API you want to expose via API Management.

The PowerShell script looks like this:

#if you have problem with execution policy execute this in a administrator runned powershell window.
Set-ExecutionPolicy -ExecutionPolicy Unrestricted

Import-Module “.\APIManagementTemplate.dll”

#Set the name of the API Mangement instance
$apimanagementname = ‘tulipexpress’

#Set the resource group
$resourcegroupname = ‘tlx-apimgmt’
#Set the subscription id
$subscriptionid = ‘guid’
#Set the tenant to use when login ing, make sure it has the right tennant
$tenant = ‘tulipexpress.onmicrosoft.com’

#optional set filter for a specific api (using standard REST filter, with path we can select api based on the API path)
#$filter = “path eq ‘api/v1/currencyconverter'”
$filter = “path eq ‘test'”

#setting the output filename
$filenname = ‘D:\’ + $apimanagementname + ‘.json’

Get-APIManagementTemplate -APIFilters $filter -APIManagement $apimanagementname -ResourceGroup $resourcegroupname -SubscriptionId $subscriptionid -TenantName $tenant -ExportPIManagementInstance $false | Out-File $filenname

After running the PowerShell script via the PowerShell ISE (run as administrator), the ARM template is created. The generated ARM template (json) contains a few important parameters that define the name of the API Management instance, the name of the virtual service and the name of the backend service. Note that the template also contains a few hard-coded values (like the display name of the virtual service). Replace those with parameterized values. Obviously you will also have to create a parameter file for each environment.

When using the ARM template for the first time, I could perfectly deploy the ARM template via an ARM Resource Group deployment step (don’t mind the somewhat confusing name) in VSTS. In this particular case, a policy was used to set the backend URL. That didn’t work, so I replaced it with the normal solution that sets the backend URL via the Settings tab.

A fragment of the ARM template where the service is created is shown below:

“resources”: [
{
“comments”: “Generated for resource /subscriptions/3c855a42-60cd-42c5-b343-f46528d6a24b/resourceGroups/FEYE-TST-API/providers/Microsoft.ApiManagement/service/feye-api-mgt-tst/apis/feye-api-external-tst”,
“type”: “Microsoft.ApiManagement/service/apis”,
“name”: “[concat(parameters(‘api-mgt_name’), ‘/’ ,parameters(‘api__name’))]”,
“apiVersion”: “2017-03-01”,
“properties”: {
“displayName”: “[parameters(‘api_name’)]”<- parameterized, not hard-coded
“apiRevision”: “[parameters(‘api_apiRevision’)]”,
“description”: “”,
“serviceUrl”: “[parameters(‘api_serviceUrl’)]”, <- url to the backend-service (no policy)
“path”: “test”,
“protocols”: [
“https”
],
“authenticationSettings”: {
“oAuth2”: null,
“openid”: null
},
“subscriptionKeyParameterNames”: {
“header”: “Ocp-Apim-Subscription-Key”,
“query”: “subscription-key”
},
“isCurrent”: “[parameters(‘feye-api-external-tst_isCurrent’)]”
}

Leave a Reply

Your email address will not be published. Required fields are marked *