How to use az command in azure function

eishi 61 Reputation points
2024-10-16T05:51:50.1+00:00

I am trying to delete a route (test) in a routetable from a function created with azure function,

When deleting the route, I get the following authentication error and cannot delete it.

How can I generate a token from within an azure function using the az command?

powershell code


$token = (az account get-access-token | ConvertFrom-Json)

$token = $token.accessToken

error message


[Error] ERROR: The term 'az' is not recognized as a name of a cmdlet, function, script file, or executable program.

Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Exception :

Type        : System.Management.Automation.CommandNotFoundException

ErrorRecord : 

    Exception             : 

        Type    : System.Management.Automation.ParentContainsErrorRecordException

        Message : The term 'az' is not recognized as a name of a cmdlet, function, script file, or executable program.

                  Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

        HResult : -2146233087

    TargetObject          : az

    CategoryInfo          : ObjectNotFound: (az:String) [], ParentContainsErrorRecordException
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
{count} votes

Answer accepted by question author
  1. Anonymous
    2024-10-17T05:43:18.2366667+00:00

    Hi @eishi,

    Welcome to the Microsoft Q&A Platform! Thank you for asking your question here.

    You want to manage Azure resources in Azure Function, but it is not advisable to directly use 'az' cli commands within a function due to security limitations. We can try alternate secure and effective approaches if you can consider.

    • We can use ARM templates. We can define the deployment or deletion logic for your route in an ARM template (JSON file) and use the Azure Functions SDK or Azure PowerShell cmdlets within your function code to deploy or delete the ARM template.
    • Leverage the Azure Resource Manager APIs (REST or SDK) directly from your function code to perform resource management actions. If you find this answer helpful, please click "Comment" and kindly upvote it.
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

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