azure-policy icon indicating copy to clipboard operation
azure-policy copied to clipboard

Function "Int()" in Azure Policy Definitions Fail

Open mylameaccount opened this issue 7 years ago • 6 comments

Using the function Int() I get errors when saving the policy definition in the Policy Definition editor on the portal. I can use powershell to create the definition however it fails if I try to create/modify an assignment with the definition. Please let me know if "int()" is not a valid function.

Below is the error I get.

readyState: 4 responseText: {"error":{"code":"InvalidPolicyParameters","message":"A function or parameter in policy assignment 'ddef0d09f26d42f4acd3fbd8' associated with the policy definition '317cc88a-97e6-49f8-b27d-abf64a76b854' could not be validated. Please either fix the policy or remove the policy assignment to unblock. If using template functions, try following the tips in: https://aka.ms/policy-avoiding-template-failures. The inner exception 'The template language function 'int' cannot convert provided value '' to integer value. Please see https://aka.ms/arm-template-expressions/#int for usage details.'."}} status: 409 statusText: error

I have tried multiple variations of the following script adding single quotes and removing them however all give the same error.

"policyRule": {
    "if": {
        "allOf": [
                  {
                    "field": "type",
                    "equals": "Microsoft.Compute/disks"
                  },
                  {
                    "field": "Microsoft.Compute/disks/sku.name",
                    "equals": "Premium_LRS"
                  },
                  {
                    "value": "128",
                    "greaterOrEquals": "[int(127)]"
                  }
                ]
    },
    "then": {
      "effect": "audit"
    }
  }

Please let me know if the function is not valid or if I am doing something wrong.

mylameaccount avatar May 27 '19 06:05 mylameaccount

I can't seem to repro this. I managed to create a policy definition with your rule in the portal. I was then able to create a policy assignment on this definition without any errors.

Is this the exact content of the policy definition? Are you using any feature flags in the portal which may target different api-versions for policy definition/assignment?

rohit-bh avatar May 28 '19 22:05 rohit-bh

Are you creating using the portal or via powershell?

I am not using any feature flags in the portal that I am aware of. I was able to create my original script using powershell and have powershell assign it but after that I cannot modify or assign the policy definition via the portal.

Below are three screen shots. The first is what I get in the portal when I try to create the policy definition in my earlier post. The ones at the bottom shows what the impact of this is when provisioning a VM when the INT() function is used.

If I create new I get the results seen in the screen shot below:
image

Below is what I see when I provision a VM while the INT() function is used: image

Below is what I see when I provision a VM while the INT() function is used but I include the Raw Errors: image

mylameaccount avatar May 28 '19 22:05 mylameaccount

I should also note that the version of the policy definition with the INT() function that was created AND assigned via powershell does indeed work as expected when it audits a disk however assigning or altering the policy definition in the portal throws the errors.

ADDITIONALLY the existence of a policy assignment for this definition blocks my ability to provision a new VM in the portal as seen in the bottom two images in my last post.

mylameaccount avatar May 28 '19 22:05 mylameaccount

I was able to use another subscription and test the script creation there and it is successful in a second subscription so its probably that this is isolated to a single subscription.

Is there a recommended path for determining the cause this type of problem in a single subscription?

mylameaccount avatar May 28 '19 23:05 mylameaccount

Can you please share the successfully created version of the policy definition+assignment which is currently assigned to subscription (i.e. the one triggering the error on VM creation)?


The error shows the policy engine attempting to convert an empty string ('') to int.

'The template language function 'int' cannot convert provided value '' to integer value.

This shouldn't happen for the policy definition with the hardcoded [int(127)] provided above. Unexpected empty strings are usually a symptom that a field or parameters function wasn't resolved correctly. Did the original definition use either of these?

camillemarie avatar May 28 '19 23:05 camillemarie

Closing this issue per changes to the support model for this repo. See the Getting Support section in the readme for details and Azure Policy support information.

mentat9 avatar Jul 15 '19 17:07 mentat9