简体   繁体   中英

Azure ARM templates - using the output of other deployments

What I am interested in is reading the output parameters of another deployment in a different resource group. My ARM templates are something like:

  1. platform.json - sets up the DNS, virtual networks and security
  2. storage.json - sets up databases and other stores
  3. app.json - sets up the web app/api

Each is deployed in different resource groups as they have different life cycles. However, when I deploy the app.json I want to pull in the outputs of the latest platform and storage deployments and use them to configure the app.

Linked templates are not a solution as the linked templates end up deployed in the same resource group as the app which defeats the purpose of segregating your resources in resource groups.

Is there any way I can read the output parameters of a deployment from a different resource group? if not, is Azure planning to support it?

I know there is a way to get the resources by id, using the resourceId function, and look at their properties but I am trying to avoid doing that to not get into a resource reference spagetti.

How are you doing your deployments? In PowerShell you can do something like:

(Get-AzureResourceGroupDeployment NameOfResourceGroup).Outputs.NameOfOuputProperty.Value

And that will give you the output of the most recent deployment. You can also throw the entire deployment object into a var and have at it that way.

$d = Get-AzureResourceGroupDeployment NameOfResourceGroup

Which would be faster if you needed a many output properties.

That help?

Update for AzureRM cmdlet

The syntax is largely the same:

(Get-AzureRmResourceGroupDeployment -ResourceGroupName NameOfResourceGroup -Name NameOfDeployment).Outputs.NameOfOutputProperty.value

If you have multiple deployments you can use:

Get-AzureRmResourceGroupDeployment -ResourceGroupName NameOfResourceGroup 

To see them all and see what the names are...

I know this is an old question but for others who come along, as of 03/12/18 you can definitely do this.

You need to ensure your output is formatted as per the Microsoft documentation for output variables which broadly has the format

"outputs": {
  "resourceID": {
    "type": "string",
    "value": "[resourceId('Microsoft.Network/publicIPAddresses', parameters('publicIPAddresses_name'))]"
  }
}

You can then use these outputs in your templates by referencing the deployment using a resource reference which has the format

reference(resourceName or resourceIdentifier, [apiVersion], ['Full'])

Note that you will need to provide the api version, as the deployment may use a different api version to the one your parent template uses.

Your reference would then look something like the following

{
  "comments": "This would have an output named myOutput you want to use",
  "apiVersion": "2017-05-10",
  "type": "Microsoft.Resources/deployments",
  "name": "my-deployment",
  "resourceGroup": "...",
  "properties": {
    "mode": "Incremental",
    "templateLink": {
      "uri": "...",
      "contentVersion": "1.0.0.0"
    },
    "parameters": { }
},
{
  "comments": "This makes use of myOutput from my-deployment",
  "apiVersion": "2017-05-10",
  "type": "Microsoft.Resources/deployments",
  "name": "my-dependent-deployment",
  "properties": {
    "mode": "Incremental",
    "templateLink": {
      "uri": "...",
      "contentVersion": "1.0.0.0"
    },
    "parameters": {
      "myValueFromAnotherDeployment": { "value": "[reference('my-deployment', '2017-05-10').outputs.myOutput.value]" }
    }
  }
}

Note the slightly awkward "repackaging" of the value, where we use myOutput.value as the input to the dependent deployment, and put that in an object with the key "value": "...." . This is because ARM parameters must have a 'value' property in order to be valid.

You'll get invalid template errors if you try to use the output directly (because output variables have a 'type' and this is not an allowed key in a parameter ). That's why you need to get the value property and then put it back into the value in downstream templates.

Are you using Azure DevOps Release Pipelines? You could just set the output to be created as variables so you can re-use them in the same or a different stage.

We use these extensions on our projects

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM