Search code examples
postgresqlazureif-statementconditional-statementsazure-bicep

Using variable in property settings for postgreSql in bicep template throws error


I'm trying to deploy a postgreSql server and database using a bicep template. Depending on the environment parameter there should be different settings. It functions well for the SKU if I write

  sku : (environment == 'production') ? {
    name: ...
  } : {
    name: ...
  }

The same logic doesn't work for the properties (just an excerpt)

properties: (environment == 'production') ? {
    storageProfile: {
      geoRedundantBackup: 'Enabled'
    }
  } : {
    storageProfile: {
      geoRedundantBackup: 'Disabled'
    }
  }

The deployment fails. In the Azure portal I get an error with Status 'Method not allowed' and details

    "error": {
        "code": "FeatureSwitchNotEnabled",
        "message": "Requested feature is not enabled"
    }

Any idea why that is happening? Is it not possible to switch the properties? The error message itself doesn't give precise information.

I tried to move the condition on different levels, e.g. directly to the geoRedundantBackup setting or to storage profile. I am getting the same error every time. I tried to use a variable instead of a conditional, which didn't help.


Solution

  • In order to set property according to the environment, As a workaround you can directly use this code:-

    backup: {
          backupRetentionDays: 7
          geoRedundantBackup: environment == 'production' ? 'Enabled' : 'Disabled'
        }
    

    I have referred the code from this MS Document to deploy Azure PostgreSQL with above settings:-

    main.bicep:-

     @description('Server Name for Azure Database for PostgreSQL')
    
    param  serverName  string
    
    @description('Database administrator login name')
    
    @minLength(1)
    
    param  administratorLogin  string
    
      
    
    @description('Database administrator password')
    
    @minLength(8)
    
    @secure()
    
    param  administratorLoginPassword  string
    
      
    
    param  environment  string = 'production'
    
      
    
    @description('Azure Database for PostgreSQL compute capacity in vCores
    (2,4,8,16,32)')
    
    param  skuCapacity  int = 2
    
      
    
    @description('Azure Database for PostgreSQL sku name ')
    
    param  skuName  string = 'GP_Gen5_2'
    
      
    
    @description('Azure Database for PostgreSQL Sku Size ')
    
    param  skuSizeMB  int = 51200
    
      
    
    @description('Azure Database for PostgreSQL pricing tier')
    
    @allowed([
    
    'Basic'
    
    'GeneralPurpose'
    
    'MemoryOptimized'
    
    ])
    
    param  skuTier  string = 'GeneralPurpose'
    
      
    
    @description('Azure Database for PostgreSQL sku family')
    
    param  skuFamily  string = 'Gen5'
    
      
    
    @description('PostgreSQL version')
    
    @allowed([
    
    '9.5'
    
    '9.6'
    
    '10'
    
    '10.0'
    
    '10.2'
    
    '11'
    
    ])
    
    param  postgresqlVersion  string = '11'
    
      
    
    @description('Location for all resources.')
    
    param  location  string = resourceGroup().location
    
      
    
    @description('PostgreSQL Server backup retention days')
    
    param  backupRetentionDays  int = 7
    
      
    
    // @description('Geo-Redundant Backup setting')
    
    // param geoRedundantBackup string = 'Disabled'
    
      
    
    @description('Virtual Network Name')
    
    param  virtualNetworkName  string = 'azure_postgresql_vnet'
    
      
    
    @description('Subnet Name')
    
    param  subnetName  string = 'azure_postgresql_subnet'
    
      
    
    @description('Virtual Network RuleName')
    
    param  virtualNetworkRuleName  string = 'AllowSubnet'
    
      
    
    @description('Virtual Network Address Prefix')
    
    param  vnetAddressPrefix  string = '10.0.0.0/16'
    
      
    
    @description('Subnet Address Prefix')
    
    param  subnetPrefix  string = '10.0.0.0/16'
    
      
    
    var  firewallrules = [
    
    {
    
    Name: 'rule1'
    
    StartIpAddress: '0.0.0.0'
    
    EndIpAddress: '255.255.255.255'
    
    }
    
    {
    
    Name: 'rule2'
    
    StartIpAddress: '0.0.0.0'
    
    EndIpAddress: '255.255.255.255'
    
    }
    
    ]
    
      
    
    resource  vnet  'Microsoft.Network/virtualNetworks@2021-05-01' = {
    
    name: virtualNetworkName
    
    location: location
    
    properties: {
    
    addressSpace: {
    
    addressPrefixes: [
    
    vnetAddressPrefix
    
    ]
    
    }
    
    }
    
    }
    
      
    
    resource  subnet 
    'Microsoft.Network/virtualNetworks/subnets@2021-05-01' = {
    
    parent: vnet
    
    name: subnetName
    
    properties: {
    
    addressPrefix: subnetPrefix
    
    }
    
    }
    
      
    
    resource  server  'Microsoft.DBforPostgreSQL/servers@2017-12-01' = {
    
    name: serverName
    
    location: location
    
    sku: {
    
    name: skuName
    
    tier: skuTier
    
    capacity: skuCapacity
    
    size: '${skuSizeMB}'
    
    family: skuFamily
    
    }
    
    properties: {
    
    createMode: 'Default'
    
    version: postgresqlVersion
    
    administratorLogin: administratorLogin
    
    administratorLoginPassword: administratorLoginPassword
    
    storageProfile: {
    
    storageMB: skuSizeMB
    
    backupRetentionDays: backupRetentionDays
    
    geoRedundantBackup: environment  ==  'production'  ?  'Enabled'  : 
    'Disabled'
    
    }
    
    }
    
      
    
    resource  virtualNetworkRule  'virtualNetworkRules@2017-12-01' = {
    
    name: virtualNetworkRuleName
    
    properties: {
    
    virtualNetworkSubnetId: subnet.id
    
    ignoreMissingVnetServiceEndpoint: true
    
    }
    
    }
    
    }
    
      
    
    @batchSize(1)
    
    resource  firewallRules 
    'Microsoft.DBforPostgreSQL/servers/firewallRules@2017-12-01' = [for 
    rule  in  firewallrules: {
    
    parent: server
    
    name: '${rule.Name}'
    
    properties: {
    
    startIpAddress: rule.StartIpAddress
    
    endIpAddress: rule.EndIpAddress
    
    }
    
    }]
    

    Output:-

    enter image description here

    enter image description here

    Portal:-

    enter image description here

    Coming to your error code you can find the Feature that is not registered for PostgreSQL resource by running the below commands from this Microsoft Document in your Azure CLI:-

    az feature list --namespace Microsoft.DBforPostgreSQL 
    

    Output:-

    enter image description here

    Find feature that is not registered according to the region in which you're trying to Deploy your PostgreSQL and then run command below from this MS Document to register the feature:-

    az feature register --name <name-of-the-feature> --namespace
    Microsoft.DBforPostgreSQL 
    

    Additionally, Refer this Github issue1 and Github issue2 for the error that you're facing.