skip to Main Content

I try to create an azure policy that appends a created-on : dd/mm/yyyy tag on newly created resources.

I’m using the following default policy :

{
  "properties": {
    "displayName": "Append a tag and its value to resources",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Appends the specified tag and value when any resource which is missing this tag is created or updated. Does not modify the tags of resources created before this policy was applied until those resources are changed. Does not apply to resource groups. New 'modify' effect policies are available that support remediation of tags on existing resources (see https://aka.ms/modifydoc).",
    "metadata": {
      "version": "1.0.1",
      "category": "Tags"
    },
    "parameters": {
      "tagName": {
        "type": "String",
        "metadata": {
          "displayName": "Tag Name",
          "description": "Name of the tag, such as 'environment'"
        }
      },
      "tagValue": {
        "type": "String",
        "metadata": {
          "displayName": "Tag Value",
          "description": "Value of the tag, such as 'production'"
        }
      }
    },
    "policyRule": {
      "if": {
        "field": "[concat('tags[', parameters('tagName'), ']')]",
        "exists": "false"
      },
      "then": {
        "effect": "append",
        "details": [
          {
            "field": "[concat('tags[', parameters('tagName'), ']')]",
            "value": "[parameters('tagValue')]"
          }
        ]
      }
    }
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/2a0e14a6-b0a6-4fab-991a-187a4f81c498",
  "type": "Microsoft.Authorization/policyDefinitions",
  "name": "2a0e14a6-b0a6-4fab-991a-187a4f81c498"
}

With the following parameters : [utcNow(‘d’)]

enter image description here

Unfortunately as you can see I keep getting this error message.

The inner exception ‘The policy language function ‘utcNow’ has ‘1’
argument(s). Expected number of arguments is ‘0’.

According to the documentation, shouldn’t I be able to set a 'd' parameters to the function ?

If I removed the parameters it works and gives me the date in yyyyMMddTHHmmssZ format as per the documentation says.

How to get the date in dd/mm/yyyy format instead?

2

Answers


  1. Some of the ARM template functions are not allowed to use in policy definition. You can check it here.

    utcNow() – Unlike an ARM template, this property can be used outside defaultValue.
    Returns a string that is set to the current date and time in Universal ISO 8601 DateTime format yyyy-MM-ddTHH:mm:ss.fffffffZ.

    Login or Signup to reply.
  2. The best solution I have seen makes use of the Concat function along with substring. My implementation looks like this:

    "field": "tags['CreatedOnDate']",
                "value": "[concat(substring(utcNow(),5,2),'/', substring(utcNow(),8,2),'/',substring(utcNow(),0,4),'-',substring(utcNow(),11,8))]"
    

    Which produces a result like 11/30/2022-18:45:55

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search