Azure ARM Template - conditional input parameter - azure

In Short:
Is it possible to have condition field (or something else to achieve this functionality), inside a parameter, so that this parameter input will be asked to the user, only based on another parameter's value.
i.e., Only if user selects Create New on a parameter, the input for the name of that parameter should be asked.
In Detail:
I have a parameter virtualNetworkCreateNewOrUseExisting which will accept two values - Create New and Use Existing.
"parameters": {
"virtualNetworkCreateNewOrUseExisting": {
"type": "string",
"defaultValue": "Create New",
"allowedValues": [
"Create New",
"Use Existing"
]
},
// Other parameters
}
I am trying to create a Virtual Network based on an input from user.
If user selects Create New, it will be created, and if they select Use Existing, it will be skipped. I see that this is achievable by using condition field inside the resource.
"resources": [
{
"condition": "[equals(parameters('virtualNetworkCreateNewOrUseExisting'), 'Create New')]",
// Other fields
}
// Other resources
]
Now, my question here is that,
Similar to this, is it possible to have condition field, inside another parameter, so that this parameter input will be asked to the user, only based on the previous parameter value.
i.e., Only if user selects Create New, the input for Virtual Network Name should be asked.
Something like this, by using a condition field:
"parameters": {
"virtualNetworksName": {
"condition": "[equals(parameters('virtualNetworkCreateNewOrUseExisting'), 'Create New')]",
"defaultValue": "vn-1",
"type": "string"
},
// Other parameters
}
But, I see that condition field, is not supported inside parameters (at least as of now).
Or, is it at least possible to have, if statements, like this (So that, the value of the field will be displayed empty by default if user selects Use Existing.):
"parameters": {
"virtualNetworksName": {
"defaultValue": "[if(equals(parameters('virtualNetworkCreateNewOrUseExisting'),'Create New'), 'vn-1', '')]",
"type": "string"
},
// Other parameters
}
Or, any other way to achieve this goal?

You can't do this natively in the template file, but in the portal user experiences for deploying the template you can... See:
https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/template-specs-create-portal-forms
and
https://learn.microsoft.com/en-us/azure/azure-resource-manager/managed-applications/create-uidefinition-overview
You don't have to use a templateSpec or managedApp (see the "Deploy to Azure button here: https://github.com/Azure/azure-quickstart-templates/tree/master/demos/100-marketplace-sample )
but templateSpec or managedApp will give you a better experience if that's an option.

As per the current Azure documentation, we cannot add conditions to parameters in parameters block.
As a side note you can use PowerShell parameter inline functions while deploying the template.
We see that there is a feature request already in place to add Support functions within the definition of parameters... ยท Community (azure.com) We would suggest you to make a comment & Upvote on the exiting feedback request .

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

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 TimerTrigger Multiple instances with different configuration

Using VS Code + "Azure Function" Extension, I generated the default python 3.7 timedTrigger function with the following settings:
// functions.json
{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "mytimer",
"type": "timerTrigger",
"direction": "in",
"schedule": "0 0 */6 * * *"
}
]
}
I have also set up two environment variables "USER" and "PASSWORD" which are set up in the Configuration of the app service.
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": ****************,
"FUNCTIONS_WORKER_RUNTIME": "python",
"USER": "********",
"PASSWORD": "*********"
}
}
Goal:
I want to run two instances of the same function, but using two different Configs, i.e. Users+Passwords.
Problem:
I believe that the Configuration/App Settings might not be sufficient for this. I can't find a way to run the function twice with multiple different parameters.
Question: What options do I have to reach my goal? One idea I had was to put the User/PW into the functions.json, but I could not figure out how to access that information from within the app function.
You have two options:
Read a custom json (not necessarily reading the value of function.json), you can add a custom json in the function app, and then read the value you want according to the hierarchy of the json file, Then use the value you read in the trigger.
Use deployment slot. (This is the official method, I think it is completely suitable for your current needs)
In this newly created slot you can use completely different environment variables in Configuration Settings.
This is the doc:
https://learn.microsoft.com/en-us/azure/azure-functions/functions-deployment-slots
I'd probably do it by having a single setting that holds a JSON array, viz
"Credentials": "[{'username':'***','password':'***'},{'username':'******','password':'******'}]"
Then, assuming you want to process them all at the same time, make a single function that parses the array and iterates over each username and password.
If you need to run them on different schedules, create a shared Python function DoTheThing(credentialIndex) that actually does the work and then multiple Azure Functions that simply call DoTheThing(0), DoTheThing(1), ...
(Security note: not immediately relevant to the problem at hand, but secrets are best kept in a secret store such as Key Vault rather than directly in the settings)
EDIT/SOLUTION:
I ended up having a the following keys in my environment variables:
"USERS": "[\"UserA\", \"UserB\"]"
"UserA_USER": "Username1"
"UserA_PW": "Password1"
"UserB_USER": "Username2"
"UserB_PW": "Password2"
Then I iterated over the USERS array and retrieved the keys for each user like so:
import os
import json
users = json.loads(os.environ["USERS"])
for u in users:
user = os.environ[u + "_USER"]
pw = os.environ[u + "_PW"]
doStuff(user, pw)

Azure Resource Manager - Compare Two Parameters for Equality

Is it possible to perform custom validation on two parameters and ensure they are equal?
I want to have something like password and password_confirm that must be equal before deploying any of the resources.
yeah, you can hack something like that, just create a resource that will fail and all the other resources would depend on it and then on the resource condition do:
"condition": "[not(equals(parameters('password'), parameters('password_confirm'))]"
that way if they are not equal fake resource would start getting deployed and would blow up (make sure you code it to blow up) and nothing would get deployed
now that I think of it, instead of creating a resource, just put a condition on all of the resources in the template:
"condition": "[equals(parameters('password'), parameters('password_confirm')]"
that way they will only get deployed if these match, but you won't have a failure.
Another option would be to add a parameter to do the validation... this is simpler but not as robust because the user could override the defaultValue of the parameter:
"validatePasswords": {
"type": "bool",
"allowedValues": [
true
],
"defaultValue": "[equals(parameters('password'), parameters('password_confirm'))]",
"metadata": {
"description": "Check to see if the 2 passwords match."
}
},
Putting a condition on each resource will work (and is harder to fool), but the deployment may succeed even though nothing is deployed.

Is it possible to create a CompositeIndex of CosmosDB with ARM template

I find instructions for using ARM templates to create or make changes to CosmosDB, but none of them contain instructions on how to add a CompositeIndex to the template. I have also heard it is not supported in the template and has to be done with PowerShell or Azure CLI script, but have not been able to find a supporting content on the net. Can someone please shed light on this?
I've not tested this but according to the Microsoft.DocumentDB resource provider docs / template reference there is a Microsoft.DocumentDB/databaseAccounts/apis/databases/containers resource which may give you what you need.
Every container has an IndexingPolicy in the template schema, which has an array of IncludedPath objects which themselves have an array of Index objects as follows:
"includedPaths": [
{
"path": "string",
"indexes": [
{
"dataType": "string",
"precision": "integer",
"kind": "string"
}
]
}
]
It's treated as a separate resource from the database / account altogether. You may want to add this resource to your template with an appropriate dependsOn value to ensure it's deployed after your database.
You can add multiple paths therefore making a composite index.
Full schema is here:
https://learn.microsoft.com/en-us/azure/templates/microsoft.documentdb/2015-04-08/databaseaccounts/apis/databases/containers
If this doesn't do it, you may want to look at this too as the schema docs may be out of date and compositeIndexes may be supported:
https://learn.microsoft.com/en-us/azure/cosmos-db/how-to-manage-indexing-policy#composite-indexing-policy-examples

Resources