2
votes

I'm working on a custom arm template.

I would like to have specific resource group to be hard coded inside the JSON so when opening the template it will simulate "Resource Group: Use existing: predefined selected Resource group"

I've been scratching my head for hours and searched the web deeply, I also tried to export existing resource group template and import it to custom deployment but it still shows

Resource Group *Create New *Use Existing

Is there any way to define existing RG inside the JSON template?

2

2 Answers

2
votes

You can use nested templates, like @4c74356b41 said, but you will still see the ugly "Select a resource group" field at the portal.

I have a similar problem (even if @4c74356b41 repeatedly claims that it doesn't make any sense). I want to generate the resource group name from a parameter.

You can find more about how to use nested templates here: Create resource group and deploy resources

{
    "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
    "contentVersion": "1.0.0.1",
    "parameters": {
        "someName": {
            "type": "string"
        }
    },
    "variables": {
        "rgName": "[concat('rg-', parameters('someName'))]"
    },
    "resources": [
        {
            "type": "Microsoft.Resources/resourceGroups",
            "apiVersion": "2018-05-01",
            "location": "[parameters('rgLocation')]",
            "name": "[variables('rgName')]",
            "properties": {}
        },
        {
            "type": "Microsoft.Resources/deployments",
            "apiVersion": "2018-05-01",
            "name": "rgDeployment",
            "resourceGroup": "[variables('rgName')]",
            "dependsOn": [
                "[resourceId('Microsoft.Resources/resourceGroups/', variables('rgName'))]"
            ],
            "properties": {
                "mode": "Incremental",
                "template": {
                    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                    "contentVersion": "1.0.0.0",
                    "parameters": {},
                    "variables": {},
                    "resources": [
                        {
                            // PUT YOUR RESOURCES TEMPLATES HERE! //
                        }
                    ],
                    "outputs": {}
                }
            }
        }
    ],
    "outputs": {}
}

Just replace the rgName variable with the name of your actual resource group name.

0
votes

there are several ways to achieve this (not that it makes sense, but you can do this).

  1. Use automation around the template to always deploy it to the same rg. this makes most sense as your template stays flexible
  2. wrap your template with a parent template (so "convert" your template to a nested template). that way the parent template can control to which resource group your nested template gets deployed (look for cross resource group ARM Template deployments).
  3. Make your template a nested inline template (worst case). this is pretty much the same as point 2, but kinda worse, because nested inline templates have this peculiar drawback of not being able to use their own parameters\variables, only the ones defined in the parent.

Again, none of this makes sense as you should just deploy it to the proper subscription\resource group combination. but there you have it, if you insist.

But the portal experience will stay the same (there is no way of working around that, you can forcé the template to always deploy to the same resource group (not that it makes any sense), but not alter the portal experience), if thats what you are concerned about.