1
votes

I need unique identifier at each custom deployment using ARM template for assigning resource name(uniqueName) which should be globally unique. As per documentation newGuid() returns a value in the format of a globally unique identifier. This function can only be used in the default value for a parameter. As newGuid() function can be called in only parameters section, but I don't want to give the input block to user, because user can edit the field while deploying this, so how can I hide that from user or is there any other way to create same unique guid globally at each deployment?

I have tried creating same unique guid using this in variables section, but it works only for few times of deployment. I'm not sure deployment issue but it may possible because guid function does not make unique field all the time.

"variables": {
        "uniqueName":"[guid(resourceGroup().id, deployment().name)]"
}

I have this template.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "appname": {
            "defaultValue": "xyz",
            "type": "String"
        },
        "uniqueName": {
            "defaultValue": "[newGuid()]",
            "type": "String"
        },
        "myIdentity": {
            "type": "String"
        }
    },
    "variables": {
        "location": "[resourceGroup().location]",
        "ResourceGroupName": "[resourceGroup().name]"
    },
    "resources": [
        {
            "type": "Microsoft.Resources/deploymentScripts",
            "apiVersion": "2019-10-01-preview",
            "name": "[parameters('uniqueName')]",
            "location": "[variables('location')]",
            "kind": "AzureCLI",
            "identity": {
                "type": "UserAssigned",
                "userAssignedIdentities": {
                    "[resourceID('Microsoft.ManagedIdentity/userAssignedIdentities/', parameters('myIdentity'))]": {}
                }
            },
            "properties": {
                "AzCliVersion": "2.0.80",
                "timeout": "PT10M",
                "arguments": "[parameters('appname')]",
                "cleanupPreference": "OnSuccess",
                "retentionInterval": "P1D",
                "supportingScriptUris": [
                    "https://some-uri/test.sh"
                ],
                "scriptContent": "[concat('./test.sh ', string(parameters('appname')), ' > $AZ_SCRIPTS_OUTPUT_PATH')]"
            }
        }
    ],
    "outputs": {
        "result": {
            "type": "String",
            "value": "[base64(string(reference(parameters('resourceName')).outputs))]"
        }
    }
}

Deployment error after some successful deployments is this.

{"code":"DeploymentFailed","message":"At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/DeployOperations for usage details.","details":[{"code":"Conflict","message":"{\r\n \"status\": \"canceled\",\r\n \"error\": {\r\n \"code\": \"ResourceDeploymentFailure\",\r\n \"message\": \"The resource operation completed with terminal provisioning state 'canceled'.\",\r\n \"details\": [\r\n {\r\n \"code\": \"DeploymentScriptExceededMaxAllowedTime\"\r\n }\r\n ]\r\n }\r\n}"}]}
1

1 Answers

0
votes

Point 1: This issue resolved after adding this property in resources. After adding this property, make sure some containers are not running. Delete storage accounts and containers manually which are created by previous failed deployments.

"cleanupPreference": "Always"

Always: Delete the automatically created resources once script execution gets in a terminal state. If an existing storage account is used, the script service deletes the file share created in the storage account. Because the deploymentScripts resource may still be present after the resources are cleaned up, the script service persists the script execution results, for example, stdout, outputs, return value, etc. before the resources are deleted.

Initially property was set to "cleanupPreference": "OnSuccess" which was not removing the created storage account and containers in failed deployments and making a issues while next deployment.

for more details check this azure document

Point 2. Creating unique identifier can be done using newGuid() function, but only in parameters section of templates.

Which creates unique Id globally, but if you don't want to show user input box then guid() also works.

"variables": {
        "uniqueName":"[guid(resourceGroup().id, deployment().name)]"
}

refer guid for more details.