Reputation: 115
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
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...
"vmSize": {
"small": "Standard_E2s_v3",
"medium": "Standard_E8s_v3",
"large": "Standard_E16s_v3"
}
"vmSize": "[variables('vmSize')[parameters('tshirtSize')]]"
Upvotes: 5
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