简体   繁体   中英

Azure tagging a resource group using an ARM template

How I can tagging a Azure resource group using an ARM template and use Azure DevOps task Azure Deployment: Create Or Update Resource Group. I have error No HTTP resource was found that matches the request URI


{
  "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {

    "customTags": {
      "type": "object",
      "defaultValue": {
        "Environment Name": "TRdev",
        "Environment Type":"Dev",
        "Product family":"RT"
      }
    },
    "rgName": {
      "type": "string",
      "defaultValue": "dev-rg",
      "metadata": {
        "description": "Name of the resourceGroup to create"
      }
    },
    "serverfarms_environment_sp_sku": {
        "defaultValue": "B1",
        "allowedValues": [ "B1", "S1", "P1V2", "P2V2", "P3V2"],
        "type": "String"
    },
    "serverfarms_environment_sp_name": {
        "defaultValue": "dev-sp",
        "type": "String"
    },
    "sites_environment_api_name": {
        "defaultValue": "dev-api",
        "type": "String"
    },
    "sites_environment_ui_name": {
        "defaultValue": "dev-ui",
        "type": "String"
    }   
  },
  "variables": { },
  "resources": [
         {
      "type": "Microsoft.Resources/resourceGroups",
      "apiVersion": "2018-05-01",
      "location": "West US",    
      "name": "[parameters('rgName')]",            
      "tags": "[parameters('customTags')]",
      "properties": {}
   },
    {
      "apiVersion": "2019-08-01",
      "name": "[parameters('rgName')]",
      "type": "Microsoft.Resources/deployments",
      "resourceGroup": "[parameters('rgName')]",
      "tags": "[parameters('customTags')]",
      "properties": {
        "mode": "Incremental",
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
          "contentVersion": "1.0.0.1",
          "parameters": {},
          "resources": [
                {
                "apiVersion": "2018-02-01",
                "type": "Microsoft.Web/serverfarms",
                "kind": "app",
                "name": "[parameters('serverfarms_environment_sp_name')]",
                "location": "[resourceGroup().location]",
                "tags": "[parameters('customTags')]",
                "properties": {},
                "dependsOn": [],
                "sku": {
                    "name": "[parameters('serverfarms_environment_sp_sku')]"
                    }
                },
                {
                "apiVersion": "2018-11-01",
                "type": "Microsoft.Web/sites",
                "kind": "app",
                "name": "[parameters('sites_environment_api_name')]",
                "location": "[resourceGroup().location]",
                "tags": "[parameters('customTags')]",
                "properties": {
                    "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('serverfarms_environment_sp_name'))]"
                },
                "dependsOn": [
                    "[resourceId('Microsoft.Web/serverfarms', parameters('serverfarms_environment_sp_name'))]"
                ]
                },
                {
                "apiVersion": "2018-11-01",
                "type": "Microsoft.Web/sites",
                "kind": "app",
                "name": "[parameters('sites_environment_ui_name')]",
                "location": "[resourceGroup().location]",
                "tags": "[parameters('customTags')]",
                "properties": {
                    "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('serverfarms_environment_sp_name'))]"
                },
                "dependsOn": [
                    "[resourceId('Microsoft.Web/serverfarms', parameters('serverfarms_environment_sp_name'))]"
                ]
                }
          ]
        },
        "parameters": {}
      }
    }
  ],
  "outputs": {
    "sites_environment_api_name": {
      "type": "string",
      "value": "[parameters('sites_environment_api_name')]"
    },
    "sites_environment_ui_name": {
      "type": "string",
      "value": "[parameters('sites_environment_ui_name')]"
    }
  }
}



Error

error No HTTP resource was found that matches the request URI ' https://management.azure.com/subscriptions/subscriptionsID/resourcegroups/resourcegroupsNeme/providers/Microsoft.Resources/resourceGroups/resourcegroupsNeme?api-version=2018-05-01 '.


Thanks.

What you're trying to achieve is referred to as a subscription level deployment. If you are trying to deploy this through a template via the portal I'm afraid you're out of luck. The deployment UI insists you specify a resource group to deploy in to which invalidates the API path routing when making the call to create your resource group.

To overcome this you'll need to use PowerShell or the Azure CLI.

Another issue with subscription level deployments is that you can't use the resourceGroup() function, so your template will need be adjusted.

Your template should be:

{
    "$schema":"https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
    "contentVersion":"1.0.0.0",
    "parameters":{
        "customTags":{
            "type":"object",
            "defaultValue":{
                "Environment Name":"TRdev",
                "Environment Type":"Dev",
                "Product family":"RT"
            }
        },
        "rgName":{
            "type":"string",
            "defaultValue":"dev-rg",
            "metadata":{
                "description":"Name of the resourceGroup to create"
            }
        },
        "serverfarms_environment_sp_sku":{
            "defaultValue":"B1",
            "allowedValues":[
                "B1",
                "S1",
                "P1V2",
                "P2V2",
                "P3V2"
            ],
            "type":"String"
        },
        "serverfarms_environment_sp_name":{
            "defaultValue":"dev-sp",
            "type":"String"
        },
        "sites_environment_api_name":{
            "defaultValue":"dev-api",
            "type":"String"
        },
        "sites_environment_ui_name":{
            "defaultValue":"dev-ui",
            "type":"String"
        }
    },
    "variables":{

    },
    "resources":[
        {
            "type":"Microsoft.Resources/resourceGroups",
            "apiVersion":"2018-05-01",
            "location":"West US",
            "name":"[parameters('rgName')]",
            "tags":"[parameters('customTags')]",
            "properties":{

            }
        },
        {
            "apiVersion":"2019-08-01",
            "name":"[parameters('rgName')]",
            "type":"Microsoft.Resources/deployments",
            "resourceGroup":"[parameters('rgName')]",
            "tags":"[parameters('customTags')]",
            "dependsOn": [
                "[resourceId('Microsoft.Resources/resourceGroups/', parameters('rgName'))]"
            ],
            "properties":{
                "mode":"Incremental",
                "template":{
                    "$schema":"https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
                    "contentVersion":"1.0.0.1",
                    "resources":[
                        {
                            "apiVersion":"2018-02-01",
                            "type":"Microsoft.Web/serverfarms",
                            "kind":"app",
                            "name":"[parameters('serverfarms_environment_sp_name')]",
                            "location":"[resourceGroup().location]",
                            "tags":"[parameters('customTags')]",
                            "properties":{

                            },
                            "dependsOn":[

                            ],
                            "sku":{
                                "name":"[parameters('serverfarms_environment_sp_sku')]"
                            }
                        },
                        {
                            "apiVersion":"2018-11-01",
                            "type":"Microsoft.Web/sites",
                            "kind":"app",
                            "name":"[parameters('sites_environment_api_name')]",
                            "location":"[resourceGroup().location]",
                            "tags":"[parameters('customTags')]",
                            "properties":{
                                "serverFarmId":"[resourceId('Microsoft.Web/serverfarms', parameters('serverfarms_environment_sp_name'))]"
                            },
                            "dependsOn":[
                                "[resourceId('Microsoft.Web/serverfarms', parameters('serverfarms_environment_sp_name'))]"
                            ]
                        },
                        {
                            "apiVersion":"2018-11-01",
                            "type":"Microsoft.Web/sites",
                            "kind":"app",
                            "name":"[parameters('sites_environment_ui_name')]",
                            "location":"[resourceGroup().location]",
                            "tags":"[parameters('customTags')]",
                            "properties":{
                                "serverFarmId":"[resourceId('Microsoft.Web/serverfarms', parameters('serverfarms_environment_sp_name'))]"
                            },
                            "dependsOn":[
                                "[resourceId('Microsoft.Web/serverfarms', parameters('serverfarms_environment_sp_name'))]"
                            ]
                        }
                    ]
                },
            }
        }
    ],
    "outputs":{
        "sites_environment_api_name":{
            "type":"string",
            "value":"[parameters('sites_environment_api_name')]"
        },
        "sites_environment_ui_name":{
            "type":"string",
            "value":"[parameters('sites_environment_ui_name')]"
        }
    }
}

You can save this file to your local machine and deploy as follows.

PowerShell:

New-AzDeployment -TemplateFile '<path-to-template>' -Location 'West US'

Azure CLI:

az deployment create --template-file "<path-to-template>" --location "US West"

You will see a warning about upcoming breaking changes. Microsoft are introducing a mandatory parameter ScopeType to the PowerShell, and scope-type to the CLI with four possible values - ResourceGroup, Subscription, ManagementGroup and Tenant. In this instance, you would set ScopeType to Subscription, but you can see where Microsoft are going with the others.

I expect the portal template deployment UI will be updated soon.

Alternatively the ARM template can remain as is and have an additional step after to run just some simple powershell like:

Set-AzResourceGroup -Name "YOURRESOURCEGROUPNAME" -Tag @{Department="IT"}

This would allow you to maintain the current CI/CD structure.

More information on using Powershell to update the Resource Group

{
  "type": "Microsoft.Resources/tags",
  "name": "default",
  "apiVersion": "2021-04-01",
  "properties": {
    "tags": "[variables('resourceTags')]"
  }
}

You can apply tags to the target resource group by using the "tags" resource. In this case I've used a variable to store my default tags, but you could explicitly define them as well.

Checkout the following for more details: https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/tag-resources?tabs=json#apply-tags-to-resource-groups-or-subscriptions

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