Using AZ CLI in a PowerShell Function App

Jonathan Lewis 20 Reputation points
2023-06-21T02:39:45.63+00:00

I am trying to use the AZ CLI in a PowerShell Function App to automatically create a new Subscription each time a new client registers to use my services. It looks like I should be able to do it however, it is just not working. I have tried different approaches. I have tried to configure the requirements.psd1 as specified by uncommenting the 'Az' = '10.*' line in the file but it is still not working.

Is there a tutorial or some documentation that shows me how to get the AZ CLI configured and running in a PowerShell Function App? That would be helpful thank you.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,867 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,970 questions
{count} votes

Accepted answer
  1. AirGordon 7,145 Reputation points
    2023-06-21T07:23:55.94+00:00

    The requirements.psd1 is for installing PowerShell modules. The Az module in this file is not the Azure CLI. Many users to want to use the Azure CLI with a PowerShell function and the best documentation and experiences of other users doing this is in the GitHub issue.

    It's not a default capability, and many use cases will be a better fit for an Azure Container Instance or Azure CLI Deployment Script.

    There is a verified workaround described in this comment :https://github.com/Azure/azure-functions-powershell-worker/issues/221#issuecomment-546039332

    It involves creating a new AZ alias in the profile.ps1 file that targets local storage used by the function app for the Azure CLI files. `

    Set-Alias -Name az -Value "d:\home\data\CLI2\wbin\az.cmd"
    

    This does require manually uploading the Azure CLI files to the File Share used by the PowerShell functionapp. I did this using the Azure Storage Explorer, but it'll be much faster to zip the files, upload the zip file and then unzip.

    The Azure CLI files can be found locally on your workstation (C:\Program Files (x86)\Microsoft SDKs\Azure). The entire CLI2 directory should be uploaded to the data directory.

    User's image

    I created a very simple http triggered function that simply ran az --version. You can see from the screengrab it works.

    User's image

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    1 deleted comment

    Comments have been turned off. Learn more

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.