Automating Tag Updates for Azure Function Apps with PowerShell

Updating tags for your Azure Function Apps is crucial for maintaining organization, tracking deployments, and managing resources effectively. In this blog, we'll explore how to automate the process of updating tags for an Azure Function App using PowerShell. This automation can be particularly useful when integrated with an Azure DevOps pipeline to update the tag after each deployment.

Why Use Tags?

Tags in Azure provide a way to categorize resources, making it easier to manage and organize them. They are key-value pairs that help you,

  • Track Deployments: Easily identify the version of the application currently deployed.
  • Resource Management: Simplify the management of resources by grouping them based on environment, project, or department.
  • Cost Management: Track and allocate costs to different departments or projects.
  • Use Case: Updating the Tag After Deployment.

One common use case is updating the version tag of a Function App after a new build is deployed. This helps in tracking which version of the code is currently running in the production or staging environment.

PowerShell Script to Update Function App Tags

Ensure you have the Azure CLI installed and you are authenticated to your Azure subscription. You can download and install the Azure CLI from here.

Login using the below command in PowerShell.

az login

Below is the PowerShell script that updates the version tag of a Function App. It takes parameters for the Function App name, resource group, subscription ID, slot (if any), and build number.

#Function to deploy Update-TagFunctionApp
Function Update-TagFunctionApp {
    #Parameters - FunctionAppName, ResourceGroupName, SubscriptionId, Slot, BuildNumber
    [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,
  
      #Slot
      [Parameter(Mandatory = $false)]
      [String]$Slot,
  
      #BuildNumber
      [Parameter(Mandatory = $false)]
      [String]$BuildNumber
  
    )
    Write-Host "##[debug] -----Starting Update-VersionResource 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 "Updating version tag for functionapp: $FunctionAppName with  $BuildNumber....."
      if ($Slot) {
        Write-Host "Updating Tag in $Slot Slot"
        az tag update --resource-id /subscriptions/$SubscriptionId/resourcegroups/$ResourceGroupName/providers/Microsoft.Web/sites/$FunctionAppName/slots/$Slot --operation Merge --tags version=$BuildNumber
      }
      else {
        Write-Host "Updating Tag in Production Slot"
        az tag update --resource-id /subscriptions/$SubscriptionId/resourcegroups/$ResourceGroupName/providers/Microsoft.Web/sites/$FunctionAppName --operation Merge --tags version=$BuildNumber
      }
      Write-Host "Updated!"
      Write-Host "##[debug] -----Completed Update-TagFunctionApp 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
    } 
  }

  Update-TagFunctionApp -FunctionAppName "samplefunc-rg" -ResourceGroupName "sample-rg"  -SubscriptionId "6ba2dfac-9ebd-4585-b" -BuildNumber "1.0.0.0"

This will Output,

Output

Validate the Tag in Azure Portal: Function App -> Overview.

Azure portal

Automating the update of tags for your Azure Function Apps ensures that your deployments are well-tracked and organized. By using the provided PowerShell script, you can easily integrate this step into your Azure DevOps pipelines, enhancing your deployment process and maintaining better control over your Azure resources.


Similar Articles