lellamary vineela
lellamary vineela

Reputation: 11

Unable to Deploy ARM template from Azure cloud shell

I'm trying to deploy ARM template to create a resource group and storage account from azure cloud shell using below commands,

New-AzSubscriptionDeployment -Location "westus3" -Name "MyResourceGroupDeployment" -TemplateFile "/home/mary/clouddrive/main-template.json" -TemplateParameterFile "/home/mary/clouddrive/variables.json"

but getting below error,

New-AzDeployment: 9:29:13 PM - Error: Code=InvalidScope; Message=The request contains resources that must be deployed at a resource group scope but a different scope was found. Make sure to specify a resource group scope similar to: '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}'New-AzDeployment: 9:29:13 PM - Error: Code=InvalidScope; Message=Resource: '/subscriptions/19713d52-d81f-4a02-9bd2-01f2a7d436e8/providers/Microsoft.Storage/storageAccounts/stppuxdatatestwus' contains an invalid scope, expected a resource group scope but found a subscription scope: '/subscriptions/19713d52-d81f-4a02-9bd2-01f2a7d436e8'.New-AzDeployment: The deployment validation failed

Can you please confirm whether creating a reasource group along with other resources in the same template is correct or not?

Upvotes: 0

Views: 134

Answers (1)

Vinay B
Vinay B

Reputation: 2401

Deploying a resource group and storage account in a single ARM template.

To create a resource group and storage account using single ARM template is achievable by the way you define the storage account scope in a nested deployment block so that storage account should be deployed under resource group but not under subscription.

Updated ARM template:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-08-01/subscriptionDeploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [
    {
      "type": "Microsoft.Resources/resourceGroups",
      "apiVersion": "2021-04-01",
      "location": "[parameters('location')]",
      "name": "[parameters('resourceGroupName')]"
    },
    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2021-04-01",
      "name": "nestedStorageDeployment",
      "resourceGroup": "[parameters('resourceGroupName')]",
      "dependsOn": [
        "[resourceId('Microsoft.Resources/resourceGroups', parameters('resourceGroupName'))]"
      ],
      "properties": {
        "mode": "Incremental",
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "resources": [
            {
              "type": "Microsoft.Storage/storageAccounts",
              "apiVersion": "2021-09-01",
              "name": "[parameters('storageAccountName')]",
              "location": "[parameters('location')]",
              "sku": {
                "name": "Standard_LRS"
              },
              "kind": "StorageV2",
              "properties": {}
            }
          ]
        }
      }
    }
  ],
  "parameters": {
    "resourceGroupName": {
      "type": "string"
    },
    "location": {
      "type": "string"
    },
    "storageAccountName": {
      "type": "string"
    }
  }
}

variable.json:

{
  "resourceGroupName": {
    "value": "vksbResourceGroup"
  },
  "location": {
    "value": "westus3"
  },
  "storageAccountName": {
    "value": "testcccsstorageaccount"
  }
}

command:

New-AzSubscriptionDeployment -Location "westus3" -Name "vkkResourceGroupDeployment" -TemplateFile "main-template.json" -TemplateParameterFile "variable.json"

Deployment:

enter image description here

enter image description here

refer:

https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/deploy-to-resource-group?tabs=azure-cli#scope-to-subscription

https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/linked-templates?tabs=azure-powershell

Upvotes: 0

Related Questions