17

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

Thanks!

Krishh
  • 4,111
  • 5
  • 42
  • 52

5 Answers5

11

Just in case there are people like me who need step by step solutions. Here is the procedure to deploy azure functions using powershell (non ARM way)

  1. 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
    
  2. Create a zip of the myFunctionName folder - let's name it my.zip. Make sure that after zipping my.zip contains the myFunctionName folder and all its contents

  3. Find your publish profile username and password 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 invoke 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. In the page that appears, navigate 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 in the azure portal.

REFERENCES

  1. https://github.com/projectkudu/kudu/wiki/REST-API#sample-of-using-rest-api-with-powershell

  2. http://markheath.net/post/deploy-azure-functions-kudu-powershell

S Raghav
  • 1,386
  • 1
  • 16
  • 26
10

You can deploy functions to Azure using the Kudu REST API. You can also see some code/samples of doing this in our templates repository. In this code sample, you can see how our test script calls out to the Kudu Rest apis to deploy a zip to the Function App.

The folder structure for functions is a function per folder. You need to deploy your Function folders to ./site/wwwroot on the Function App. You also need to add any app settings which might contain your secrets if you add any new bindings between updates.

The PowerShell code would look something along the lines of:

    $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"
Saca
  • 10,355
  • 1
  • 34
  • 47
Chris Anderson
  • 8,305
  • 2
  • 29
  • 37
7

In addition to what Chris describes, there is a first class ARM API you can use to deploy functions. Here is 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
}

See https://github.com/projectkudu/kudu/wiki/Functions-API for information about the underlying API.

Saca
  • 10,355
  • 1
  • 34
  • 47
David Ebbo
  • 42,443
  • 8
  • 103
  • 117
  • I can get to the functions log using `Invoke-RestMethod` and the endpoint below. How do/Can you do this in PowerShell with a first class ARM API? `$scmEndpoint = "https://$SiteName.scm.azurewebsites.net/api/vfs/LogFiles/Application/Functions/Function/$functionName/"` – Doug Finke Jun 07 '16 at 17:06
  • Does this work if I want to deploy a brand-new function app? I get an error stating that the resource cannot be found. – willem Aug 23 '16 at 06:17
  • @David Ebbo how does one upload custom assemblies along with this. are there any code samples for this scenario? – S Raghav Jun 14 '17 at 12:32
  • 1
    @raghav710 this approach is only for simple cases without binaries. See Chris's answer for the alternative. – David Ebbo Jun 15 '17 at 12:46
  • @this assumes a pre existing function app – David Ebbo Jun 15 '17 at 12:47
5

You can also use Azure CLI 2.0 + Azure Function CLI to deploy Azure functions form commandline/powershell

Azure CLI API can be used to provision a function app, using command

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

And apply application setting

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

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

func azure functionapp publish <azurefunctionapp>

Handy tools!

Chandermani
  • 42,589
  • 12
  • 85
  • 88
4

In addition to all of the above, we have released a Preview module for Azure Functions (https://www.powershellgallery.com/packages/Az.Functions/0.0.1-preview).

Currently, this module contains cmdlets to manged function apps and function app plans. I have opened an issue to request creating a cmdlet to deploy a function app. If you are interested in this feature, please vote up at https://github.com/Azure/azure-powershell/issues/10966.

To install the Azure Functions (Az.Functions) module, run the following command from the latest version of psws which can be downloaded at https://github.com/PowerShell/PowerShell/releases.

Install-Module -Name Az.Functions -AllowPrerelease

Please give it a try and send us feedback at https://github.com/Azure/azure-powershell/issues. When opening an issue, please make sure [Az.Functions] is included in the title.

Cheers,

Francisco