Configure Azure API Management with PowerShell

Recently I needed to create Azure API Management Service, API Product and APIs inside the service. We’re doing this in Azure DevOps pipelines. So we used a basic ARM Template to deploy the service, and then we wanted to use the plugin here. Which would also work well, except that at present doesn’t support V2 Azure Functions. So we ended up using the Azure API Management PowerShell module to do the remaining configuration.

Service Deployment

As mentioned above we used a template to deploy the instance. You can find that template here.

It works well and it will take 20-40 minutes to deploy.

The PowerShell way to deploy the instance is with the New-AzApiManagement command.

New-AzApiManagement -resourcegroupname "yourresourcegroup" -Name "SandlotAPI" -Location "Central US" -Organization "Sandlot" -AdminEmail "admin@sandlot.com" 

The same parameters are used for PowerShell and the ARM Template.

API Management Context

When doing anything with API Management the PowerShell commands all have a required parameter called context.


$Api = Get-AzAPIManagement -ResourceGroupName "yourresourcegroup" -Name "SandlotAPI"

$apiContext = New-AzApiManagementContext -ResourceGroupName $api.resourcegroupname -ServiceName $api.name

To get the API Management context you’ll need the Resource Group Name and the API Management Service Name. We’ll use Get-AzAPIManagement to get the API Management service, which will have those fields in the returned object. Technically you don’t need to give the command the resource group name or the API Management service name, it will find it. But if you have more than one then you need to specify which one you want.

 

Create API Management Product

In API Management a Product can have multiple APIs, and it sets the terms of use, and whether or not a subscription is required.


$newProduct = New-AzApiManagementProduct -Context $apiContext -ProductId "123" -Title "Integrations" -Description "API Product for Integrations" -State "Published" 

To create a new Product we’ll use the Context from above, and use New-AzApiManagementProduct.

Create API


$azwebapp = get-azwebapp -ResourceGroupName yourresourcegroup

$uri = "https://" + $azwebapp.defaulthostname

$newapi = New-AzApiManagementApi -context $apiContext -name "azure function" -ServiceUrl $uri -protocols @('http','https') -path "testapi"

When creating an API you have to provide a URI. In our case we were using an Azure Function. So I get the azure function and then used the default hostname after appending HTTP:// to it. The path gets appended as part of the command.

 

 

 

Create API Operation

Every API needs an operation. In this case we give it a POST operations using New-AzAPIManagementOperation.

New-AzApiManagementOperation -Context $apiContext -ApiId $newapi.apiid -OperationId "operation1" -Name "HttpEndPoint" -Method "POST" -UrlTemplate "/HttpEndPoint" 

Note, we’re using the output of our new api created in the previous step using $newapi

Add to Product

And finally, we’ll add our API to our product.

add-AzApiManagementApiToProduct -context $apiContext -ProductId $newproduct.productid -apiid $newapi.id

Using the variables from New-AzApiManagementProduct which we assigned to $newproduct and $newapi we use the productId and api.Id to add the API to the product.

 

API Creation Review

If you’ve followed along this far we have:

  • created an API Management Service
  • API Product
  • An API
  • gave that API an Operation
  • Added the API to the Product

API added to Product

azure api management powershell

API with its operation

azure api management powershell

Azure DevOps Pipelines

If you can definitely try to use this Azure Devops Plugin here. As mentioned previously, because we were doing V2 Azure Functions, that plugin does not presently support it. We ended up using PowerShell. But using PowerShell in Pipelines you’ll need to add some logic around detecting the API if it wasn’t deleted before you do a new build and release. The create PowerShell commands aren’t smart enough to detect that the API already exists.

 

There are a number of configuration options and corresponding commands to Configure Azure API Management with PowerShell. You can find all the commands here.

 

Leave a Comment