How can I use dependsOn on copy resources in nested deployment?

Viewed 17

I'm trying to deploy a RG tag for saving the roleAssignments version. I want that the tag deployment will be depended on the creation of the roleassignments. the roleassignments are created by using "copy" and the deployment is nested (since I need to change the scope to another RG and subscription).

I'm getting the following error message: Deployment template validation failed: 'The template reference 'roleAssignment' is not valid: could not find template resource or resource copy with this name. Please see https://aka.ms/arm-template-expressions/#reference for usage details.'.",

How can I resolve it?

The template:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "managedIdentityName": {
            "type": "String",
            "metadata": {
                "description": "The name of the managed identity resource."
            }
        },
        "roleAssignmentsDefinitionIds": {
            "type": "Array"
        },
        "roleAssignmentsVersion": {
            "defaultValue": 0,
            "type": "Int"
        },
        "resourceId": {
            "type": "String"
        },
        "rolesAssignmentsResourceGroup": {
            "type": "String"
        },
        "rolesAssignmentSubscriptionID": {
            "type": "String"
        }
    },
    "variables": {
        "copy": [
            {
                "name": "roleAssignmentsToCreate",
                "count": "[length(parameters('roleAssignmentsDefinitionIds'))]",
                "input": {
                    "name": "[guid(resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', parameters('managedIdentityName')), resourceGroup().id, parameters('roleAssignmentsDefinitionIds')[copyIndex('roleAssignmentsToCreate')])]",
                    "roleDefinitionId": "[parameters('roleAssignmentsDefinitionIds')[copyIndex('roleAssignmentsToCreate')]]"
                }
            }
        ],
        "roleAssignmentVersionTagName": "[concat(parameters('managedIdentityName'), 'RoleAssignmentVersion')]",
        "roleAssignmentsVersionTags": {
            "tags": {
                "[variables('roleAssignmentVersionTagName')]": "[parameters('roleAssignmentsVersion')]"
            }
        },
        "updatedResourceGroupTags": "[union(resourceGroup(), variables('roleAssignmentsVersionTags')).tags]",
        "roleAssignmentsDefaultVersion": {
            "tags": {
                "[variables('roleAssignmentVersionTagName')]": 0
            }
        }
    },
    "resources": [
        {
            "type": "Microsoft.Resources/deployments",
            "apiVersion": "2019-05-01",
            "name": "[uniqueString(concat('nonRegionalRoleAssignments-', parameters('resourceId'), variables('roleAssignmentsToCreate')[copyIndex()].roleDefinitionId))]",
            "subscriptionId": "[parameters('rolesAssignmentSubscriptionID')]",
            "resourceGroup": "[parameters('rolesAssignmentResourceGroup')]",
            "copy": {
                "name": "roleAssignment",
                "count": "[length(variables('roleAssignmentsToCreate'))]",
                "mode": "serial",
                "batchSize": 1
            },
            "properties": {
                "mode": "Incremental",
                "parameters": {},
                "template": {
                    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                    "contentVersion": "1.0.0.0",
                    "variables": {},
                    "resources": [
                        {
                            "name": "[guid(parameters('resourceId'), 'Microsoft.Authorization/roleDefinitions', variables('roleAssignmentsToCreate')[copyIndex()].roleDefinitionId, resourceGroup().id)]",
                                
                            "condition": "[less(int(union(variables('RoleAssignmentsDefaultVersion'), resourceGroup()).tags[variables('roleAssignmentVersionTagName')]), parameters('roleAssignmentsVersion'))]",
                            "apiVersion": "2020-04-01-preview",
                            "properties": {
                                "description": "[parameters('roleAssignmentDescription')]",
                                "principalId": "[reference(parameters('resourceId'), '2018-11-30').principalId]",
                                "roleDefinitionId": "[subscriptionResourceId('Microsoft.Authorization/roleDefinitions',  variables('roleAssignmentsToCreate')[copyIndex()].roleDefinitionId)]",
                                "principalType": "ServicePrincipal"
                            }
                        },
                        {
                            "type": "Microsoft.Resources/tags",
                            "name": "default",
                            "dependsOn": [
                                "roleAssignment"
                            ],
                            "apiVersion": "2019-10-01",
                            "properties": {
                                "tags": "[variables('updatedResourceGroupTags')]"
                            }
                        }
                    ]
                }
            }
        }
    ]
}

Thanks

0 Answers
Related