In API Management, you can automate the creation of subscriptions and their corresponding subscription keys. There are several reasons why you would like to do this:
- Configuration as code: the access control to your APIs is managed and automated through automated release pipelines
- You want to rely on your source control to restore your API Management instance in case of a disaster (e.g. when using Consumption tier that has no backup feature)
- You want to determine the value of the subscription keys yourself (e.g. using an already existing unique identifier)
- You want to synchronize subscription keys across multiple API Management instances (e.g. when having a multi-region setup within a non-Premium tier)
Not many people are aware that you can choose the value of the subscription keys yourself. This is because this feature is only available through scripting and not within the Azure Portal. Let’s have a look at the different options to script API Management subscriptions. At the time of writing, the Azure CLI module for API Management is under development and has not support yet for subscriptions.
Azure Powershell
The following snippet creates a new subscription for the Conference API. Find more info over here.
#Log in with you user Connect-AzAccount #Install the API Management PowerShell Module Install-Module Az.ApiManagement #Set API Management context $apimContext = New-AzApiManagementContext -ResourceGroupName "<resource group name>" -ServiceName "<apim service name>" #Create a new subscription New-AzApiManagementSubscription -Context $apimContext -Name "Auto-Subscription" -Scope "/apis/conference-api" -PrimaryKey "key-1" -SecondaryKey "key-2"
Azure Resource Manager
The following ARM template is a starting point to create an API Management subscription. This one is scoped to all APIs. Find more info over here.
{ "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "apim_name": { "type": "String" } }, "resources": [ { "apiVersion": "2019-01-01", "type": "Microsoft.ApiManagement/service/subscriptions", "name": "[concat(parameters('apim_name'), '/arm-subscription')]", "properties": { "scope": "/apis", "displayName": "ARM Subscription", "primaryKey": "123456789", "secondaryKey": "987654321", "allowTracing": "true" } } ] }
Did you find this useful? Thanks for sharing!
Cheers,
Toon