Azure availability zone ARM A parameter syntax - azure

I am attempting to add availability zone into my VM arm template.
Majority of times I don't want the VM to be in a zone as it is a single VM.
So in my ARM template, I have defined the zone section as:
"zones":[
"[if(greaterOrEquals(parameters('availabilityZone'), 1),parameters('availabilityZone'),json('null'))]"
],
this works fine if I set a value of 1 or higher but fails if I leave as blank.
failed validation with message: 'The zone(s) '' for resource
'Microsoft.Compute/virtualMachines/XXX' is not supported.
if I remove the if condition then hard code in the blank it works:
"zones": "",
I appreciate your help in advance.
Stu

we found the following solution that worked:
"zones":
"if(empty(parameters('availabilityZone')),parameters('availabilityZone'),array(parameters('availabilityZone')))]"

Please try something like this, if your parameter doesn't contain then it will pass the empty value,
"zones": "[if(empty(parameters('availabilityZone')),'', parameters('availabilityZone'))]",
https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-string?tabs=json#empty

Related

Can I pull data from an existingAzure Storage Account table using ARM Templates?

I have an existing Azure Storage Account which has a table. This table has a few details that I would be needing to use in my mainTemplate.json ARM file. Can I pull these values directly in the ARM Template.
[concat(reference(resourceId('Microsoft.Storage/storageAccounts',parameters('storageAccountName'))).primaryEndpoints.table, parameters('tableName'))]
I have been using the above statement in the outputs section and it returns me the table uri. Can I get the values inside that table by any way?
As suggested by Silent By referring this link
Try with using DeploymentScriptOutputs
The script takes one parameter, and output the parameter value. DeploymentScriptOutputs is used for storing outputs.
example
"outputs": {
"result": {
"value": "[reference('runPowerShellInlineWithOutput').outputs.text]",
"type": "string"
}
}
In the outputs section, the value line shows how to access the stored values. Write-Output is used for debugging purpose. To learn how to access the output file, see Monitor and troubleshoot deployment scripts
Thank you #silent for your suggestion

Azure CLI query help - JMESPath - filter using key having space , extract keys with space in them ( from Powershell )

Good morning everyone, Happy Friday!
Edit: Ended up solving it myself while debugging through it..
Thought I will post it here regardless so that anyone who comes here may get the solution...
Was helped by microsoft docs.
https://learn.microsoft.com/en-us/cli/azure/use-cli-effectively
Original question
I have Azure Resources which are tagged like :
"tags":{
"Application Name":"The Best App",
"Budget Line":"Home project",
"Technology":"Best Tech",
"Environment":"Development",
"Project":"Learn"
}
Sample json with array of resources and tags would look like
[{"name":"Resource1",
"tags": {
"Application Name":"The Best App",
"Budget Line":"Home project",
"Technology":"Best Tech",
"Environment":"Development",
"Project":"Learn"
}},
{"name":"Resource2",
"tags": {
"Application Name":"The Best App",
"Budget Line":"Home project",
"Technology":"Best Tech",
"Environment":"Development",
"Project":"Learn"
}},
{"name":"Resource3",
"tags": {
"Application Name":"Not App",
"Budget Line":"Home project",
"Technology":"Best Tech",
"Environment":"Development",
"Project":"Learn"
}
}
]
So now I am looking to query all Azure Resources where the tag **Application Name** = **The Best App**.
I was going through the JMESPath tutorial and got the following syntax to work on the JMESPath tutorial website.
What I tried to do :
Filter a resource based on a tag value, and then extract another tag value of those resources.
[?tags."Application Name"==`"The Best App"`].tags."Budget Line"
Next step was to try the same query as part of an azure cli query with JMESPath filter query.
Note I am running azure cli from PowerShell as I am more comfortable in it than Bash.
Unable to get it to work
Solution after quite a few trial and error
Looking at the powershell examples for escaping double quotes from https://learn.microsoft.com/en-us/cli/azure/use-cli-effectively
These worked : Examples are in PowerShell
Query an azure resource based on a tag key which does not have space in it, but project/extract the value of a tag having key name with space.
az resource list -g RESOURCEGROUPNAME --query "[?tags.Technology==`'Best Tech'`].tags.\`"Budget Line\`"" # Works in powershell
Query an azure resource based on a tag key which has a space in it, and extract/project the value of a tag having key name with space.
az resource list -g RESOURCEGROUPNAME --query "[?tags.\`"Budget Line\`"==`'Home Project'`].tags.\`"Application Name\`"" # Works in powershell
Happy Friday guys! Hope it helps someone!
Solution after quite a few trial and error
Looking at the powershell examples for escaping double quotes from https://learn.microsoft.com/en-us/cli/azure/use-cli-effectively
These worked : Examples are in PowerShell
Query an azure resource based on a tag key which does not have space in it, but project/extract the value of a tag having key name with space.
az resource list -g RESOURCEGROUPNAME --query "[?tags.Technology==`'Best Tech'`].tags.\`"Budget Line\`"" # Works in powershell
Query an azure resource based on a tag key which has a space in it, and extract/project the value of a tag having key name with space.
az resource list -g RESOURCEGROUPNAME --query "[?tags.\`"Budget Line\`"==`'Home Project'`].tags.\`"Application Name\`"" # Works in powershell
Happy Friday guys! Hope it helps someone!
This is a totally different context, but just in case it helps somebody, the author's solution didn't work for me doing a JSON query from Ansible (which also uses JMESPath). Instead, I had to do the following:
storage_url: "{{ openstack_api_versions.stdout | from_json | json_query('[? \"Service Type\" == `object-store`].Endpoint') | first }}"
In this context, it only works without backticks.

if condition in ARM Template resource

I have a resource in my Arm Template as follows:
parameters:
env
prodparam
nonprodparam
resources:
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2018-05-01",
"url": "[if(equals(parameters('env'),'prod'), parameters('prodparam'), parameters('nonprodparam'))]"
}
I see the url is always set to parameters('nonprodparam') even if parameters('env') = 'prod'. Is this if condition correct? Am I missing something?
Your if condition statement is correct, I tested it and got the correct result successfully.
You need to do the following steps to check where your problem is:
1. Check if your parameter definition is correct, especially as Stringfellow mentioned in the comment, to be case sensitive. It should be defined as follows.
2. Pay attention to whether to save after editing arm templates in the azure portal.
You can check the value of the parameter during the deployment process:

Azure Resource Manager - Convert value to 'lower'

I was recently using ARM templates to deploy multiple resources into Azure. While deploying Storage accounts, I ran into an issue which was due to some constraints put up by Azure like
Name of Storage Account should not contain upper case letters
Its max length should be 24.
I want this name from the user and can handle the 2nd issue using the "maxLength" property on 'parameters'. But for lower case, there is no such property in 'parameters' also I'm unable to find any function which will convert the user entered value to lower case.
What I expect:
Method to convert the user entered value in lower case.
Any other method to suit my use case.
Thanks in advance.
You should look at the string function reference of the ARM templates.
you need to create a variable (or just add those functions to the name input, like so:
"name": "[toLower(parameters('Name'))]"
or add a substring method, something like this:
"variables": {
"storageAccountName": "[tolower(concat('sawithsse', substring(parameters('storageAccountType'), 0, 2), uniqueString(subscription().id, resourceGroup().id)))]"
},

BUG:: Azure Resource Groups Validation Rules NOT WORKING

[First of all, it is very sad that BizSpark susbcription do not have any technical support, even to inform a error like this :-((( ]
Ok, well , the error, that occurs twice creating a VirtualMachine, so replicated:
{
"error": {
"code": "InvalidParameter",
"target": "resourceGroupName",
"message": "The entity name 'resourceGroupName' is invalid according to its validation rule: ^[^_\\W][\\w-._]{0,79}(?<![-.])$."
}
}
The reason, it is because my resource group is called _ReGr_MyName,
but IT ALREADY EXIST !!!
(indeed the rest of resources, like Public-Ip, Storage Accounts, etc, are already under that resource group)
so seems like validation rules are inconsistent across different resources
I can provide the Operation-Id or the TRacking-ID if necesary
But please, solve this short of issues, Azure should be an stable system
In general, avoid having any special characters (- or _) as the first or last character in any name. These characters will cause most validation rules to fail.
For detailed information, please check this article.
Since we can't edit the name of created resource group, you may need to create a new resource group and move the resources into it.
Here is a good answer from Zain Rizvi.

Resources