John Vasquez
John Vasquez

Reputation: 115

Using If/Else in Arm Templates

I have an ARM that automates the build of a multi-VM environment. I hope to ask the user to define SMALL/MEDIUM/LARGE in terms of the size of the environment. The template will then decide types of VMs base on the value of environment size. For example, if the size = 'SMALL', the vmSize = 'Standard_E2s_v3', else if size = 'MEDIUM', then vmSize = 'Standard_E8s_v3', else if size = 'LARGE' then vmSize = 'Standard_E16s_v3'. How can I do that?

Do ARM Templates even support if/else statements?

Upvotes: 0

Views: 2607

Answers (2)

bmoore-msft
bmoore-msft

Reputation: 8717

You can certainly nest if() statements in the language (see: https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions#logical-functions) but the approach below is a little easier to read IMO...

  1. create a variable with your T-Shirt setting (you can put more than one property in here if you need to)
    "vmSize": {
      "small": "Standard_E2s_v3",
      "medium": "Standard_E8s_v3",
      "large": "Standard_E16s_v3"
    }
  1. And then set the size via:

"vmSize": "[variables('vmSize')[parameters('tshirtSize')]]"

Upvotes: 5

Alex
Alex

Reputation: 18526

You should look into variables.

Code could be something like this:

"parameters": {
  "size": {
    "type": "string",
    "allowedValues": [
      "small",
      "medium"
    ]
  }
},
"variables": {
  "vmsizes": {
    "small": {
      "vmSize": "Standard_E2s_v3"
    },
    "medium": {
      "vmSize": "Standard_E8s_v3"
    }
  }
},

Which can be used like this:

"[variables('vmsizes')[parameters('size')].vmSize]"

You could also do the same thing using a single variable and a logical expression (if/else)

"vmSize": "[if(equals(parameters('size'), 'small'), 'Standard_E2s_v3', '<probably a nested if in your case>')]"

I would prefer the first option.

Upvotes: 3

Related Questions