How to specify the Azure function in the Powershell New-AzApiManagementBackend command?

Siegfried Heintze 1,906 Reputation points
2023-06-13T23:37:50.91+00:00

I'm following along with this blog/tutorial: https://techcommunity.microsoft.com/t5/azure-paas-blog/import-azure-function-app-to-azure-api-management/ba-p/2594810

I have created a powershell script that uses the commands in the tutorial.

When I add the azure function to the APIM using the portal, my javascript inside my azure static web page can successfully call my Azure AD authenticated Azure function via the APIM.

However, when I delete the API, named value and backend and run my powershell script to add them back, I get 404 errors when testing it via my static web page.

I compare the portal displays of the backend when creating the backend with the portal and creating the backend with powershell and I see a difference. The stuff in the red is missing!

User's image

I think I need to enhance Hailey's New-AzApiManagementBackend command to indicate that this is an azure resource that has a name and runtime URL.

Here is my attempt:

$backEndRunTimeURL="https://myazurefunc.azurewebsites.net/api"
$backEndName="myazurefunc"
$backend = New-AzApiManagementBackend -Context $ApiMgmtContext -BackendId $backEndName -Url     $backEndRunTimeURL -Protocol http -Credential $credential -Title "myazurefunc"-Description $backEndName -ResourceId "https://myazurefunc.azurewebsites.net"

When I go to the portal to confirm that the script has correctly created everything correctly, I see 3 problems:

  1. I get this message when I look at the properties for the backend the stuff that is supposed to be in the red box (the azure resource and the runtime URL) are blank and I get this message ("newtaskmgrauthsvcs" is not what I specified in my powershell New-AzApiManagementBackend command):

User's image

  1. My new API requires a subscription. What powershell command can I use to uncheck the box that says it requires a subscription?
  2. It is not part of my product called unlimited. What powershell command can I use to add it my product called Unlimited?

Also: Is it safe to publish my azure function name in these forums? It might be helpful if some MS employees wanted to look at my azure resources from the inside.

Thanks

Siegfried

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,447 questions
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,911 questions
Windows for business Windows Server User experience PowerShell
{count} votes

Accepted answer
  1. Saurabh Sharma 23,846 Reputation points Microsoft Employee Moderator
    2023-06-23T20:35:00.0533333+00:00

    Hi Siegfried Heintze,

    Here are some pointers to resolve issues which you are facing -

    1. Once you are done with the import, you can use Add-AzApiManagementApiToProduct cmdlet to add product to the imported API. You also need to pass the productid to this cmdlet which you can get from the portal (or you can use Get-AzApiManagementProduct to get all products and choose the one which you need)-

    For example -

    To set the API product to Starter, you can use the Id - "0123456789" as it appears on Azure Portal.

    User's image

    $Api = Get-AzApiManagementApi -Context $ApiMgmtContext -Name {ImportedApiname} 
    Add-AzApiManagementApiToProduct -Context $ApiMgmtContext -ProductId "0123456789" -ApiId $Api.ApiId 
    
    1. Now, unfortunately, the API cmdlets lacks the feature to update the "Subscription Required" Flag to False. Using the cmdlet "Set-AzApiManagementApi" does not work either. However, you can use the below alternative PowerShell code to update the subscription required flag to false.
    $Api = Get-AzApiManagementApi -Context $ApiMgmtContext -Name {ImportedApiname}
    $Api.SubscriptionRequired=$false ## This will make the subscription flag as false
    Set-AzApiManagementApi -InputObject $Api -Name $Api.Name -ServiceUrl $Api.ServiceUrl -Protocols $Api.Protocols
    

    I have tested this and it works as expected.

    1. Regarding the resource error, which you are getting on Azure Portal, you can use the New-AzApiManagementBackend cmdlet without the -ResourceId parameter while adding the API backend.
    $credential = New-AzApiManagementBackendCredential -AuthorizationHeaderScheme basic -Header @{"x-functions-key" = @("{{funcapim-key}}")} 
    $backend = New-AzApiManagementBackend -Context  $ApiMgmtContext -BackendId FuncBackend -Url 'https://funcapim.azurewebsites.net/api' -Protocol http -Title "FuncBackend" -SkipCertificateChainValidation $true -Credential $credential -Description "Function App API Backend"
    

    Once added, the backend will be set with the Runtime URL under the Custom URL properties on Azure Portal and you don't specifically need to update Azure Resource and RunTimeUrl properties as long as the Named values are configured correctly with the function key. User's image

    I have tested this with the custom URL and able to call the Azure function API from APIM after importing and following the above-mentioned steps. Please check the below screenshot for your reference.User's image

    Hope this helps. Please let me know if you still have any issues and I would be happy to continue the conversation to get you unblocked.

    Thanks

    Saurabh


    Please consider hitting Accept Answer button. Accepted answers help community as well.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Aditya Shankar Khorne 25 Reputation points
    2023-06-27T16:07:42.2133333+00:00

    To specify an Azure Function in the New-AzApiManagementBackend command in PowerShell, you need to provide the necessary parameters for the Azure Function backend. Here's an example of how you can do it:

    # Import the Az module if not already imported
    Import-Module Az
    # Connect to your Azure account
    Connect-AzAccount
    # Define the variables for your Azure Function backend
    $resourceGroupName = "YourResourceGroupName"
    $apiManagementName = "YourApiManagementInstanceName"
    $backendUrl = "https://yourfunctionapp.azurewebsites.net/api/yourfunctionname"
    # Create the Azure Function backend
    New-AzApiManagementBackend -Context $apiManagementName -Url $backendUrl -ResourceGroupName $resourceGroupName -BackendId "your-function-backend"
    
    

    In the above example:

    1. You need to replace "YourResourceGroupName" with the name of your Azure resource group where your API Management instance is located.
    2. Replace "YourApiManagementInstanceName" with the name of your API Management instance.
    3. Modify "https://yourfunctionapp.azurewebsites.net/api/yourfunctionname" with the URL of your Azure Function. Make sure to include the appropriate endpoint and function name.
    4. The -BackendId parameter is optional and allows you to specify a custom ID for the backend.

    By executing the above script in PowerShell, you will create an Azure Function backend in your Azure API Management instance using the New-AzApiManagementBackend command.

    1 person found this answer helpful.
    0 comments No comments

  2. Limitless Technology 44,751 Reputation points
    2023-06-14T08:32:35.5933333+00:00

    Hello Siegfried,

    Thank you for your question and for reaching out with your question today.

    I understand that you are encountering a few issues when using PowerShell to create the Azure API Management backend and API. Let's address each problem individually:

    1. Missing information in the backend properties: The missing information in the backend properties might be related to how you are defining the backend in your PowerShell script. To ensure that the required information is populated correctly, you can modify your script as follows:
    
    $backend = New-AzApiManagementBackend -Context $ApiMgmtContext -BackendId $backEndName -Url $backEndRunTimeURL -Protocol http -Credential $credential -Title "myazurefunc" -Description $backEndName -Properties @{"resourceId"="https://myazurefunc.azurewebsites.net"; "runtimeUrl"=$backEndRunTimeURL}
    
    

    By using the -Properties parameter, you can provide additional properties, including the "resourceId" and "runtimeUrl", to the backend.

    1. Unchecking the subscription requirement for the API: To remove the subscription requirement for your API, you can use the following PowerShell command:
    
    Set-AzApiManagementApi -Context $ApiMgmtContext -ApiId "your-api-id" -SubscriptionRequired $false
    
    

    Replace "your-api-id" with the ID or name of your API.

    1. Adding the API to a product: To add the API to your product called "Unlimited", you can use the following PowerShell command:
    
    Add-AzApiManagementProductApi -Context $ApiMgmtContext -ProductId "your-product-id" -ApiId "your-api-id"
    
    

    Replace "your-product-id" with the ID or name of your product, and "your-api-id" with the ID or name of your API.

    Regarding the safety of publishing your Azure function name, it's generally recommended to avoid sharing sensitive information, such as specific resource names, in public forums. If Microsoft employees need to access your Azure resources, they would typically request that information privately for security reasons.

    Make sure to replace "your-api-id" and "your-product-id" with the actual IDs or names of your API and product in the provided PowerShell commands.

    I used AI provided by ChatGPT to formulate part of this response. I have verified that the information is accurate before sharing it with you.

    If the reply was helpful, please don’t forget to upvote or accept as answer.

    Best regards.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.