azure-functions-powershell-worker icon indicating copy to clipboard operation
azure-functions-powershell-worker copied to clipboard

azure cli from azure function powershell

Open dariuszbz opened this issue 6 years ago • 57 comments
trafficstars

How to run azure cli from azure function powershell?

dariuszbz avatar May 13 '19 14:05 dariuszbz

Can you use the Azure PowerShell module instead?

daxian-dbw avatar May 14 '19 05:05 daxian-dbw

Not from functions. When I'm loading azurerm module for SQL scaling (thats my task) and than login/connected nf to azure I'm getting error message.

On Tue, 14 May 2019, 06:50 Dongbo Wang, [email protected] wrote:

Can you use the Azure PowerShell module instead?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Azure/azure-functions-powershell-worker/issues/221?email_source=notifications&email_token=AEVINAV63GQNJIQXPMGA5FDPVJHKLA5CNFSM4HMQGFKKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVKKXCI#issuecomment-492088201, or mute the thread https://github.com/notifications/unsubscribe-auth/AEVINAUKDXLQHAWLMR72KDTPVJHKLANCNFSM4HMQGFKA .

dariuszbz avatar May 14 '19 07:05 dariuszbz

Btw. it works (4ex: Get-AzContext ) when running from vscode, but not after deployment into azure function.

On Tue, 14 May 2019 at 06:50, Dongbo Wang [email protected] wrote:

Can you use the Azure PowerShell module instead?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Azure/azure-functions-powershell-worker/issues/221?email_source=notifications&email_token=AEVINAV63GQNJIQXPMGA5FDPVJHKLA5CNFSM4HMQGFKKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVKKXCI#issuecomment-492088201, or mute the thread https://github.com/notifications/unsubscribe-auth/AEVINAUKDXLQHAWLMR72KDTPVJHKLANCNFSM4HMQGFKA .

dariuszbz avatar May 14 '19 10:05 dariuszbz

Ok I found: first on your VM/workstation: Connect-AzAccount Save-AzContext

copy context json file to your azure function file system.

in your functions: Import-AzContext -Path "D:\home\site\wwwroot\\azctx.json" $az = Get-AzContext write-host $az

works.

On Tue, 14 May 2019 at 11:19, Dariusz Bzówka [email protected] wrote:

Btw. it works (4ex: Get-AzContext ) when running from vscode, but not after deployment into azure function.

On Tue, 14 May 2019 at 06:50, Dongbo Wang [email protected] wrote:

Can you use the Azure PowerShell module instead?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Azure/azure-functions-powershell-worker/issues/221?email_source=notifications&email_token=AEVINAV63GQNJIQXPMGA5FDPVJHKLA5CNFSM4HMQGFKKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVKKXCI#issuecomment-492088201, or mute the thread https://github.com/notifications/unsubscribe-auth/AEVINAUKDXLQHAWLMR72KDTPVJHKLANCNFSM4HMQGFKA .

dariuszbz avatar May 14 '19 12:05 dariuszbz

Did you enable MSI for your FunctionApp? Once you enable MSI and assign the necessary roles to your identity object, the default profile.ps1 will log you in Azure automatically.

daxian-dbw avatar May 14 '19 17:05 daxian-dbw

It didn't work.

I can try any step by step guide if you send me step by step instructions.

The only way it works for me is the one I described.

On Tue, 14 May 2019, 18:05 Dongbo Wang, [email protected] wrote:

Did you enable MSI for your FunctionApp? Once you enable MSI and assign the necessary roles to your identity object, the default profile.ps1 will log you in Azure automatically.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Azure/azure-functions-powershell-worker/issues/221?email_source=notifications&email_token=AEVINASTU533Q37LCGD5T33PVLWODA5CNFSM4HMQGFKKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVMEMYQ#issuecomment-492324450, or mute the thread https://github.com/notifications/unsubscribe-auth/AEVINATGTJGGPQQODMRQYPDPVLWODANCNFSM4HMQGFKA .

dariuszbz avatar May 14 '19 17:05 dariuszbz

az cli is not available currently out the box in the functions powershell environment. This is a feature ask and we are looking into it.

anirudhgarg avatar May 28 '19 22:05 anirudhgarg

In the meantime, the following workaround has been verified:

  • Deploy the content of the local "C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2" folder to the app (e.g. upload it to the file share for the function app using storage explorer).
  • Add an alias for the az command to this path in the profile.ps1: Set-Alias -Name az -Value "d:\home\data\CLI2\wbin\az.cmd"

After that, the following function runs successfully:

param($Request, $TriggerMetadata)
az login --identity
$FunctionApps = az functionapp list | ConvertFrom-Json
$FunctionApps.Name

AnatoliB avatar Oct 24 '19 18:10 AnatoliB

@AnatoliB can you elaborate on where to to upload the CLI2 folder?

You said:

to the file share for the function app using storage explorer

I am using SE and I do not see a file share.

VictorioBerra avatar Feb 18 '20 21:02 VictorioBerra

@VictorioBerra When you create a PowerShell Function app, a storage account is normally created in the same resource group, and this storage account contains an automatically created file share. You can access it in multiple ways, for example:

  1. Using Storage Explorer: In the Portal, find the Function app and click on the Overview tab, then follow the link to the resource group. You will find a storage account in this resource group. Open this storage account in Storage Explorer, go to file shares and find a share with an auto-generated name. I recommend uploading CLI2 to the data directory.
  2. Using Kudu: In the Portal, find the Function app and click Platform features -> Advanced tools (Kudu), then select Debug console -> PowerShell, and then you can drag-and-drop zip files to be uploaded there.

AnatoliB avatar Feb 19 '20 01:02 AnatoliB

@AnatoliB I have no file shares in mine. Does it matter that i'm hosting an existing app service plan on Windows? As for Kudu, if I upload the CLI2 as a zip I assume I simply unzip using a PS command?

VictorioBerra avatar Feb 19 '20 02:02 VictorioBerra

@VictorioBerra I see. Yes we use a different kind of file storage on app service plan, so it is not on Azure Files, you are right. In this case, the Kudu method should still work. When you drag-and-drop a zip file into the browser window, Kudu will automatically unzip the content into the current location, preserving the directory structure in the zip.

AnatoliB avatar Feb 19 '20 02:02 AnatoliB

@AnatoliB I must have done something wrong with Kudu, I zipped the CLI2 directory, dragged the zip into my browser, it uploaded successfully but never extracted. This was about 30 minutes ago now image

VictorioBerra avatar Feb 19 '20 02:02 VictorioBerra

I was able to unzip manually in the PS console by just using unzip CLI2.zip. Its not done unzipping yet but it seems to be working.

EDIT: No luck. When I tried to do az login --identity in my run.ps1 I got the following error:

Exception: The term 'd:\home\data\CLI2\wbin\az.cmd' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

I have Set-Alias -Name az -Value "d:\home\data\CLI2\wbin\az.cmd" in my profile.ps1

VictorioBerra avatar Feb 19 '20 03:02 VictorioBerra

I gave up and used the REST API to accomplish my task. IMO the PS Functions are severely lacking without the AZ CLI. The Azure Powersehll Module is not enough. For example, what is the equivalent of this in APM? https://docs.microsoft.com/en-us/cli/azure/webapp/webjob/triggered?view=azure-cli-latest#az-webapp-webjob-triggered-list

@anirudhgarg what is the ETA of the feature ask? Do you have a link we can use to track the progress of the feature. Like on https://feedback.azure.com/?

VictorioBerra avatar Feb 20 '20 15:02 VictorioBerra

@VictorioBerra by now it looks like the only way. see: anirudhgarg commented on 28 May 2019 • az cli is not available currently out the box in the functions powershell environment. This is a feature ask and we are looking into it.

dariuszbz avatar Feb 24 '20 10:02 dariuszbz

I am looking to use az cli for some features that are not available in PS yet. https://docs.microsoft.com/en-us/cli/azure/container?view=azure-cli-latest#az-container-start Please let me know if there is an alternate. Az PS commands works from within the functions.

g0pinath avatar Mar 11 '20 04:03 g0pinath

@g0pinath Which Az PS commands are working for you within the functions? I am trying this command "az login --identity", still does not work for me. I have followed the steps given by @AnatoliB

bhushanI avatar Apr 02 '20 21:04 bhushanI

@anirudhgarg could you please tell when this feature will be available?

bhushanI avatar Apr 03 '20 05:04 bhushanI

Hi Team, I followed the above steps of uploading azure cli from mac book onto azure powershell functions and updated the profile.ps1, but still receive az command in not found. It would help if a detailed step by step instructions are provided for the workaround till the time microsoft provide inbulilt az cli support in azure functions.Thanks.

prasanthchil avatar May 21 '20 14:05 prasanthchil

Its been more than a year and We still do not have this feature.

abhilashredd avatar Jul 07 '20 20:07 abhilashredd

Hi All,

We able to run the Azure CLI commands from Function App (Powershell) But we are facing PERFORMANCE issue, like "az account set" command take minimum 2 mins and "az aks show" command takes minimum 3 mins. we achieved our requirement by using these commands inside the loop. so it impacts much.

  1. Why the Azure CLI commands taking long time in Azure Function App? because in Windows Azure CLI the "az account set" command takes around 3 secs and "az aks show" command takes around 6 secs to run.
  2. Do we have any option to improve the performance of the Azure CLI commands from Azure Function App?

Please let me know if anything is not clear and share your thoughts on this.

Thanks, Seenivasan.M

MasilamaniSeenivasan avatar Jul 16 '20 15:07 MasilamaniSeenivasan

@MasilamaniSeenivasan 2-3 min sounds way more than expected. How do you deploy Azure CLI? Do you include it in your app content, or upload it separately? What is the target location? Also, how do you invoke it and how you measure the time? Could you please share the code?

AnatoliB avatar Jul 16 '20 18:07 AnatoliB

  • Manually upload CLI2 zip file from local machine to Function App (location:D:\home\data) through kudu and unzip file in using PowerShell command.

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

  • Code snippet,

#onetime login using function MSI az login --identity

#used in loop to achieve our functionality (Get-Date).ToUniversalTime() #set subscription of current AKS az account set -s <SubscriptionName> (Get-Date).ToUniversalTime() #get AKS's msi objectId $objId = $(az aks show -g <ResourceGroupName> -n <AksName> --query identityProfile.kubeletidentity.objectId -o tsv) (Get-Date).ToUniversalTime()

Please let me know if anything else required.

MasilamaniSeenivasan avatar Jul 17 '20 04:07 MasilamaniSeenivasan

Is this still not supported?

I have Az listed in my requirements.psd1 image

And I also invoked Enable-AzureRmAlias in my profile.ps1 image

But I'm still getting the "The term 'az' is not recognized" error... I don't particularly want to go down the route of having to manually upload the CLI2 folder, as that just seems gross.

OwainJ avatar Oct 01 '20 12:10 OwainJ

@OwainJ Please note that Azure PowerShell (Az/AzureRm modules) and Azure CLI are separate and independent tools. The requirements.psd1 and profile.ps1 code in your comment refers specifically to Azure PowerShell. In order to use Azure CLI, the workarounds mentioned above are still required.

Please do let us know, however, which capabilities of Azure CLI you are interested in. Can this be currently achieved by Azure PowerShell instead? Chances are, extending Azure PowerShell is a better idea, but this depends on the details. Your feedback will help us prioritize, thank you.

AnatoliB avatar Oct 01 '20 17:10 AnatoliB

Hi @AnatoliB, thanks for your reply!

Ahh, I see, thanks for clearing that up! I'm very much a newbie with this stuff, so apologies in advance if anything I say is completely wrong.

Basically, I've been given a PowerShell command that creates a container instance and pushes the latest release image of a crawler to it. That command currently runs as an inline script (Azure CLI task) on an Azure DevOps release pipeline, which is triggered on a timer schedule.

This is the script that we have in the release pipeline Azure CLI task: az container create --resource-group $(resourceGroupName ) --name crawler-incremental-$(deploymentName) --image $(acrLoginServer)/crawlers/incremental:$(Build.BuildId) --dns-name-label crawler-incremental-$(deploymentName) --ports 80 --registry-login-server $(acrLoginServer) --registry-username $(acrName) --registry-password $(acrPassword) --cpu 4 --memory 16 --location $(location) --azure-file-volume-account-key $(storageAccountKey) --azure-file-volume-account-name $(storageAccountName) --azure-file-volume-mount-path /crawler/ --azure-file-volume-share-name crawler-incremental-$(deploymentName) -fs --restart-policy Never

I was hoping that I could move this to a Timer based PowerShell Function App, so that it can then keep it in source control, and deploy it to our client's azure resource group, rather than having it just float around in a DevOps release pipeline.

OwainJ avatar Oct 02 '20 09:10 OwainJ

Yes that should be possible using the corresponding Azure PowerShell Module for Azure Container Instances.

Have a look here: https://docs.microsoft.com/en-us/azure/container-instances/container-instances-quickstart-powershell https://docs.microsoft.com/en-us/azure/container-instances/container-instances-quickstart-powershell#create-a-container

anirudhgarg avatar Oct 02 '20 16:10 anirudhgarg

@anirudhgarg Thank you so much, that worked perfectly!

OwainJ avatar Oct 06 '20 16:10 OwainJ

Still looking for this feature, I'm looking to run an ad-hoc command in Azure Container Instance using az container exec from Azure CLI as Powershell Az module lacks this functionality https://docs.microsoft.com/en-us/powershell/module/az.containerinstance/?view=azps-5.0.0#container-instances

Nilsas avatar Nov 15 '20 13:11 Nilsas