Azure ARM templates – Example use of Outputs
2017, Nov 18
The Outputs part of an ARM template allow data to be retrieved as part of that templates creation.
Given a simple example of creating an vNet, here’s what we get.
Defining 3 outputs of differing types,
– Virtual Network [Object]
– Virtual Network Addresses [Array]
– Virtual Network Address Prefix [String] (first string in the array)
"outputs" : { "vNet" : { "type" : "object", "value" : "[reference(resourceId('Microsoft.Network/virtualNetworks', parameters('vnetName')))]" }, "vNetAddresses" : { "type" : "array", "value": "[reference(resourceId('Microsoft.Network/virtualNetworks', parameters('vnetName'))).addressSpace.addressPrefixes]" }, "vNetAddressSpace" : { "type" : "string", "value": "[reference(resourceId('Microsoft.Network/virtualNetworks', parameters('vnetName'))).addressSpace.addressPrefixes[0]]" } }
We get this output.
Outputs : Name Type Value =============== ========================= ========== vNet Object { "provisioningState": "Succeeded", "resourceGuid": "f8830a45-ee29-4932-889d-d6f34bb1817c", "addressSpace": { "addressPrefixes": [ "10.250.0.0/16" ] }, "subnets": [ { "name": "win-test-subnet", "id": "/subscriptions/2d5bb2c8-8be8-4539-b48f-fbfd86852fa9/resourceGroups/myTmpRg25/providers/Microsoft.Network/virtualNetwork s/apigw-test-vnet/subnets/apigw-test-subnet", "etag": "W/\"759461be-66a2-4268-b3b3-1a3201e00686\"", "properties": { "provisioningState": "Succeeded", "addressPrefix": "10.250.0.0/24" } } ] } vNetAddresses Array [ "10.250.0.0/16" ] vNetAddressSpace String 10.250.0.0/16 vNetAddressSpace String 10.250.0.0/16
Powershell script for deploying and retrieving values.
$vnet = New-AzureRmResourceGroupDeployment -Name vnetDeploy -ResourceGroupName $rgName -TemplateFile template.json -TemplateParameterFile parameters.json [psobject]$vnetAddresses = $vnet.Outputs.item("vNet").value.ToString() | ConvertFrom-Json [array]$vnetAddresses = $vnet.Outputs.item("vNetAddresses").value [string]$vnetAddressSpace = $vnet.Outputs.item("vNetAddressSpace").value
Taking a more practical example, retrieving the StaticIP’s from APIM after creation;
Template
"outputs" : { "ApimStaticIps" : { "type" : "array", "value": "[reference(resourceId('Microsoft.ApiManagement/service', variables('apiManagementServiceName'))).staticIps]" } } }
Powershell Output
Outputs : Name Type Value =============== ========================= ========== apimStaticIps Array [ "52.233.235.194", "10.250.1.5" ]