Deploying azure functions using powershell

Is there any way I can deploy azure functions using powershell scripts? CI will not work for us because we use octopus deployment to deploy in all of our production services. Therefore, it would be useful if there was a deployment method using powershell scripts.

Thanks!

+6
source share
4 answers

You can deploy Azure features using the Kudu REST API . You can also see the code / examples of this in our template repository . In this code example, you can see how our test script calls Kudu Rest apis to deploy zip in a Function application.

The structure for functions is a function for each folder. You need to deploy your functional folders to ./site/wwwroot in the Function application. You also need to add any application settings that may contain your secrets if you add new bindings between updates.

PowerShell code will look something like this:

  $apiUrl = $config.scmEndpoint + "/api/zip/" if ($destinationPath) { $apiUrl = $apiUrl + $destinationPath } $response = Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $config.authInfo)} -Method PUT -InFile $zipFilePath -ContentType "multipart/form-data" 
+7
source

In addition to what Chris describes, there is a first-class ARM API that you can use to deploy functions. Here's what it looks like in PowerShell:

 Function DeployHttpTriggerFunction($ResourceGroupName, $SiteName, $FunctionName, $CodeFile, $TestData) { $FileContent = "$(Get-Content -Path $CodeFile -Raw)" $props = @{ config = @{ bindings = @( @{ type = "httpTrigger" direction = "in" webHookType = "" name = "req" } @{ type = "http" direction = "out" name = "res" } ) } files = @{ "index.js" = $FileContent } test_data = $TestData } New-AzureRmResource -ResourceGroupName $ResourceGroupName -ResourceType Microsoft.Web/sites/functions -ResourceName $SiteName/$FunctionName -PropertyObject $props -ApiVersion 2015-08-01 -Force } 

For information on the core API, see https://github.com/projectkudu/kudu/wiki/Functions-API .

+5
source

Just in case, there are people like me who need step-by-step solutions. Below is the procedure for deploying azure functions using powershell (path without ARM)

  • Create an azure function with the following structure

     myFunctionName(Folder) | |_ function.json (contains info on input, output, trigger) |_ run.csx (contains code) |_ [OPTIONAL] project.json (for nuget packages) |_ [OPTIONAL] bin(Folder) |_ all custom DLLs go in this folder 
  • Create a zip code in the myFunctionName folder - name it my.zip . Make sure that after zipping my.zip contains the myFunctionName folder and all its contents

  • Find your username and password to publish as described here , namely

  $creds = Invoke-AzureRmResourceAction -ResourceGroupName YourResourceGroup -ResourceType Microsoft.Web/sites/config -ResourceName YourWebApp/publishingcredentials -Action list -ApiVersion 2015-08-01 -Force $username = $creds.Properties.PublishingUserName $password = $creds.Properties.PublishingPassword 

and then call the KUDU REST API using powershell as follows

  $username = '<publish username>' #IMPORTANT: use single quotes as username may contain $ $password = "<publish password>" $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password))) $apiUrl = "https://<yourFunctionApp>.scm.azurewebsites.net/api/zip/site/wwwroot" $filePath = "<yourFunctionName>.zip" Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method PUT -InFile $filePath -ContentType "multipart/form-data" 
  1. Go to <yourFunctionApp>.scm.azurewebsites.net -> Debug menu at the top -> CMD . On the page that appears, go to site -> wwwroot . You should see the contents of your zip file extracted there, and you can also verify that your azure function is available on the azure portal.

LITERATURE

+3
source

You can also use the Azure CLI 2.0 + Azure Function CLI to deploy Azure functions from the command line / powershell

You can use the Azure CLI API to provide a functional application with the command

 az functionapp create --name $functionappName --resource-group $resourceGroup --storage-account $storageAccountName --consumption-plan-location $consumptionPlanLocation 

And apply the application setting

 az functionapp config appsettings set --name $functionappName --resource-group $resourceGroup --settings "test=value" 

And the Azure Function CLI api can be used to deploy the functions you have

 func azure functionapp publish <azurefunctionapp> 

Convenient tools!

+1
source

All Articles