Search code examples
azureazure-resource-manager

The resource 'Microsoft.Storage/storageAccounts/mystorageAccountName' is not defined in the template


I'm deploying an ARM template with yaml pipelines using the task AzureResourceManagerTemplateDeployment with deploymentScope: 'Subscription'.

I want to deploy a resourcegroup, containing a storageAccounts and blobServices. When adding the blobServices the deployment fails with The resource 'Microsoft.Storage/storageAccounts/mystorageAccountName' is not defined in the template.

I tried different solutions like moving the blobServices to the storageAccounts.Resources or removing the dependsOn section but nothing worked. Any idea how to fix this? This is my current template:

{
    "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
    "contentVersion": "1.0.0.1",
    "parameters": {
        "rgName": {
            "type": "string"
        },
        "rgLocation": {
            "type": "string"
        },
        "rgtags": {
            "type": "object"
        }
    },
    "variables": {
        "rgName": "[parameters('rgName')]",
        "storageAccountName": "mystorageAccountName"
    },
    "resources": [
        {
            "type": "Microsoft.Resources/resourceGroups",
            "apiVersion": "2022-09-01",
            "location": "[parameters('rgLocation')]",
            "name": "[variables('rgName')]",
            "properties": {},
            "tags": "[parameters('rgtags')]",
            "resources": []
        },
        {
            "type": "Microsoft.Resources/deployments",
            "apiVersion": "2022-09-01",
            "name": "storageDeployment",
            "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",
                    "parameters": {},
                    "variables": {},
                    "resources": [
                        {
                            "type": "Microsoft.Storage/storageAccounts",
                            "apiVersion": "2022-09-01",
                            "name": "[variables('storageAccountName')]",
                            "location": "[parameters('rgLocation')]",
                            "kind": "StorageV2",
                            "sku": {
                                "name": "Standard_LRS"
                            }
                        },
                        {
                            "type": "Microsoft.Storage/storageAccounts/blobServices",
                            "apiVersion": "2022-09-01",
                            "name": "[concat(variables('storageAccountName'), '/default')]",
                            "sku": {
                                "name": "Standard_LRS",
                                "tier": "Standard"
                            },
                            "dependsOn": [
                                "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]"
                            ]
                        }
                    ],
                    "outputs": {}
                }
            }
        }
    ],
    "outputs": {
        "deploymentName": {
            "type": "string",
            "value": "[deployment().name]"
        },
        "rgName": {
            "type": "string",
            "value": "[variables('rgName')]"
        }
    }
}

Solution

  • You need to set the properties.expressionEvaluationOptions.scope property of the Microsoft.Resources/deployments resource to be Inner.

    Without this the resourceId (and other) expressions in the deployment resource will be evaluated in the scope of the outer deployment template.

    However in doing this you will then not be able to reference your variables and parameters from the outer template and will need to introduce parameters and values to your inner template

    You can replace the deployment resource with the following.

    I can't validate this as I'm on mobile.

    {
                "type": "Microsoft.Resources/deployments",
                "apiVersion": "2022-09-01",
                "name": "storageDeployment",
                "resourceGroup": "[parameters('rgName')]",
                "dependsOn": [
                    "[resourceId('Microsoft.Resources/resourceGroups/', parameters('rgName'))]"
                ],
                "properties": {
                    "mode": "Incremental",
                    "expressionEvaluationOptions": {
                        "scope": "Inner"
                    },
                    "parameters": {
                        "mystorageAccountName": {
                             "value": "[variables('storageAccountName')]"
                        },
                        "rgLocation": {
                             "value": "[parameters('rgLocation')]"
                        }
                    },
                    "template": {
                        "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                        "contentVersion": "1.0.0.0",
                        "parameters": {
                            "mystorageAccountName": {
                                 "type": "string"
                            },
                            "rgLocation": {
                                 "type": "string"
                            }
                        },
                        "variables": {},
                        "resources": [
                            {
                                "type": "Microsoft.Storage/storageAccounts",
                                "apiVersion": "2022-09-01",
                                "name": "[parameters('storageAccountName')]",
                                "location": "[parameters('rgLocation')]",
                                "kind": "StorageV2",
                                "sku": {
                                    "name": "Standard_LRS"
                                }
                            },
                            {
                                "type": "Microsoft.Storage/storageAccounts/blobServices",
                                "apiVersion": "2022-09-01",
                                "name": "[concat(parameters('storageAccountName'), '/default')]",
                                "sku": {
                                    "name": "Standard_LRS",
                                    "tier": "Standard"
                                },
                                "dependsOn": [
                                    "[resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName'))]"
                                ]
                            }
                        ],
                        "outputs": {}
                    }
                }
            }