2

I'm rewriting an ARM template because we no longer use Linked Templates. The Linked templates give us versioning headaches. I'm using a subscription level deployment to deploy a resource group, with nested a deletion lock, storage account, keyvault, 2 functionapps, user assigned managed identity and a keyvault access policy.

ARM Template I use:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "deplocation": {
            "type": "string",
            "allowedValues": [
                "West Europe",
                "North Europe"
            ],
            "defaultValue": "West Europe",
            "metadata": {
                "description": "Location for all resources."
            }
        },
        "tags": {
            "type": "object"
        },
        "rgName": {
            "type": "string"
        },
        "saName": {
            "type": "string",
            "metadata": {
                "description": "The name of the resource."
            }
        },
        "saType": {
            "type": "string",
            "allowedValues": [
                "Standard_LRS",
                "Standard_GRS",
                "Standard_ZRS",
                "Premium_LRS"
            ],
            "defaultValue": "Standard_LRS",
            "metadata": {
                "description": "Gets or sets the SKU name. Required for account creation; optional for update. Note that in older versions, SKU name was called accountType. - Standard_LRS, Standard_GRS, Standard_RAGRS, Standard_ZRS, Premium_LRS, Premium_ZRS, Standard_GZRS, Standard_RAGZRS"
            }
        },
        "saKind": {
            "type": "string",
            "allowedValues": [
                "StorageV2",
                "BlobStorage",
                "FileStorage",
                "BlockBlobStorage"
            ],
            "defaultValue": "StorageV2",
            "metadata": {
                "description": "Indicates the type of storage account. - Storage, StorageV2, BlobStorage, FileStorage, BlockBlobStorage"
            }
        },
        "saAccessTier": {
            "type": "string"
        },
        "saSupportsHttpsTrafficOnly": {
            "type": "bool"
        },
        "kvName": {
            "type": "string"
        },
        "kvSkuName": {
            "type": "string"
        },
        "kvSkuFamily": {
            "type": "string"
        },
        "kvSecretsPermissions": {
            "type": "array"
        },
        "uamiName": {
            "type": "string"
        },
        "fa1Name": {
            "type": "string"
        },
        "fa2Name": {
            "type": "string"
        },
        "aspName": {
            "type": "string"
        },
        "aspRg": {
            "type": "string"
        },
        "appInsightsName": {
            "type": "string"
        },
        "appInsightsRg": {
            "type": "string"
        }
    },
    "variables": {
        "tenantId": "[subscription().tenantId]",
        "subscriptionId": "[subscription().subscriptionId]"
    },
    "resources": [
        {
            "type": "Microsoft.Resources/resourceGroups",
            "apiVersion": "2018-05-01",
            "location": "[parameters('depLocation')]",
            "name": "[parameters('rgName')]",
            "tags": "[parameters('tags')]",
            "properties": {
            }
        },
        {
            "type": "Microsoft.Resources/deployments",
            "apiVersion": "2018-05-01",
            "name": "resourceDeployment",
            "resourceGroup": "[parameters('rgName')]",
            "dependsOn": [
                "[resourceId('Microsoft.Resources/resourceGroups/', parameters('rgName'))]"
            ],
            "properties": {
                "mode": "Incremental",
                "template": {
                    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                    "contentVersion": "1.0.0.0",
                    "resources": [
                        {
                            "name": "DeletionLock",
                            "type": "Microsoft.Authorization/locks",
                            "apiVersion": "2017-04-01",
                            "properties": {
                                "level": "CanNotDelete",
                                "notes": "[parameters('rgName')]"
                            }
                        },
                        {
                            "name": "[parameters('saName')]",
                            "type": "Microsoft.Storage/storageAccounts",
                            "apiVersion": "2019-04-01",
                            "sku": {
                                "name": "[parameters('saType')]"
                            },
                            "kind": "[parameters('saKind')]",
                            "location": "[parameters('deplocation')]",
                            "tags": "[parameters('tags')]",
                            "properties": {
                                "accessTier": "[parameters('saAccessTier')]",
                                "supportsHttpsTrafficOnly": "[parameters('saSupportsHttpsTrafficOnly')]"
                            }
                        },
                        {
                            "name": "[concat(parameters('saName'), '/default')]",
                            "type": "Microsoft.Storage/storageAccounts/blobServices",
                            "apiVersion": "2019-04-01",
                            "dependsOn": [
                                "[resourceId('Microsoft.Storage/storageAccounts', parameters('saName'))]"
                            ],
                            "properties": {
                                "cors": {
                                    "corsRules": [
                                    ]
                                },
                                "deleteRetentionPolicy": {
                                    "enabled": false
                                }
                            }
                        },
                        {
                            "name": "[parameters('kvName')]",
                            "type": "Microsoft.KeyVault/vaults",
                            "apiVersion": "2018-02-14",
                            "location": "[parameters('deplocation')]",
                            "tags": "[parameters('tags')]",
                            "properties": {
                                "tenantId": "[variables('tenantId')]",
                                "accessPolicies": [
                                ],
                                "sku": {
                                    "name": "[parameters('kvSkuName')]",
                                    "family": "[parameters('kvSkuFamily')]"
                                }
                            }
                        },
                        {
                            "name": "[parameters('uamiName')]",
                            "type": "Microsoft.ManagedIdentity/userAssignedIdentities",
                            "apiVersion": "2018-11-30",
                            "location": "[parameters('deplocation')]",
                            "tags": "[parameters('tags')]",
                            "properties": {
                            }
                        },
                        {
                            "name": "[parameters('fa1Name')]",
                            "type": "Microsoft.Web/sites",
                            "apiVersion": "2019-08-01",
                            "kind": "functionapp",
                            "location": "[parameters('deplocation')]",
                            "tags": "[parameters('tags')]",
                            "dependsOn": [
                                "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities/', parameters('uamiName'))]",
                                "[resourceId('Microsoft.Storage/storageAccounts/', parameters('saName'))]"
                            ],
                            "identity": {
                                "type": "SystemAssigned, UserAssigned",
                                "userAssignedIdentities": {
                                    "[concat('/subscriptions/', variables('subscriptionId'), '/resourceGroups/', parameters('rgName'), '/providers/Microsoft.ManagedIdentity/userAssignedIdentities/', parameters('uamiName'))]": {
                                    }
                                }
                            },
                            "properties": {
                                "siteConfig": {
                                    "appSettings": [
                                        {
                                            "name": "FUNCTIONS_WORKER_RUNTIME",
                                            "value": "dotnet"
                                        },
                                        {
                                            "name": "WEBSITE_TIME_ZONE",
                                            "value": "W. Europe Standard Time"
                                        },
                                        {
                                            "name": "AzureWebJobsStorage",
                                            "value": "[concat('DefaultEndpointsProtocol=https;AccountName=',parameters('saName'),';AccountKey=',listKeys(concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('rgName'),'/providers/Microsoft.Storage/storageAccounts/',parameters('saName')),providers('Microsoft.Storage', 'storageAccounts').apiVersions[0]).keys[0].value,';')]"
                                        },
                                        {
                                            "name": "FUNCTIONS_EXTENSION_VERSION",
                                            "value": "~2"
                                        },
                                        {
                                            "name": "WEBSITE_RUN_FROM_PACKAGE",
                                            "value": "1"
                                        },
                                        {
                                            "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
                                            "value": "[reference(concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('appInsightsRg'),'/providers/microsoft.insights/components/',parameters('appInsightsName')),providers('microsoft.insights', 'components').apiVersions[0]).InstrumentationKey]"
                                        }
                                    ],
                                    "alwaysOn": true
                                },
                                "serverFarmId": "[concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('aspRg'),'/providers/Microsoft.Web/serverfarms/',parameters('aspName'))]",
                                "httpsOnly": true
                            }
                        },
                        {
                            "name": "[parameters('fa2Name')]",
                            "type": "Microsoft.Web/sites",
                            "apiVersion": "2019-08-01",
                            "kind": "functionapp",
                            "location": "[parameters('deplocation')]",
                            "tags": "[parameters('tags')]",
                            "dependsOn": [
                                "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities/',parameters('uamiName'))]",
                                "[resourceId('Microsoft.Storage/storageAccounts/', parameters('saName'))]"
                            ],
                            "identity": {
                                "type": "SystemAssigned, UserAssigned",
                                "userAssignedIdentities": {
                                    "[concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('rgName'),'/providers/Microsoft.ManagedIdentity/userAssignedIdentities/',parameters('uamiName'))]": {
                                    }
                                }
                            },
                            "properties": {
                                "siteConfig": {
                                    "appSettings": [
                                        {
                                            "name": "FUNCTIONS_WORKER_RUNTIME",
                                            "value": "dotnet"
                                        },
                                        {
                                            "name": "WEBSITE_TIME_ZONE",
                                            "value": "W. Europe Standard Time"
                                        },
                                        {
                                            "name": "AzureWebJobsStorage",
                                            "value": "[concat('DefaultEndpointsProtocol=https;AccountName=',parameters('saName'),';AccountKey=',listKeys(concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('rgName'),'/providers/Microsoft.Storage/storageAccounts/',parameters('saName')),providers('Microsoft.Storage', 'storageAccounts').apiVersions[0]).keys[0].value,';')]"
                                        },
                                        {
                                            "name": "FUNCTIONS_EXTENSION_VERSION",
                                            "value": "~2"
                                        },
                                        {
                                            "name": "WEBSITE_RUN_FROM_PACKAGE",
                                            "value": "1"
                                        },
                                        {
                                            "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
                                            "value": "[reference(concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('appInsightsRg'),'/providers/microsoft.insights/components/',parameters('appInsightsName')),providers('microsoft.insights', 'components').apiVersions[0]).InstrumentationKey]"
                                        }
                                    ],
                                    "alwaysOn": true
                                },
                                "serverFarmId": "[concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('aspRg'),'/providers/Microsoft.Web/serverfarms/',parameters('aspName'))]",
                                "httpsOnly": true
                            }
                        },
                        {
                            "name": "[concat(parameters('kvName'), '/add')]",
                            "type": "Microsoft.KeyVault/vaults/accessPolicies",
                            "apiVersion": "2018-02-14",
                            "dependsOn": [
                                "[resourceId('Microsoft.KeyVault/vaults', parameters('kvName'))]",
                                "[resourceId('Microsoft.Web/sites', parameters('fa1Name'))]",
                                "[resourceId('Microsoft.Web/sites', parameters('fa2Name'))]"
                            ],
                            "properties": {
                                "accessPolicies": [
                                    {
                                        "tenantId": "[variables('tenantId')]",
                                        "objectId": "[reference(concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('rgName'),'/providers/Microsoft.Web/sites/', parameters('fa1Name'), '/providers/Microsoft.ManagedIdentity/Identities/default'),providers('Microsoft.ManagedIdentity', 'Identities').apiVersions[0]).principalId]",
                                        "permissions": {
                                            "secrets": "[parameters('kvSecretsPermissions')]"
                                        }
                                    }
                                    ,
                                    {
                                        "tenantId": "[variables('tenantId')]",
                                        "objectId": "[reference(concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('rgName'),'/providers/Microsoft.Web/sites/', parameters('fa2Name'), '/providers/Microsoft.ManagedIdentity/Identities/default'),providers('Microsoft.ManagedIdentity', 'Identities').apiVersions[0]).principalId]",
                                        "permissions": {
                                            "secrets": "[parameters('kvSecretsPermissions')]"
                                        }
                                    }
                                ]
                            }
                        }
                    ]
                }
            }
        }
    ],
    "outputs": {
        // "uamiPrincipalId": {            
        //     "value": "[reference(concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('rgName'),'/providers/Microsoft.ManagedIdentity/userAssignedIdentities/', parameters('uamiName')), providers('Microsoft.ManagedIdentity', 'userAssignedIdentities').apiVersions[0]).principalId]",
        //     "type": "string"
        // }
    }
}

Powershell code to deploy the template.

#region variableDeclaration
$ErrorActionPreference = "Stop"
$subscriptionId = "subscription id here"
$location = "West Europe"
#endregion variableDeclaration

Set-location -path $PSScriptRoot

#region connectToSubscription
Connect-AzAccount -ErrorAction Stop
Set-AzContext -Subscription $subscriptionId
#endregion connectToSubscription

#region createAzureResources
$workloadInputResources = @{
    depLocation                = $location
    tags                       = @{
        dienst         = "-"
        kostenplaats   = "-"
        omgeving       = "-"
        contactpersoon = "-"
        eigenaar       = "-"
        referentie     = "-"
        omschrijving   = "-"
    }    
    rgName                     = "resources-dev-rg"
    saName                     = "resourcesdevsa"
    saType                     = "Standard_LRS"
    saKind                     = "StorageV2"
    saAccessTier               = "Hot"
    saSupportsHttpsTrafficOnly = $true
    kvName                     = "resourcesdevkv"
    kvSkuName                  = "Standard"
    kvSkuFamily                = "A"
    kvSecretsPermissions       = @("get", "list" )
    uamiName                   = "resources-dev-uami"
    fa1Name                    = "resources-dev-fa1"
    fa2Name                    = "resources-dev-fa2"
    aspName                    = "resources-dev-asp"
    aspRg                      = "resources-asp-dev-rg"
    appInsightsName            = "resources-dev-appins"
    appInsightsRg              = "resources-appins-dev-rg"
}


New-AzDeployment -Name "deployResources" -Location $location -TemplateFile .\deploy.json  @workloadInputResources

#endregion createAzureResources

Problems:

  1. When deploying the arm template as-is I get the following error:
Resource Microsoft.Storage/storageAccounts 'resourcesdevsa' failed with message '{
  "error": {
    "code": "ResourceGroupNotFound",
    "message": "Resource group 'resources-dev-rg' could not be found."
  }
}'

But the creation of the resource group is successful.

  1. When rerunning the script I get the following error:
Resource Microsoft.Storage/storageAccounts 'resourcesdevsa' failed with message '{
  "error": {
    "code": "ResourceNotFound",
    "message": "The Resource 'Microsoft.Storage/storageAccounts/saName' under resource group 'resources-dev-rg' was not found."
  }
}'
  1. The second problem disappears when I comment out the deployment fa1, fa2 and the access policy

I was under the impression that using dependsOn solves the dependency issues but apparently I'm either wrong, using it incorrectly or missing a dependsOn somewhere.

Have been staring at this problem for hours now and I can't seem to find the problem. Any help is appreciated.

10
  • resourceId() function could work differently for subscription-level deployment. I would try to precise a resource group name parameter for resourceId() function in dependsOn Commented Nov 22, 2019 at 16:27
  • "dependsOn": [ "[parameters('rgName')]" ], try doing this, on the nested template, instead of what you are doing? Commented Nov 22, 2019 at 17:38
  • You're going to have even more headaches trying to mix resource group level and subscription level resource deployments. I'd recommend splitting the resource group and resource lock deployment into their own template. Commented Nov 25, 2019 at 6:36
  • @IvanIgnatiev: This could well be the problem but I can't seem to work out what the correct use of resourceId() should be in a subscription level deployment. Commented Nov 25, 2019 at 9:43
  • 1
    @jarrad_obrien: I are absolutely correcty. When home with a headache last friday. I have now taken your advise and split the deployment in two. Not very happy about it because one deployment script would be my preference but I have to move on for now. Thanks for the advice. If I get a solution I will post it here in the future. Commented Nov 25, 2019 at 9:45

1 Answer 1

1

Small update because parts of it are solved. Still a couple of issues though.

I have rewritten the ARM Template file as shown below

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "deplocation": {
            "type": "string",
            "allowedValues": [
                "West Europe",
                "North Europe"
            ],
            "defaultValue": "West Europe",
            "metadata": {
                "description": "Location for all resources."
            }
        },
        "tags": {
            "type": "object"
        },
        "rgName": {
            "type": "string"
        },
        "saName": {
            "type": "string",
            "metadata": {
                "description": "The name of the resource."
            }
        },
        "saType": {
            "type": "string",
            "allowedValues": [
                "Standard_LRS",
                "Standard_GRS",
                "Standard_ZRS",
                "Premium_LRS"
            ],
            "defaultValue": "Standard_LRS",
            "metadata": {
                "description": "Gets or sets the SKU name. Required for account creation; optional for update. Note that in older versions, SKU name was called accountType. - Standard_LRS, Standard_GRS, Standard_RAGRS, Standard_ZRS, Premium_LRS, Premium_ZRS, Standard_GZRS, Standard_RAGZRS"
            }
        },
        "saKind": {
            "type": "string",
            "allowedValues": [
                "StorageV2",
                "BlobStorage",
                "FileStorage",
                "BlockBlobStorage"
            ],
            "defaultValue": "StorageV2",
            "metadata": {
                "description": "Indicates the type of storage account. - Storage, StorageV2, BlobStorage, FileStorage, BlockBlobStorage"
            }
        },
        "saAccessTier": {
            "type": "string"
        },
        "saSupportsHttpsTrafficOnly": {
            "type": "bool"
        },
        "kvName": {
            "type": "string"
        },
        "kvSkuName": {
            "type": "string"
        },
        "kvSkuFamily": {
            "type": "string"
        },
        "kvSecretsPermissions": {
            "type": "array"
        },
        "uamiName": {
            "type": "string"
        },
        "fa1Name": {
            "type": "string"
        },
        "fa2Name": {
            "type": "string"
        },
        "aspName": {
            "type": "string"
        },
        "aspRg": {
            "type": "string"
        },
        "appInsightsName": {
            "type": "string"
        },
        "appInsightsRg": {
            "type": "string"
        }
    },
    "variables": {
        "tenantId": "[subscription().tenantId]",
        "subscriptionId": "[subscription().subscriptionId]"
    },
    "resources": [
        {
            "type": "Microsoft.Resources/resourceGroups",
            "apiVersion": "2018-05-01",
            "location": "[parameters('depLocation')]",
            "name": "[parameters('rgName')]",
            "tags": "[parameters('tags')]",
            "properties": {
            }
        },
        {
            "type": "Microsoft.Resources/deployments",
            "apiVersion": "2018-05-01",
            "name": "resourceDeployment",
            "resourceGroup": "[parameters('rgName')]",
            "dependsOn": [
                "[resourceId('Microsoft.Resources/resourceGroups/', parameters('rgName'))]"
            ],
            "properties": {
                "mode": "Incremental",
                "template": {
                    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                    "contentVersion": "1.0.0.0",
                    "resources": [
                        {
                            "name": "DeletionLock",
                            "type": "Microsoft.Authorization/locks",
                            "apiVersion": "2017-04-01",
                            "properties": {
                                "level": "CanNotDelete",
                                "notes": "[parameters('rgName')]"
                            }
                        },
                        {
                            "name": "[parameters('saName')]",
                            "type": "Microsoft.Storage/storageAccounts",
                            "apiVersion": "2019-04-01",
                            "sku": {
                                "name": "[parameters('saType')]"
                            },
                            "kind": "[parameters('saKind')]",
                            "location": "[parameters('deplocation')]",
                            "tags": "[parameters('tags')]",
                            "properties": {
                                "accessTier": "[parameters('saAccessTier')]",
                                "supportsHttpsTrafficOnly": "[parameters('saSupportsHttpsTrafficOnly')]"
                            },
                            "resources": [

                            ]
                        },                       
                        {
                            "type": "Microsoft.Storage/storageAccounts/blobServices",
                            "apiVersion": "2019-04-01",
                            "name": "[concat(parameters('saName'), '/default')]",
                            "dependsOn": [                                
                                "[concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('rgName'),'/providers/Microsoft.Storage/storageAccounts/',parameters('saName'))]"
                            ],
                            "properties": {
                                "cors": {
                                    "corsRules": [
                                    ]
                                },
                                "deleteRetentionPolicy": {
                                    "enabled": false
                                }
                            }
                        },
                        {
                            "name": "[parameters('uamiName')]",
                            "type": "Microsoft.ManagedIdentity/userAssignedIdentities",
                            "apiVersion": "2018-11-30",
                            "location": "[parameters('deplocation')]",
                            "tags": "[parameters('tags')]",
                            "properties": {
                            }
                        },
                        {
                            "name": "[parameters('fa1Name')]",
                            "type": "Microsoft.Web/sites",
                            "apiVersion": "2019-08-01",
                            "kind": "functionapp",
                            "location": "[parameters('deplocation')]",
                            "tags": "[parameters('tags')]",
                            "dependsOn": [
                                "[concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('rgName'),'/providers/Microsoft.ManagedIdentity/userAssignedIdentities/',parameters('uamiName'))]",
                                "[concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('rgName'),'/providers/Microsoft.Storage/storageAccounts/',parameters('saName'))]"
                            ],
                            "identity": {
                                "type": "SystemAssigned, UserAssigned",
                                "userAssignedIdentities": {
                                    "[concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('rgName'),'/providers/Microsoft.ManagedIdentity/userAssignedIdentities/',parameters('uamiName'))]": {
                                    }
                                }
                            },
                            "properties": {
                                "siteConfig": {
                                    "appSettings": [
                                        {
                                            "name": "FUNCTIONS_WORKER_RUNTIME",
                                            "value": "dotnet"
                                        },
                                        {
                                            "name": "WEBSITE_TIME_ZONE",
                                            "value": "W. Europe Standard Time"
                                        },
                                        // {
                                        //     "name": "AzureWebJobsStorage",
                                        //     "value": "[concat('DefaultEndpointsProtocol=https;AccountName=',parameters('saName'),';AccountKey=',listKeys(concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('rgName'),'/providers/Microsoft.Storage/storageAccounts/',parameters('saName')),providers('Microsoft.Storage', 'storageAccounts').apiVersions[0]).keys[0].value,';')]"
                                        // },
                                        {
                                            "name": "FUNCTIONS_EXTENSION_VERSION",
                                            "value": "~2"
                                        },
                                        {
                                            "name": "WEBSITE_RUN_FROM_PACKAGE",
                                            "value": "0"
                                        },
                                        {
                                            "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
                                            "value": "[reference(concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('appInsightsRg'),'/providers/microsoft.insights/components/',parameters('appInsightsName')),providers('microsoft.insights', 'components').apiVersions[0]).InstrumentationKey]"
                                        }
                                    ],
                                    "alwaysOn": true
                                },
                                "serverFarmId": "[concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('aspRg'),'/providers/Microsoft.Web/serverfarms/',parameters('aspName'))]",
                                "httpsOnly": true
                            }
                        },
                        {
                            "name": "[parameters('fa2Name')]",
                            "type": "Microsoft.Web/sites",
                            "apiVersion": "2019-08-01",
                            "kind": "functionapp",
                            "location": "[parameters('deplocation')]",
                            "tags": "[parameters('tags')]",
                            "dependsOn": [
                                "[concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('rgName'),'/providers/Microsoft.ManagedIdentity/userAssignedIdentities/',parameters('uamiName'))]",
                                "[concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('rgName'),'/providers/Microsoft.Storage/storageAccounts/',parameters('saName'))]"
                            ],
                            "identity": {
                                "type": "SystemAssigned, UserAssigned",
                                "userAssignedIdentities": {
                                    "[concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('rgName'),'/providers/Microsoft.ManagedIdentity/userAssignedIdentities/',parameters('uamiName'))]": {
                                    }
                                }
                            },
                            "properties": {
                                "siteConfig": {
                                    "appSettings": [
                                        {
                                            "name": "FUNCTIONS_WORKER_RUNTIME",
                                            "value": "dotnet"
                                        },
                                        {
                                            "name": "WEBSITE_TIME_ZONE",
                                            "value": "W. Europe Standard Time"
                                        },
                                        // {
                                        //     "name": "AzureWebJobsStorage",
                                        //     "value": "[concat('DefaultEndpointsProtocol=https;AccountName=',parameters('saName'),';AccountKey=',listKeys(concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('rgName'),'/providers/Microsoft.Storage/storageAccounts/',parameters('saName')),providers('Microsoft.Storage', 'storageAccounts').apiVersions[0]).keys[0].value,';')]"
                                        // },
                                        {
                                            "name": "FUNCTIONS_EXTENSION_VERSION",
                                            "value": "~2"
                                        },
                                        {
                                            "name": "WEBSITE_RUN_FROM_PACKAGE",
                                            "value": "0"
                                        },
                                        {
                                            "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
                                            "value": "[reference(concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('appInsightsRg'),'/providers/microsoft.insights/components/',parameters('appInsightsName')),providers('microsoft.insights', 'components').apiVersions[0]).InstrumentationKey]"
                                        }
                                    ],
                                    "alwaysOn": true
                                },
                                "serverFarmId": "[concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('aspRg'),'/providers/Microsoft.Web/serverfarms/',parameters('aspName'))]",
                                "httpsOnly": true
                            }
                        },
                        {
                            "name": "[parameters('kvName')]",
                            "type": "Microsoft.KeyVault/vaults",
                            "apiVersion": "2018-02-14",
                            "location": "[parameters('deplocation')]",
                            "tags": "[parameters('tags')]",
                            "dependsOn": [
                                "[concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('rgName'),'/providers/Microsoft.Web/sites/',parameters('fa1Name'))]",
                                "[concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('rgName'),'/providers/Microsoft.Web/sites/',parameters('fa2Name'))]"                                
                            ],
                            "properties": {
                                "tenantId": "[variables('tenantId')]",
                                "accessPolicies": [
                                    // {
                                    //     "tenantId": "[variables('tenantId')]",
                                    //     "objectId": "[reference(concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('rgName'),'/providers/Microsoft.Web/sites/', parameters('fa1Name'), '/providers/Microsoft.ManagedIdentity/Identities/default'),providers('Microsoft.ManagedIdentity', 'Identities').apiVersions[0]).principalId]",
                                    //     "permissions": {
                                    //         "secrets": "[parameters('kvSecretsPermissions')]"
                                    //     }
                                    // },
                                    // {
                                    //     "tenantId": "[variables('tenantId')]",
                                    //     "objectId": "[reference(concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('rgName'),'/providers/Microsoft.Web/sites/', parameters('fa2Name'), '/providers/Microsoft.ManagedIdentity/Identities/default'),providers('Microsoft.ManagedIdentity', 'Identities').apiVersions[0]).principalId]",
                                    //     "permissions": {
                                    //         "secrets": "[parameters('kvSecretsPermissions')]"
                                    //     }
                                    // }
                                ],
                                "sku": {
                                    "name": "[parameters('kvSkuName')]",
                                    "family": "[parameters('kvSkuFamily')]"
                                }

                            }
                        }
                    ]
                }
            }
        }
    ],
    "outputs": {
        // "uamiPrincipalId": {            
        //     "value": "[reference(concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('rgName'),'/providers/Microsoft.ManagedIdentity/userAssignedIdentities/', parameters('uamiName')), providers('Microsoft.ManagedIdentity', 'userAssignedIdentities').apiVersions[0]).principalId]",
        //     "type": "string"
        // }
    }
}

This work flawlessly very time, but as you can see I have 3 sections commented out. This is the problem area now. They are all dependsOn issues. When I uncomment the AzureWebJobsStorage part in the function app deployments the deployment fails with this message:

12:00:18 - Resource Microsoft.Storage/storageAccounts 'resourcesdevsa' failed with message '{
  "error": {
    "code": "ResourceGroupNotFound",
    "message": "Resource group 'resources-dev-rg' could not be found."
  }
}'

I have added the StorageAccount to the dependsOn section

"dependsOn": [
  "[concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('rgName'),'/providers/Microsoft.ManagedIdentity/userAssignedIdentities/',parameters('uamiName'))]",
  "[concat('/subscriptions/',variables('subscriptionId'),'/resourceGroups/',parameters('rgName'),'/providers/Microsoft.Storage/storageAccounts/',parameters('saName'))]"
],

But that doesn't seem to do the trick.

Any ideas?

Update 28/11/2019

Oke. I'm getting slightly frustrated. I now have a fully functional resourcegroup level deployment. I'm creating the resourcegroup and resourcegroup deletionlock in powershell and after that a New-AzResourceGroupDeployment. When I try to rewrite this into a subscription level deployment I keep getting dependency issues. For instance; creating the KeyVault Access Policies results in an error that the function app couldn't be found. And a similar error for setting the AzureWebJobsStorage setting for the function app. But than offcourse a reference to the storageaccount.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.