0

trying to do some extra tweaks to my ps1 script that deploys vm by using an arm template.

the script asks if an availability zone is needed, if yes then gives you the zone options available. if its not needed, then the $zone variable is set to defaultvalue or null, and then that value is used in the arm template to deploy a vm without any availability zones.

my template.json file contains the following parameter for the zone.

"availabilityZone": {
  "type": "array",
  "defaultValue": []

the IF statement contains

   "zones": "[if(empty(parameters('availabilityZone')), json('[]'), array(parameters('availabilityZone')))]"

ive also tried using the following, but it does not work

json('null') 

if i hardcode the Availabilityzone then it deploys accordingly. if the Availabilityzone is left out of the deployment it will deploy into avalabilityzone 1.

If the $zone is set as 0, $null or left as blank it fails and says

Error: Code=InvalidDeploymentParameterValue; Message=The value of deployment parameter 'availabilityZone' is null. Please specify the value or use the parameter reference.

what am i missing?

cheers

RogerDingo
  • 13
  • 8
  • If no parameter is passed via PowerShell script , then the ARM template picks the default value. you can refer [this](https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/deploy-powershell#pass-parameter-values) documentation to pass parameter values using PowerShell inline parameters. – VenkateshDodda Nov 08 '21 at 08:10
  • You can use if condition in ARM template - https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-logical#if – Jagrati Modi Nov 08 '21 at 08:32
  • thanks guys... i will take a look and see if i can figure out. im sure to have some questions soon!... thanks! – RogerDingo Nov 08 '21 at 09:30
  • ive tried the following... "zones": [ "if(empty(parameters('availabilityZone')),parameters('availabilityZone'),array(parameters('availabilityZone')))" ] but cant get it to work... what am i missing? – RogerDingo Nov 08 '21 at 14:53
  • also tried this "zones": "[if(not(empty(parameters('availabilityZone'))), reference(parameters('availabilityZone'), json('null'))]" but getting an error message saying 14:57:51 - Error: Code=InvalidTemplate; Message=Deployment template language expression evaluation failed: 'Unable to parse language expression | 'if(not(empty(parameters('availabilityZone'))), reference(parameters('availabilityZone'), json('null'))': expected token 'RightParenthesis' and | actual 'EndOfData'.'. Please see https://aka.ms/arm-template-expressions for usage details. – RogerDingo Nov 08 '21 at 14:58
  • gotten a bit further by using this "zones": "[if(not(empty(parameters('availabilityZone'))), parameters('availabilityZone'), json('null'))]" but i cannot get it to work ... Cannot bind argument to parameter 'availabilityZone' because it is null. any suggestions as to how i can get this to accept the null/blank value so it doesnt set any availabilityzones ? – RogerDingo Nov 08 '21 at 15:24
  • ive updated the main question with the IF statement and coding... but i still cant get it to read the variable as null... can you advise as to what im missing? – RogerDingo Nov 09 '21 at 11:32

2 Answers2

0

property zones is an array, you can achive what you need by passing empty array '[]'

  • hey mate... can you show me the proper code for this as im sure ive tried using the square brackets before and it didnt work .. :/ – RogerDingo Dec 23 '21 at 09:16
0

I spent 2 hours looking for the answer to this for myself... and it was pretty simple in the end. I had extra square brackets "[]":

"zones": ["[parameters('zone')]"]

So I kept getting an error and it wouldn't accept a 'null' value for AV zone like I wanted. The issue wasn't that the template won't accept a null value, but that with the schema above I was submitting '[]' as a zone because of the extra brackets. Here is the correct schema:

"zone": "[parameters('zone')]"

For your parameter, this is how it should look like:

"zone": {"type": "array", "defaultValue": []}

Now when you submit your parameter file (in my case, it is thru PowerShell), you can either leave the zone parameter out of the parameter object/file (because of the default value), or submit the zone parameter as an empty array:

 $zone = @()

This will ensure that when a zone is not required, you submit the empty array as above. And if you require a zone, then submit the zone number that you want.