Search code examples
azureazure-devopsazure-resource-managerazure-rm-template

How to test linked ARM templates?


My ARM template is getting too big so I would like to use linked templates. I understand that templates need to be somewhere where they are accessible to ARM. But I should be able to test them somehow before I upload them to target location. Otherwise there is a risk that I override previously working templates with invalid ones. How do I revert then?

How do you do it?


Solution

  • So I came up with this script. Each developer has precreated resource group where he will deploy resources and precreated Blob container to store templates currently in development. Before deploying templates I use azcopyto synchronize my local folder with Blob container. Unfortunately sometimes Test-AzResourceGroupDeployment doesn't give you enough details in case of failure so I can't make decision based on it's return value to execute deployment or not. For now this seems to work fine. But it's already 3rd or 4th version and it will probably change in future. One idea is to incorporate ARM-TTK for template testing.

    $currentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
    $resourceGroup = $currentUser.Substring($currentUser.IndexOf('\') + 1) + "-testing"
    $containerName = $currentUser.Substring($currentUser.IndexOf('\') + 1) + "-testing"
    $storageAccountName = "team_shared_account_here";
    $containerUrl = "https://${storageAccountName}.blob.core.windows.net/${containerName}"
    
    Write-Host "Current user: <${currentUser}>" -ForegroundColor Green
    Write-Host "Deployment will use templates from <${containerName}> container" -ForegroundColor Green
    Write-Host "Resources will be deployed to <${resourceGroup}> resource group" -ForegroundColor Green
    Write-Host
    
    Write-Host "Syncing templates..." -ForegroundColor Green
    .\azcopy.exe sync '.' $containerUrl --include-pattern "*.json" --delete-destination true
    
    $toDeploy = "app1", "app2"
    foreach ($template in $toDeploy) {
        $templateUri = "${containerUrl}/${template}.json"
        $templateParameterUri = "${containerUrl}/${template}.parameters.DEV.json"
    
        Write-Host "`nDeploying:  ${templateUri}" -ForegroundColor Green
        Write-Host "Paramaters: ${templateParameterUri}" -ForegroundColor Green
    
        Test-AzResourceGroupDeployment -ResourceGroupName $resourceGroup `
            -TemplateUri $templateUri `
            -TemplateParameterUri $templateParameterUri `
            -Mode Incremental `
            -Verbose
    
        New-AzResourceGroupDeployment -ResourceGroupName $resourceGroup `
            -TemplateUri $templateUri `
            -TemplateParameterUri $templateParameterUri `
            -Mode Incremental `
            -DeploymentDebugLogLevel All `
            -Verbose
    }