Deploying Azure Function App to Specific Slot using PowerShell

Introduction

Efficiently deploying Azure Function Apps is critical for maintaining the stability, security, and performance of cloud applications. Handling deployments manually can be both time-consuming and error-prone, especially when managing multiple environments like development, staging, and production. Additionally, deploying to specific slots within a Function App can provide greater control over the deployment process and allow for safer testing and rollbacks. In this blog post, we will explore how to deploy Azure Function Apps to specific slots using PowerShell scripting. This approach not only automates the deployment process but also ensures flexibility and precision by allowing deployments to targeted slots.

Use cases

  1. Continuous deployment: Integrate Function App deployments into CI/CD pipelines for consistent and reliable deployments across different slots.
  2. Environment management: Deploy Function Apps to different environments (development, staging, production) by specifying the target slot.
  3. Blue-Green deployments: Use deployment slots to conduct blue-green deployments, reducing downtime and risk during updates.
  4. Testing and validation: Deploy to staging slots for testing and validation before swapping to production, ensuring higher quality and reliability.

Step-by-step deployment to Azure Function app slots using PowerShell


Step 1. Prepare the deployment package

Ensure your Azure Function App code is packaged into a zip file. This package will be deployed to the Function App. Sample Package with http trigger function is attached to the blog.

Step 2. Install Azure CLI

  • Install Azure CLI: Install Azure CLI to execute commands seamlessly within PowerShell. Install Azure CLI

Step 3. Created a slot in the Function app

Create a new slot in the existing or new Function App

Staging

Step 4. Authenticate with Azure using Az CLI

az login

Step 5. Run PowerShell script

Execute the PowerShell script, ensuring to provide necessary parameters like Function App Name, Resource Group Name, Subscription ID, and the path to the function app package zip file and slot where we need to deploy. Please Find the Script below.

# Function to deploy FunctionApp
Function Deploy-FunctionApp {
    # Parameters - FunctionAppName, ResourceGroupName, SubscriptionId, ZipFilePath, Slot
    [CmdletBinding()]
    param (
        # FunctionApp Name
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]$FunctionAppName,
        # ResourceGroup Name
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]$ResourceGroupName,
        # Subscription Id
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]$SubscriptionId,
        # ZipFilePath Location
        [Parameter(Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [String]$ZipFilePath,
        # Slot
        [Parameter(Mandatory = $false)]
        [String]$Slot
    )
    Write-Host "##[debug] -----Starting Deploy-FunctionApp Function-----" -ForegroundColor Cyan
    Write-Host "##[command] Parameters" -ForegroundColor Yellow
    "_" * 10
    # Get the command name
    $CommandName = $PSCmdlet.MyInvocation.InvocationName
    # Get the list of parameters for the command
    $ParameterList = (Get-Command -Name $CommandName).Parameters
    # Grab each parameter value, using Get-Variable
    foreach ($Parameter in $ParameterList) {
        Get-Variable -Name $Parameter.Values.Name -ErrorAction SilentlyContinue
        # Get-Variable -Name $ParameterList;
    }
    # Set Subscription
    Write-Host "Setting Subscription"
    az account set -s $SubscriptionId
    try {
        Write-Host "Starting Deploying....."
        if ($Slot) {
            Write-Host "Deploying in $Slot Slot"
            az functionapp deployment source config-zip -g $ResourceGroupName -n $FunctionAppName --src $ZipFilePath -s $Slot
        } else {
            Write-Host "Deploying in Production Slot"
            az functionapp deployment source config-zip -g $ResourceGroupName -n $FunctionAppName --src $ZipFilePath
        }
        Write-Host "Deployment Done!"
        Write-Host "##[debug] -----Completed Deploy-FunctionApp Function-----" -ForegroundColor Cyan
    } catch [Exception] {
        write-host $_.Exception.Message
        Write-Host "`nError in Line: " $_.InvocationInfo.Line
        Write-Host "`nError in Line Number: "$_.InvocationInfo.ScriptLineNumber
        Write-Host "`nError Item Name: "$_.Exception.ItemName
        throw $_.Exception.Message
    }
}
Deploy-FunctionApp -FunctionAppName "samplefunc-rg" -ResourceGroupName "sample-rg" -SubscriptionId "11a2dfac-1111-1111-1111-11111111111" -ZipFilePath "C:\Users\\OneDrive\Desktop\Blogs\3-DeployFunctionAppToSlot\CICD.HttpTrigger.zip" -Slot "staging"

This will Output

Parameter

Note. For Deploying in the Production Slot, ignore the parameter "slot" while calling the function below.

Deploy-FunctionApp -FunctionAppName "samplefunc-rg" -ResourceGroupName "sample-rg"  -SubscriptionId "11a2dfac-1111-1111-1111-11111111111" -ZipFilePath "C:\Users\\OneDrive\Desktop\Blogs\3-DeployFunctionAppToSlot\CICD.HttpTrigger.zip" -Slot "staging"

Step 6. Validate in Azure portal

Finally, Validate the Function App Slot from the Azure Portal.

Overview

Conclusion

Automating the deployment of Azure Function Apps using PowerShell, with the ability to target specific slots, simplifies the deployment process, improves efficiency, and reduces errors. By following the outlined steps, you can seamlessly deploy your function apps to different environments and slots, ensuring consistency and reliability. This approach enhances your deployment workflows, allowing you to focus on building and maintaining your applications with confidence and precision.