I have an issue with a ARM template in which I replace a certain part with a template function. This happens during the execution of the release pipeline. The ARM template is installed by a Azure Powershell Script which I added to the release pipeline.
Here is the relevant part throwing the error:
"parameters": {
"connections_office365_externalid": {
"defaultValue": "[concat(resourceGroup().id,'/providers')]/Microsoft.Web/connections/office365",
"type": "String"
},
"connections_sql_externalid": {
"defaultValue": "[concat(resourceGroup().id, '/providers')]/Microsoft.Web/connections/sql25",
"type": "String"
}
},
When it comes to the deployment of the ARM template I get the following error:
> Status Message: Property id '[concat(resourceGroup().id,
> '/providers')]/Microsoft.Web/connections/office365' at path
> 'properties.parameters.$connections.value.office365.connectionId' is
> invalid. Expect fully qualified resource Id that start with
> '/subscriptions/{subscriptionId}' or
> '/providers/{resourceProviderNamespace}/'.
> (Code:LinkedInvalidPropertyId)
It seems that the part with the template function is not executed because the function is not translated into the appropriate value.Does anybody know what could be the issue that this template function is not executed on runtime of the release pipeline?`
UPDATE:
This is how the DefaultValue Part is replaced prior deployment:
I replace a particular part of the string (regex) to have it dynamic
(Get-Content $file.FullName -Raw) -replace "\/subscriptions\/(.*?)\/resourceGroups\/customerPrefix(.*?)\/providers","[concat(resourceGroup().id, '/providers')]" | Set-Content $file.FullName
If you are setting up Connections through ARM templates for Logic Apps, go ahad and use this piece:
"office365_1": {
"id": "[concat(subscription().id, '/providers/Microsoft.Web/locations/', parameters('logicAppLocation'), '/managedApis/', 'office365')]",
"connectionId": "[resourceId('Microsoft.Web/connections', parameters('office365_1_Connection_Name'))]",
"connectionName": "[parameters('office365_1_Connection_Name')]"
}
You just need to change your replacing script a little bit to make it work. See below:
I have tested and found the expression [concat(resourceGroup().id,'/providers')]/Microsoft.Web/connections/office365 cannot be evaluated in the template.
The correct expression should be like below:
"defaultValue": "[concat(resourceGroup().id, '/providers', '/Microsoft.Web/connections/office365')]"
Since you want to replace a particular part of the string (regex) to have it dynamic. You can change your replacing script like:
(Get-Content $file.FullName -Raw) -replace "\/subscriptions\/(.*?)\/resourceGroups\/(.*?)\/providers(\/.*)`"",'[concat(resourceGroup().id, ''/providers'', ''$3'')]"' | Set-Content $file.FullName
Above script will replace relevant string to be like this "[concat(resourceGroup().id, '/providers', '/Microsoft.Web/connections/sql25')]",
Related
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
I feel really dump right now but I just can't get this to work. Kind of new to ARM Templates so sorry for my ignorance.
I am trying to use a parameters.json with New-AzResourceGroupDeployment but I want to dynamically feed it in the VMName.
I am trying to use this for the NSG name:
"value": "[concat(variables('vmName'),'-nsg')]"
But I get back an error of:
New-AzResourceGroupDeployment: 6:39:21 AM - Error:
Code=InvalidResourceName; Message=Resource name
[concat(variables('vmName'),'-nsg')] is invalid. The name can be up to
80 characters long. It must begin with a word character, and it must
end with a word character or with ''. The name may contain word
characters or '.', '-', ''.
What am I missing?
Where do you use the Concat function? Because ARM template functions are only available in the ARM template itself, not in the .parameters.json file.
Edit as a response:
It really depends on the use case but I would do something like this in the main ARM template if the 'nsg' value does not change for the given ARM template. If it does then define a second parameter 'vmsuffix' and concat both parameters into the full VMname.
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"VmName": {
"type": "string",
"defaultValue": ""
}
},
"variables": {
"FullVmName": "[concat(parameters('VmName'), 'nsg')]"
},
"resources": [
{
...
## Use the [variables('FullVmName') somewhere here
}
]
}
If I try to deploy my arm Template (Something like this)
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {},
"variables": {
"AAS": "TestAAS",
"AFU": "TestAFU",
},
"resources": [
//define some resource here
],
"outputs": {
"asName": {
"type": "string",
"value": "[variables('AAS')]"
},
"azureFunctionName": {
"type": "string",
"value": "[variables('AFU')]"
}}
}
if for any reason this isn't going well, I can not read output in Powershell. and I get the following message:
Template output evaluation skipped: at least one resource deployment operation failed. Please list deployment operations for details
what should I do so that the output parameters are passed to powershell script despite faulty execution
My Powershell code:
//Standard PowerShell code for Deploying ARM Template
try
{
Stop-AzureRmWebApp -ResourceGroupName $ResourceGroupName -Name $deployment.Outputs.item("AFU").value
Suspend-AzureRmAnalysisServicesServer -Name $deployment.Outputs.item("AAS").value -ResourceGroupName $ResourceGroupName
}
catch
{
Write-Host "error here"
}
You cant do anything here. Outputs are only being generated if there were no errors in the ARM Template flow. So you need your ARM Template to succeed to be able to retrieve those (doesn't matter which tool you use, API behind those is always the same one).
I have a misunderstanding how blueprint outputs works and how to properly import values from one artifact to another.
Let me describe my attempts to get variable from artifact:
I have created two artifacts inside resource groups:
I have tried to transfer variables like vnet_name, vnet_addresses from VNET artifact to SUBNET_AKS artifact using the following syntax:
VNET:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
........
"outputs": {
"vnet_name_output": {
"type": "string",
"value": "[variables('vnet_name')]"
},
"vnet_ip_range_output": {
"type": "string",
"value": "[parameters('vnet_ip_range')]"
},
"vnet_path_output": {
"type": "string",
"value": "[resourceId('Microsoft.Network/virtualNetworks', variables('vnet_name'))]"
}
}
}
Next step was to add output variable to SUBNET_AKS artifacts:
"resources": [
{
"apiVersion": "2018-04-01",
"type": "Microsoft.Network/virtualNetworks/subnets",
"name": "[concat(artifacts('VNET').outputs.vnet_name_output, '/', concat(parameters('deployment_prefix'),'-aks-subnet'))]",
But the following error appears:
Deployment template validation failed: 'The template resource '[concat(artifacts('VNET').outputs.vnet_name_output, '/', concat(parameters('deployment_prefix'),'-aks-subnet'))]' at line '27' and column '9' is not valid: The template function 'artifacts' is not valid. Please see https://aka.ms/arm-template-expressions for usage details.. Please see https://aka.ms/arm-template-expressions for usage details.'.
How can I understand how outputs parameters should properly work in Azure Blueprint definition?
Azure Blueprint is just an orchestration layer responsible for the ordering and deployment of artifact(s). ARM templates are one of three valid types - policyAssignment and roleAssignment are the other two.
This means you have two "template" artifacts: VNET and SUBNET_AKS. Each one should be treated like an actor / black-box, meaning you can only use functions available to ARM templates. If you need a parameter from the Blueprint it must come in as a parameter.
That is why you are getting that particular syntax error. The artifacts() function is only available to Blueprints.
Instead, you need to update your ARM template so that it specifies a named output value. In your Azure Blueprint, you can reference the output of a prior artifact as an input parameter to a subsequent Blueprint artifact.
Hopefully these code snippets and docs can point you in the right direction.
I can't seem to find any way to use the value of a parameter as part of another parameter in a parameter file for ARM templates:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"someCustomParam": {
"value": "desired value"
},
"tags": {
"value": {
"tag1": "[parameters('someCustomParam')]",
"tag2": "some tag value"
}
},
}
}
Notice how I want to use the value of a previous parameter for the value of another.
The value for "tag1" is simply the string and the value does not get substituted in from the parameter() function. I've tested this by using the Test-AzResourceGroupDeployment PowerShell cmdlet.
Is there any way I can do this?
You can do this using PowerShell. Before calling Test-AzResourceGroupDeployment you can get content of parameter file in PowerShell variable/object using
$ParameterObject = Get-Content ./ParameterFileName.json
Update required value like:
$ParameterObject.parameters.tags.value.tag1 = #Value to assign
Pass $parameterObject to -TemplateParameterObject parameter of Test-AzResourceGroupDeployment
------OR------
Convert $parameterObject to JSON using ConvertTo-Json and Save as temp.json and pass temp.json to -TemplateParameterFile [reference below]
$TempParameterFile = ( $ParametersObject | ConvertTo-Json -Depth 20 ) -replace "\\u0027", "'" -replace "\\u0026", "&" | Out-File $tmp -Force
and use $TempParameterFile for -TemplateParameterFile
You need to use variables.
In variables, you can use "[parameters('parameterName')]".
And you can use variables in a similar way as parameters : "[variables('variableName')]"
Update:
Here is a sample:
"parameters": {
"someCustomParam": {
"type": "string"
}
},
"variables": {
"tags": {
"tag1": "[parameters('someCustomParam')]",
"tag2": "some tag value"
}
}
And then you can use the variable in the resource of your template.
You can do this just like what you normally do when you reference a parameter, an example as following:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"paramOne": {
"value": "hello"
},
"paramTwo": {
"value": "[concat(parameters('paramOne'), '-', 'world)]"
}
}
}
the output value of 'paramTwo' will be 'hello-world'.
Hope this helps whoever wants to utilize referencing a parameter in a parameter in a parameter file.