skip to Main Content

I’m trying to create a TemplateSpec using Bicep and the New-AzTemplateSpec command in Az-PS. However, I’m encountering an error and need some help to resolve it:

{
  "error": {
    "code": "InvalidSchema",
    "message": "The template is invalid. Error: 'The template resource 'subscriptionDeployment' at line '168' and column '18' is invalid. The api-version '2018-11-01' used to deploy the template does not support 'Scope' property. Please use api-version '2019-05-01' or later to deploy the template. Please see https://aka.ms/arm-syntax-resources for usage details.'"
  }
}

Here is the corresponding snippet from the ARM Template gets compiled after execution:

{
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2022-09-01",
      "name": "subscriptionDeployment",
      "scope": "/",
      "location": "[deployment().location]",

Created from this Bicep snippet:

resource subscriptionRes 'Microsoft.Subscription/aliases@2021-10-01' = {
  name: subscriptionName
  scope: tenant()
  properties: {
    displayName: subscriptionName
    billingScope: '/billingAccounts/${billingAccountName}/billingProfiles/${billingProfileName}/invoiceSections/${invoiceSectionName}'
    additionalProperties: {
      managementGroupId: managementGroupId
    }
  }
}

This is what my PS command looks like:

New-AzTemplateSpec -Name name -Version "0.0.1" -ResourceGroupName rgname -Location westeurope -TemplateFile "<path_to_bicep_file>"

I found similar posts, mentioning that the Cmdlets need to be updated and this is the part where I am not exactly sure. My Az.Resources PowerShell module which I thought is responsible for creating the TemplateSpec is on the newest version:

Get-InstalledModule -Name Az.Resources

Version              Name                                Repository           Description
-------              ----                                ----------           -----------
6.9.0                Az.Resources                        PSGallery            Microsoft Azure PowerShell - Azure Resource Manager and Active Directory cmdlets in Windows PowerShell and …

Also, I am using the newest PowerShell 7 version:

$PSVersionTable. PSVersion

Major  Minor  Patch  PreReleaseLabel BuildLabel
-----  -----  -----  --------------- ----------
7      3      6

Any advice is much appreciated!

Edit:
The output I get from listing the api versions of my installed Microsoft.Resources module are as follows:

(Get-AzResourceProvider -ProviderNamespace Microsoft.Resources).ResourceTypes | Where-Object ResourceTypeName -eq 'templateSpecs'

ResourceTypeName : templateSpecs
Locations        : {East Asia, Southeast Asia, Australia East, Australia Central…}
ApiVersions      : {2022-02-01, 2021-05-01, 2021-03-01-preview, 2019-06-01-preview}

Edit 2:
This is the way I import the deployment into the main bicep file:

// Subscription
module sub 'artifacts/subscription.bicep' = {
  scope: tenant()
  name: 'subscriptionDeployment'
  params: {
    managementGroupId: managementGroupId
    subscriptionName: subscriptionName
    billingAccountName: billingAccountName
    billingProfileName: billingProfileName
    invoiceSectionName: invoiceSectionName
    budgetName: '${subShortName}-budget'
    amount: amount
    category: category
    timeGrain: timeGrain
    startDate: startDate
    endDate: endDate
  }
}

2

Answers


  1. Chosen as BEST ANSWER

    Okay I now was able to find out the problem. The scope I gave to the module was wrong:

    module sub 'artifacts/subscription.bicep' = { 
    scope: tenant()
    

    I need to set the scope of the module to 'managementGroup' and also set the targetScope to 'managementGroup' in the subscription bicep file I import (I found that out in this article). The scope property in the resource definition itself still needs to be of tenant().

    Thank you very much Jahnavi for helping me through the process.


  2. The error shows that the template’s Api-version '2018-11-01' does not support the 'Scope' property. To deploy the template, you must use Api-version '2019-05-01' or later.

    I see that you are running Api-version "apiVersion":"2022-09-01". Try to modify it as "apiVersion":"2019-05-01" and deploy it to check compatibility.

    You mentioned that you are using the most recent version of the Az.Resources PowerShell module, yet it is possible that this version does not support the requisite Api-version.

    Note:

    1. Update the Az.resources module if required using below command.

      Update-Module -Name Az.Resources -Force

    2. Try retrieving the providers with their Api-versions that are available and registered in your environment as shown.

    Get-AzResourceProvider -Listavailable
    (Get-AzResourceProvider -ProviderNamespace Microsoft.Resources).ResourceTypes | Where-Object ResourceTypeName -eq 'templateSpecs'
    

    By referring to the MSDoc template spec deployment template for storage, I tried the similar deployment in my environment by changing the -version parameter to -Version "2.0" and it worked as expected.

    New-AzTemplateSpec -Name xxxx -Version "2.0" -ResourceGroupName <resourcegroup> -Location westus2 -TemplateFile "stoa.bicep"     
    

    enter image description here

    If still the issue persists, clear the cache and redeploy the template spec deployment.

    Update:

    targetScope = 'tenant'
    var SubscriptionName = 'Free Trial'
    resource Subscription  'Microsoft.Subscription/aliases@2020-09-01' = {
    scope: tenant()
    name:guid(SubscriptionName,  tenant().tenantId)
    properties: {
    displayName: SubscriptionName
    billingScope: '/providers/Microsoft.Billing/billingAccounts/xxxxx'
    workload: 'Production'
       }
    }
    

    I tried the above bicep code for subscription alias with api-version 2020-09-01 and the deployment was successful.

    enter image description here

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search