Search code examples
azureazure-resource-managerazure-rm-template

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://learn.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

  • 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.