If condition in ARM Template - arm-template

Is it possible to set a value based on parameter value?
Let's say I have a reource in my arm template as follows:
parameters:
env
param1
param2
resources:
{
"type": "Microsoft.Resources/deployments",
"apiVersion": "2018-05-01",
"url": <value-based-on-if-condition>
}
If parameter('env') = "a", set url value = parameter('param1').
If parameter('env') = "b", set url value = parameter('param2')

You can use if function in the ARM Template like:
if(condition, trueValue, falseValue)
Please refer this documentation for explanation and examples.

Related

Reference Azure ARM template output variable from same deployment in another output variable

In an ARM Template, I have two output variables which are built by concating some strings. So, for the sake of it, let's just say it looks like this
"outputs": {
"firstKey": {
"type": "string",
"value": "[concat('first', 'Key')]"
},
"secondKey": {
"type": "string",
"value": "[concat('second', 'Key')]"
}
}
I now want to introduce a variable that concats these strings, like this
"bothKeys": {
"type": "string",
"value": "[concat('FirstKey=', outputs(firstKey), ';', 'SecondKey=', outputs(secondKey)]"
}
so I basically get these outputs
firstKey = firstKey
secondKey = secondKey
bothKeys = FirstKey=firstKey;SecondKey=secondKey
How would I do this?
I found various solutions to reference output variables from different deployments like [reference('deploymentName').outputs.propertyName.value] or [reference(resourceId('randomResource', variables('ResourceName'))).downloadLocation], but these are for referncing the outputs of OTHER deployments, where I want it from the same deployment.
Of course I could just copy paste the concat string but when I would edit one, I'd have to edit others as well. Is there a better way?
I couldn't find how to reference other output variables in the documentation listed here
https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/outputs?tabs=azure-powershell
https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/syntax#outputs

Azure ARM template for Automation Account Variable - cant use paramets as value

I am trying to create an ARM template for my runbook with additional variables ab packages.
I want to have the values of Automation Account Variables as parameters of ARM template.
When I am using the documentation syntax I am getting the variables value as "[parameters(parameterName)] instead the value of the parameter
when I am not using the syntax code I just get this error:
Invalid JSON - Kindly check the value of the variable
This is the ARM template resource code:
{
"apiVersion": "2020-01-13-preview",
"type": "Microsoft.Automation/automationAccounts/variables",
"name": "[concat(parameters('AutomationAccount'), '/blobContainerName')]",
"location": "[parameters('automationRegion')]",
"properties": {
"description": "output container name",
"isEncrypted": false,
"value": "\"[parameters('blobContainerName')]\""
}
}
how its looks like in the variables after deployment:
Try the following:
"properties": {
"value": "[concat('\"', parameters('blobContainerName'), '\"')]"
},
You need to use concat to join the strings

Create ARM parameter names dynamically

I have a scenario where i need to generate the parameters names dynamically. Like certificate1, certificate2, certificate3 .. so on. Currently all these parameters should be defined in Main template. Can we use copy to iterate and define parameter Names dynamically in Main/Parent template? Or is there a way in ARM templates by which this can be accomplished?
you can use copy construct in variables section or in resource definition\resource properties. and then you can use concat() together with copyIndex() function to create names.
example:
[concat('something-', copyIndex())]
this will give you names like something-0, something-1, something-2 etc. (copyIndex starts at 0). you can also choose to offset copyIndex by giving it an offset number:
[concat('something-', copyIndex(10))]
this will give you name like something-10, something-11, something-12 etc.
copy in variables\properties:
"copy": [
{
"name": "nameOfThePropertyOrVariableYouWantToIterateOver",
"count": 3,
"input": {
"name": "[concat('something-', copyIndex('nameOfThePropertyOrVariableYouWantToIterateOver', 1))]"
}
}
]
here you need to specify which loop are you referring to with copyIndex function and you can use offset as well
You can use the copy function in Azure Template to generate the name of the resource, just like certificate1, certificate2, certificate3 .. so on.
Example below:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"apiVersion": "2016-01-01",
"type": "Microsoft.Storage/storageAccounts",
"name": "[concat('storage',copyIndex())]",
"location": "[resourceGroup().location]",
"sku": {
"name": "Standard_LRS"
},
"kind": "Storage",
"properties": {},
"copy": {
"name": "storagecopy",
"count": 3
}
}
],
"outputs": {}
}
And the storage name will like these:
storage0
storage1
storage2
For more details, see Deploy multiple instances of a resource or property in Azure Resource Manager Templates.

How to dynamically create Azure KeyVault reference in ARM template?

I'm using the following piece of code in my ARM template parameters file to retrieve the secret value from keyvault:
"parameters": {
"mailAccount": {
"reference": {
"keyVault": {
"id": "/subscriptions/GUID/resourceGroups/KeyVaultRG/providers/Microsoft.KeyVault/vaults/KeyVault"
},
"secretName": "mailAccount"
}
},
and in the template file:
"appSettings": [
{
"name": "mailAccount",
"value": "[parameters('mailAccount')]"
},
{
I'd like to know if it is possible to reference a KeyVault by its name using dynamically constructed object (i.e. not /subscriptions/GUID/resourceGroups/KeyVaultRG/providers/Microsoft.KeyVault/vaults/KeyVault but [resourceId(subscription().subscriptionId, resourcegroup().name, 'Microsoft.KeyVault/vaults', parameters('KeyVaultName'))]) or [resourceId('Microsoft.KeyVault/vaults', parameters('KeyVaultName'))] ?
In fact, the main objective is to be able to pass the different KeyVault names when deploying templates - where the similar values are stored.
The need to have several KeyVaults is justified by the resources (and cost) separation.
Now I see only validation errors saying ~ resourceId function cannot be used while referencing parameters.
I cannot use nested\linked templates (and output values).
What I am usually doing to avoid this limitation of the resourceId function is to define a variable with the value of the parameter, then using the variable instead in the resourceId function.
Example:
"parameters": {
"KeyVaultName": {
"type": "string",
"metadata": {
"description": "Key Vault Name"
}
}
},
"variables": {
"KeyVaultName": "[parameters('KeyVaultName')]"
}
Then when I am referencing the KeyVault resource I reference it using the variable like this:
"[resourceId('Microsoft.KeyVault/vaults', variables('KeyVaultName')]"

Arm template: concatenate each element of an array with constant value

When creating an arm template is it possible to concatenate each element of an array with a constant string? Below is my created parameter and the resource I am trying to create.
"parameters": {
"servicesNames": {
"type": "array",
"defaultValue": [
"test-api-content",
"test-svc-content"
]
}
}
{
"apiVersion": "2016-03-30",
"type": "Microsoft.Network/publicIPAddresses",
"name": "[concat(parameters('servicesNames'),'pip')]",
"location": "[resourceGroup().location]",
"copy": {
"name": "PIPaddresscopy",
"count": "[length(parameters('servicesNames'))]"
},
"tags": {
"displayName": "PublicIPAddress"
}
}
I would like the output of the resource name to be created with
"test-api-contentpip"
How ever I am getting the following error
The provided parameters for language function 'concat' are invalid.
Either all or none of the parameters must be an array
Please suggest how I can concatenate each value of the element
Just to add to existing answer (as it is a bit unclear in my opinion).
What you are trying to do with your code - concatenate array with a string, and what you need to do is concatenate each element of the array with string.
There's a copyIndex() function that represent current iteration of the loop. and you can use array[number] to access specific member of the array. so
parameters('servicesNames')[copyIndex()]
means parameters('servicesNames')[0] and parameters('servicesNames')[1] in your case. That would effectively mean you've iterated over this array.
You can concatenate each value of the element by modifying your name property for your publicIpAddress resource as below.
"name": "[concat(parameters('servicesNames')[copyIndex()], 'pip')]",
copyIndex:
This function is always used with a copy object.
If no value is provided for offset, the current iteration value is returned. The iteration value starts at zero.

Resources