BICEP - Parameter file variable assignment - azure

I was following the repo for separate parameter file to each env as defined in the https://github.com/Azure/bicep/discussions/4586
I tried the separate parameters file for dev, stage, prod but the value assignment in main module variable remains flagged by intelligence even though it exists same param exist in the respective parameter file.
Other approach I tried is loadjson variable, but it does not show auto completion for items under subnet block as it stopes right after value.
Maybe I am overthinking and not applying the correct approach, Perhaps I should ignore intellisense and try deploying by applying parameter and hope it will auto pick correct value during the deployment param check.
Here is my parameter file and the same value applies to each env param json.
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"department": {
"value": "finance"
},
"saAccountCount": {
"value": 1
},
"vmCount": {
"value": 1
},
"locationIndex": { //idenx 1 = app server, 2=AD, 3=Tool server, 4= dchp server
"value": 1
},
"appRoleIndex": { //idenx 1 = westus2, 2= westus, 3= eastus, 4=centralus, 5=uswest3
"value": 1
},
"appRole": {
"value": {
"Applicatoin Server": "ap",
"Active Directory": "dc",
"Tool server": "tool",
"DHCP server": "dhcp"
}
},
"environment": {
"value": "dev"
},
"addressPrefixes": {
"value": [
"172.16.0.0/20"
]
},
"dnsServers": {
"value": [
"1.1.1.1",
"4.4.4.4"
]
},
"locationList": {
"value": {
"westus2": "azw2",
"westus": "azw",
"Eastus": "aze",
"CentralUS": "azc",
"westus3": "azw3"
}
},
"subnets": {
"value": [
{
"name": "frontend",
"subnetPrefix": "172.16.2.0/24",
"delegation": "Microsoft.Web/serverfarms",
"privateEndpointNetworkPolicies": "disabled",
"serviceEndpoints": [
{
"service": "Microsoft.KeyVault",
"locations": [
"*"
]
},
{
"service": "Microsoft.Web",
"locations": [
"*"
]
}
]
},
{
"name": "backend",
"subnetPrefix": "172.16.3.0/24",
"delegation": "Microsoft.Web/serverfarms",
"privateEndpointNetworkPolicies": "enabled",
"serviceEndpoints": [
{
"service": "Microsoft.KeyVault",
"locations": [
"*"
]
},
{
"service": "Microsoft.Web",
"locations": [
"*"
]
},
{
"service": "Microsoft.AzureCosmosDB",
"locations": [
"*"
]
}
]
}
]
}
}
}

You appear to be attempting to deploy an Azure Resource Management (ARM) template using a parameter file.
The parameter file is used to pass values to the ARM template during deployment. The parameter file must use the same types as the ARM template and can only include values for the ARM template's parameters.
You will receive an error if the parameter file contains extra parameters that do not match the ARM template's parameters.
In the same deployment process, you can use both inline parameters and a local parameter file. If you specify a parameter's value in both the local parameter file and inline, the inline value takes priority.
Refer to create a parameter file of an ARM template
About the different parameters file for dev, stage, and prod, it's likely that the parameter file is not correctly linked to the ARM template.
You can deploy the ARM template with the parameter file to determine if it will automatically select the proper value during the deployment parameter check.
Regarding the loadjson variable, it is possible that the loadjson variable is not properly formatted.
You can double-check the loadjson variable's format to ensure it's proper.
After a workaround on this, I created a sample parameter.json file for a webapp to deploy in a production environment and that worked for me.
Note: Alternatively, You can use az deployment group create with a parameters file and deploy into Azure to avoid these conflicts.

Related

Key Vault ipRules property as parameter issue

I am trying to add Firewall rules for Azure Key Vault using ARM templates. It works as expected if ipRules property in conjunction with multiple IPs are defined in template (not as parameter).
However, if I try to define it as parameter getting "Bad JSON content found in the request."
Property defined in Template ("apiVersion": "2019-09-01"):
"kv-ipRules": {
"type": "array",
"metadata": {
"description": "The address space (in CIDR notation) to use for the Azure Key Vault to be deployed as Firewall rules."
}
}
"networkAcls": {
"defaultAction": "Deny",
"bypass": "AzureServices",
"virtualNetworkRules": [
{
"id": "[concat(parameters('kv-virtualNetworks'), '/subnets/','kv-subnet')]",
"ignoreMissingVnetServiceEndpoint": false
}
],
"ipRules": "[parameters('kv-ipRules')]"
}
Property defined in Parameters:
"kv-ipRules": {
"value": [
"xx.xx.xx.xxx",
"yy.yy.yy.yyy"
]
}
Given the documentation (https://learn.microsoft.com/en-us/azure/templates/Microsoft.KeyVault/vaults?tabs=json#IPRule), I would use:
"kv-ipRules": {
"value": [
{
"value": "xx.xx.xx.xxx"
},
{
"value": "yy.yy.yy.yyy"
}
]
}

applicationGatewayBackendAddressPools configurations does not apply in virtual machine scale set

I have a VMSS which I deployed using ARM templates. This is the networkProfile block under VMSS resource section.
"networkProfile": {
"networkInterfaceConfigurations": [
{
"name": "[variables('nicName')]",
"properties": {
"primary": true,
"ipConfigurations": [
{
"name": "[concat(variables('VMSSName'), '-ipconfig')]",
"properties": {
"subnet": {
"id": "[variables('subnetRef')]"
},
"applicationGatewayBackendAddressPools": "[variables('AppGatewayBackendAddressPool')]"
}
}
]
}
}
]
},
In Variable section, if I use resourceId() function and provide values from parameters then it does not apply the configuration in VMSS. for example:
"AppGatewayBackendAddressPool": "[resourceId(parameters('VirtualNetworkResourceGroup'),'Microsoft.Network/applicationGateways/backendAddressPools', parameters('ApplicationGatewayName'), parameters('BackendAddressPool'))]",
I've also tried adding parameters('SubscriptionName') but the result is same.
"AppGatewayBackendAddressPool": "[resourceId(parameters('SubscriptionName') ,parameters('VirtualNetworkResourceGroup'),'Microsoft.Network/applicationGateways/backendAddressPools', parameters('ApplicationGatewayName'), parameters('BackendAddressPool'))]",
When I declare variable like below then it applies backendAddressPool configuration in Networking -> Load Balancing.
"AppGatewayBackendAddressPool": [
{ "id": "/subscriptions/<subscriptionId>/resourceGroups/<resourceGroupName>/providers/Microsoft.Network/applicationGateways/<applicationGatewayName>/backendAddressPools/<backendAddressPool>" }
],
Similar I'm doing with subnetRef like below and that is working fine.
"subnetRef": "[resourceId(parameters('VirtualNetworkResourceGroup'), 'Microsoft.Network/virtualNetworks/subnets', parameters('VirtualNetworkName'), parameters('SubnetName'))]",
I want to parametrize the deployment by defining separate parameters.json file so I can attach applicationGatewayBackendAddressPools with different virtual machine scale sets.
This is how I achieved it by following Ked Mardemootoo answer.
IP configuration section under networkProfile of VMSS resource.
"ipConfigurations": [
{
"name": "[concat(variables('VMSSName'), '-ipconfig')]",
"properties": {
"subnet": {
"id": "[variables('subnetRef')]"
},
"applicationGatewayBackendAddressPools": [
{ "id": "[concat(parameters('AapplicationGatewayExternalid'), '/backendAddressPools/', parameters('BackendAddressPool'))]" }
]
}
}
]
Template file parameters:
"BackendAddressPool": {
"type": "string",
"metadata": {
"description": "Backend pool to host blue/green vmss."
}
},
"AapplicationGatewayExternalid": {
"type": "string",
"metadata": {
"description": "Application Gateway Id."
}
}
Now, ARM template is calling and referencing applicationGatewayBackendAddressPools attribute dynamically under VMSS' resource section.
I have these two parameters in parameters.json file where I can define values according to environment.
"BackendAddressPool": {
"value": "<backendPoolName>"
},
"AapplicationGatewayExternalid": {
"value": "/subscriptions/<subscriptionId>/resourceGroups/<resourceGroupName>/providers/Microsoft.Network/applicationGateways/<ApplicationGatewayName>"
}
Overriding template variables in release pipeline vars:
overriding template vars
Defining in pipeline vars
pipeline var
You seem to be missing the concat in the variables. Looking at the raw json on my end, this is how it's configured. See if you can do something similar, and convert the subnet name and backend address pool to variables.
"ipConfigurations": [
{
"name": "ip-vmss-name",
"properties": {
"primary": true,
"subnet": {
"id": "[concat(parameters('virtualNetworks_vnet_externalid'), '/subnets/snet-vm')]"
},
"privateIPAddressVersion": "IPv4",
"applicationGatewayBackendAddressPools": [
{
"id": "[concat(parameters('applicationGateways_agw_1_externalid'), '/backendAddressPools/be-addr-pool-vmss-1')]"
}
]
}
}
]
Nothing seems wrong with your variables/parameters call but applicationGatewayBackendAddressPools is not a valid attribute for neither VMSS nor Application Gateway.
You can do it check AKS and Application Gateway documentations. I achieve the same goal by setting backendAddressPools, which is in Application Gateway section, in different parameters.json files.

Using Empty/Optional Parameters in Azure ARM Template

I'm having issues setting up an ARM Template for Azure Web Apps in that I can't add ConnectionString parameters where it sets the values if the parameters are set, but leave blank (default) if the parameters aren't set.
Here is how it looks in the template.json file:
"connectionStrings": [
{
"name": "[parameters('connString').connName)]",
"connectionString": "[parameters('connString').string]",
"type": "[parameters('connString').connType]"
}
],
And in the parameters.json file:
"connString": {
"value": {
"connName": "",
"string": "",
"connType": ""
}
},
When running the deployment with the above it fails on "Parameter name cannot be empty"
I attempted to use an equals function to set the value as empty if the parameter is empty, but set the parameter if the parameter is filled out, however it doesn't like the empty value.
"name": "[if(equals(parameters('connString').connName,''),'',parameters('connString').connName)]"
Also attempted an empty function:
"name": "[not(empty(parameters('connString').connName))]"
However this returns "False" if empty and "True" if the parameter is set (as designed)
The deployment works fine if I set dummy values as the parameters, is it possible to set a function or something similar so if the parameter is empty it uses whatever value is sent as if the connectionStrings section wasn't present in the template? These parameters are optional but it looks like because they're in the actual template.json file its expecting a value.
Cheers
EDIT
Going to post what my end templates looked like in case someone else needs assistance.
Template File
Variables
"variables": {
"empty": []
},
Resources
"connectionStrings": "[if(empty(parameters('connString')), variables('empty'), array(parameters('connString')))]",
Parameter File
If setting a connection string
"connString": {
"value": [{
"name": "test",
"connectionString": "ufgndjkngsdgjkn",
"type": "Custom"
}]
},
If not wanting to set a connection string
"connString": {
"value": [
]
},
you should just do this:
parameters:
"connString": {
"value": {}
},
variables:
"empty": [],
template:
"connectionStrings": "[if(empty(parameters('connString')), variables('empty'), array(parameters('connString')))]"
you could try:
parameters:
"connString": [{
"value": {
"name": null,
"connectionString": null,
"type": "Custom"
}
}],
variables:
template:
"connectionStrings": "[if(empty(parameters('connString')[0].value.name), json('null'), parameters('connString'))]"

Azure: Cannot use output from Linked Template in IF condition

I'm trying to deploy a Virtual network using ARM template with multiple subnets. I have a linked template which creates NSGs,route table and assigns it to a specific subnet. I'm using copy to create multiple subnets. Route table should be assigned to only one particular subnet. I control this using If condition. The issue here is i'm not able to use the linked template output in the If condition. It fails with below error.
'{
"error": {
"code": "InvalidTemplate",
"message": "Unable to process template language expressions for resource
'/subscriptions//resourceGroups//providers/Microsoft.Network/virtualNetworks/' at line '143' and column '9'. 'The provided
arguments for template language function 'if' is not valid: all arguments should be of type 'boolean'. Please see https://aka.ms/arm-template-expressions#if for usage details.'"
}
}'
Main Template (Subnet Creation snippet):-
"copy": [
{
"name": "subnets",
"count": "[length(parameters('subnetList'))]",
"input": {
"name": "[parameters('subnetList')[copyIndex('subnets')].name]",
"properties": {
"addressPrefix": "[parameters('subnetList')[copyIndex('subnets')].addressprefix]",
"networkSecurityGroup": "[if(equals(parameters('subnetList')[copyIndex('subnets')].name, 'GatewaySubnet'), json('null'), variables('nsgId'))]",
"routeTable": "[if(bool(parameters('subnetList')[copyIndex('subnets')].useRouteTable), reference('routeTableDeployment').outputs.resourceID.value, json('null'))]"
}
}
}
]
Route Table (Output snippet):-
"outputs": {
"resourceID": {
"type": "object",
"value": {
"id": "[resourceId('Microsoft.Network/routeTables', variables('routeTableName')]"
}
}
}
It works if I define a variable and pass it to if condition similar to nsg as below:-
"routeTableId": {
"id": "[resourceId('Microsoft.Network/routeTables', 'routeTableName')]"
}
"routeTable": "[if(bool(parameters('subnetList')[copyIndex('subnets')].useRouteTable), variables('routeTableId'), json('null'))]"

using conditional IF statements in arm templates for properties,

I have a situation where I want to only add a property to a VM if a condition is met. For example if I want to add an availability set property to a machine then do this : Below I ONLY what to execute the availability set statement if a condition is TRUE, can you do this in an ARM template? eg if a value is true then do this line, if not skip?
{
"name": "[parameters('ComputerName')]",
"type": "Microsoft.Compute/virtualMachines",
"location": "[parameters('location')]",
"apiVersion": "2017-03-30",
"dependsOn": [
"[resourceId('Microsoft.Network/networkInterfaces', variables('1stNicName'))]",
"[resourceId('Microsoft.Network/networkInterfaces', variables('2ndicName'))]"
],
"tags": {
"displayName": "[parameters('ComputerName')]"
},
"properties":
{
"availabilitySet": {
"id": "[resourceId('Microsoft.Compute/availabilitySets',variables('availabilitySetName'))]"
},
"hardwareProfile": {
"vmSize": "[parameters('serverVmSize')]"
},
"osProfile": {
"computerName": "[parameters('serverName')]",
"adminUsername": "[parameters('adminUsername')]",
"adminPassword": "[parameters('adminPassword')]"
},
As of this week (1st August 2017), ARM templates now have an IF function that can be used for variables, properties and resource parameters. It works like most other ARM functions, with syntax like:
[if(condition, true value, false value)]
The example in the documentation does exactly what you are asking, adds (or doesn't add) an availbility set to a VM:
"apiVersion": "2016-03-30",
"type": "Microsoft.Compute/virtualMachines",
"properties": {
"availabilitySet": "[if(equals(parameters('availabilitySet'),'yes'), variables('availabilitySet'), json('null'))]",
...
}
Note that the True value, the availiblity set to use, is stored as a variable rather than inline in the if statement.
"variables": {
...
"availabilitySetName": "availabilitySet1",
"availabilitySet": {
"id": "[resourceId('Microsoft.Compute/availabilitySets',variables('availabilitySetName'))]"
}
},
There is no direct way of doing this. you can use conditional to choose a value, but you cannot not set a value or pass in null (unless you want to define 2 variables for `properties').
so you can just define 2 vm resources that are exactly the same except for the property in question (so availabilitySet). One would have it and the other one would not. And you would use "condition": expression in both resources. Condition should equals to false or true to work. There are several functions in ARM templates that can evaluate input and return true or false.
Reference:
https://samcogan.com/conditions-in-arm-templates-the-right-way/
"variables": {
"loadBalancerBackendAddressPools": [
{
"id": "[parameters('lbBackendAddressPool')]"
}
]
},
Believe i have found a workable way to avoid defining two entire VM definitions for an availability set. following are from my own testing in an arm template i have made with extensive conditional controls.
this works:
"availabilitySet": {
"id": "[resourceId('Microsoft.Compute/availabilitySets',parameters('availabilitySetName'))]"
}
this does not:
"availabilitySet": "[if(equals(parameters('availabilitySet'),'yes'), variables('availabilitySet'), json('null'))]",
this also does not work due to ID being 'null':
"availabilitySet": {
"id": "[if(equals(parameters('availabilitySet'),'yes'), variables('availabilitySet'), json('null'))]"
}
I have added the following variable and parameter:
"parameters": {
"SpotInstance": {
"type": "bool",
"defaultValue": false,
"allowedValues": [
true,
false
],
"metadata": {
"description": "Should the VM be deployed as a low cost Spot Instance, this will deploy without attaching the Availability Set."
}
}
},
"variables": {
"AvailibilitySet": {
"On":{
"id":"[resourceId('Microsoft.Compute/availabilitySets/',variables('ASName'))]"
},
"Off":"[json('null')]"
}
}
then used this logic on the availability set assignment:
"availabilitySet": "[if(equals(parameters('SpotInstance'),bool('true')),variables('AvailibilitySet').Off,variables('AvailibilitySet').On)]",
if you do not define id as a property of "AvailabilitySet" this can be null, if you Do however it cannot be null.
When i try the same for loadbalancer backend pool i get errors:
12:21:49 - [ERROR] "message": "Cannot deserialize the current JSON object (e.g.
12:21:49 - [ERROR] {\"name\":\"value\"}) into type 'Microsoft.WindowsAzure.Networking.Nrp.Frontend
12:21:49 - [ERROR] .Contract.Csm.Public.ResourceReferenceHashSet`2[Microsoft.WindowsAzure.Networki
12:21:49 - [ERROR] ng.Nrp.Frontend.Contract.Csm.Public.LoadBalancerBackendAddressPool,Microsoft.Wi
12:21:49 - [ERROR] ndowsAzure.Networking.Nrp.Frontend.Contract.Csm.Public.NetworkInterfaceIpConfig
12:21:49 - [ERROR] uration]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize
12:21:49 - [ERROR] correctly.\r\nTo fix this error either change the JSON to a JSON array (e.g.
12:21:49 - [ERROR] [1,2,3]) or change the deserialized type so that it is a normal .NET type
12:21:49 - [ERROR] (e.g. not a primitive type like integer, not a collection type like an array
12:21:49 - [ERROR] or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute
12:21:49 - [ERROR] can also be added to the type to force it to deserialize from a JSON
variables
"availabilitySet": {
"id": "[resourceId('Microsoft.Compute/availabilitySets',parameters('availabilitySetName'))]"
},
"loadBalancerBackendAddressPools": {
"id": "[resourceId('Microsoft.Network/loadBalancers',parameters('LoadBalancername'))]"
},
"loadBalancerBackendAddressPools": "[if(equals(parameters('DeployavailabilitySet'),'yes'), variables('loadBalancerBackendAddressPools'), json('null'))]",

Resources