Search code examples
azureazure-resource-managerazure-powershellazure-rm-templateazure-bicep

Can't create Azure TemplateSpec due to invalid api-versions


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
  }
}

Solution

  • 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.