'How to tag Current time as a Tag for an ARM Deployment

I am trying to create a Log Analytics Workspace using an ARM template and a parameter files. I am also thinking to tag currrent time as CreatedOn tag for the resource. Below is my ARM template-

{
    "$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "LAWName": {
            "type": "string"
        },
        "LocationName": {
            "type": "string"
        },
        "SKUName": {
            "type": "string"
        },
        "Tags": {
            "type": "object"
        }
    },
    "resources": [
        {
            "apiVersion": "2017-03-15-preview",
            "name": "[parameters('LAWName')]",
            "location": "[parameters('LocationName')]",
            "tags": "[parameters('Tags')]",
            "type": "Microsoft.OperationalInsights/workspaces",
            "properties": {
                "sku": {
                    "name": "[parameters('SKUName')]"
                }
            }
        }
    ]
}

and here is my param file-

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "LAWName": {
            "value": "atifmtest1"
        },
        "LocationName": {
            "value": "westeurope"
        },
        "SKUName": {
            "value": "pergb2018"
        }
        "Tags": {
            "value": {
                "CreatedBy": "Atif",
                "CreatedOn": "[utcNow()]",
                "Purpose": "Monitoring"
            }
        }
    }
}

I read here https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-date#utcnow that there is utcNow() function for ARM template but that is being considered as a string here and the current time does not appear as a tag for the resource. enter image description here What is the other way using which this can be achieved ? Thanks in advance !!



Solution 1:[1]

Here is a working example:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "utcShort": {
            "type": "string",
            "defaultValue": "[utcNow('d')]"
        },
        "location": {
            "type": "string",
            "defaultValue": "[resourceGroup().location]"
        }
    },
    "resources": [
        {
            "apiVersion": "2019-04-01",
            "type": "Microsoft.Storage/storageAccounts",
            "name": "[concat('storage', uniqueString(resourceGroup().id))]",
            "location": "[parameters('location')]",
            "tags": {
                "Dept": "Finance",
                "Environment": "Production",
                "LastDeployed": "[parameters('utcShort')]"
            },
            "sku": {
                "name": "Standard_LRS"
            },
            "kind": "Storage",
            "properties": {}
        }
    ]
}

Source.

Solution 2:[2]

Please follow the below steps for better results. Add the utcShort in parameters and give a default value "[utcNow()]", Its not work from the parameters file. add utcShort into variables to make an object type. Follow the below steps.

  "utcShort ": {
    "type": "string",
    "defaultValue": "[utcNow()]"
    },
    "resourceTags": {
    "type": "object"
  }
  },
  "variables":{
    "createdDate": {
    "createdDate": "[parameters('utcShort ')]"
    }
  },

Use this variable in Tags like below..

"tags": "[union(parameters('resourceTags'), variables('createdDate'))]"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martin Brandl
Solution 2 Jeremy Caney