how to use utcNow() in tags object type for ARM templates

Viewed 71

I am using utcNow() in resource tags, But instead of getting the date and time, am getting the "[utcNow()]" only again. Below is the format am using it Arm template.

ARM Template parameters:

"resourceTags": {
      "type": "object"
    }

parameters.json value:

"resourceTags": {
      "value": {
        "criticality": "Tier1",
        "applicationName": "Devzone",
        "owner": "abcd",
        "costCenter": "1234",
        "contactEmail": "abcd@gmail.com",
        "dataClassification": "Confidential",
        "environment": "Dev",
        "CreatedDate": "[utcNow()]"
      }
    }

I tried to use in the pipeline variable groups. Still am getting the same "[utcNow()]" instead of value.

What am I doing wrong? How can I get utcNow() substituted?

3 Answers

Per the documentation, you can only use the utcNow() function as the default value for a parameter.

The function isn't allowed in other parts of the template because it returns a different value each time it's called. Deploying the same template with the same parameters wouldn't reliably produce the same results.

Here's what that would look like given your object

"resourceTags": {
      "type": "object",
      "defaultValue" : {
        "criticality": "Tier1",
        "applicationName": "Devzone",
        "owner": "abcd",
        "costCenter": "1234",
        "contactEmail": "abcd@gmail.com",
        "dataClassification": "Confidential",
        "environment": "Dev",
        "CreatedDate": "[utcNow()]"
      }
    }

The entire object will be overwritten if you supply a value for the parameter, though. So instead of setting it there, you'll need to separate the values of the object into individual parameters. For the CreatedDate parameter, set the default value there instead and don't supply the value in the pipeline. Then set the tag as the parameter.

"CreatedDate": "[parameters('utcNow')]"

I followed the below steps and I got the results what I want

    "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'))]"

This works for me. Thank you

I also find other way to achieve this. Pls find the below json.

    {
     "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
    "tagValues": {
    "type": "object",
    "defaultValue": {
    "Dept": "Finance",
    "Environment": "Production"
    }
  }
  },
   "variables" : {
 "customTag" : {"myExtraDifferentTag": "myDifferentTagValue", 
"myAnotherExtraDifferentTag": "myAnotherDifferentTagValue"}
  },
"resources": [
  {
   "apiVersion": "2016-01-01",
  "type": "Microsoft.Storage/storageAccounts",
  "name": "[concat('storage', uniqueString(resourceGroup().id))]",
  "location": "[resourceGroup().location]",
  "tags": "[union(parameters('tagValues'),json('{\"myExtraTag\":\"myTagValue \"}'))]",  //Concatenates `tagValues` object to inline object    
  "sku": {
    "name": "Standard_LRS"
  },
  "kind": "Storage",
  "properties": {}
  }
   {
      "apiVersion": "2016-01-01",
      "type": "Microsoft.Storage/storageAccounts",
      "name": "mySecondResource",
      "location": "[resourceGroup().location]",
      "tags": "[union(parameters('tagValues'),variables('customTag'))]",     //Concatenates `tagValues` object to `customTag` object
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "Storage",
      "properties": {}
    }
]
 }
Related