Pass anything to a bicep template

Hasan Al-Saadi 0 Reputation points
2024-10-30T11:23:27.87+00:00

Hi.

I have a problem passing a number to a Bicep template

Bicep template:

// This module patches a setting into an existing Azure App Configuration.

param appConfigName string
param appConfigKey string
param appConfigValue any
param isKeyVaultRef bool

var contentTypeKeyVaultRef = 'application/vnd.microsoft.appconfig.keyvaultref+json;charset=utf-8'
var contentTypeString = ''

resource AppConfigurationResource 'Microsoft.AppConfiguration/configurationStores@2021-03-01-preview' existing = {
  name: appConfigName
}

resource ConfigStoreKeyValue 'Microsoft.AppConfiguration/configurationStores/keyValues@2021-10-01-preview' = {
  parent: AppConfigurationResource
  name: appConfigKey
  properties: {
    value: isKeyVaultRef ? '{"uri":"${appConfigValue}"}' : appConfigValue
    contentType: isKeyVaultRef ? contentTypeKeyVaultRef : contentTypeString
  }
}


The way I pass the values from a different bicep fil:

This how I am passing the numbers 
module appConfigPatchPlainValues '../../shared/modules/configStorePatch.bicep' = [for pv in plainValues: {
  name: 'deploy-${configStoreName}-${replace(pv.key, ':', '-')}'
  dependsOn: [
    configStoreModule
  ]
  params: {
    appConfigName: configStoreName
    appConfigKey: pv.key
    appConfigValue: pv.value
    isKeyVaultRef: false
  }
}]

According to microsoft "data-type-assignable" we are be able to use "any" datatype

https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/data-types#data-type-assignability

The error I am getting during the deployment

ERROR: /home/vsts/work/1/shared/modules/configStorePatch.bicep(5,22) : Error BCP302: The name "any" is not a valid type. Please specify one of the following types: "array", "bool", "int", "object", "string". [https://aka.ms/bicep/core-diagnostics#BCP302]
Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
{count} votes

1 answer

Sort by: Most helpful
  1. Amira Bedhiafi 41,121 Reputation points Volunteer Moderator
    2024-10-31T10:43:03.1366667+00:00

    In Bicep, while any is conceptually supported, it's not directly specified as a valid parameter type. You can work around this by using conditional logic based on the expected input types (like int, string, bool). I updated the template like below :

    
    param appConfigName string
    
    param appConfigKey string
    
    param appConfigValue string  // Change "any" to "string"
    
    param isKeyVaultRef bool
    
    var contentTypeKeyVaultRef = 'application/vnd.microsoft.appconfig.keyvaultref+json;charset=utf-8'
    
    var contentTypeString = ''
    
    resource AppConfigurationResource 'Microsoft.AppConfiguration/configurationStores@2021-03-01-preview' existing = {
    
      name: appConfigName
    
    }
    
    resource ConfigStoreKeyValue 'Microsoft.AppConfiguration/configurationStores/keyValues@2021-10-01-preview' = {
    
      parent: AppConfigurationResource
    
      name: appConfigKey
    
      properties: {
    
        value: isKeyVaultRef ? '{"uri":"${appConfigValue}"}' : appConfigValue
    
        contentType: isKeyVaultRef ? contentTypeKeyVaultRef : contentTypeString
    
      }
    
    }
    

    If you’re passing integers or booleans, you can convert them to strings at the calling template level. This way, all values are received as strings in the main template.

    
    module appConfigPatchPlainValues '../../shared/modules/configStorePatch.bicep' = [for pv in plainValues: {
    
      name: 'deploy-${configStoreName}-${replace(pv.key, ':', '-')}'
    
      dependsOn: [
    
        configStoreModule
    
      ]
    
      params: {
    
        appConfigName: configStoreName
    
        appConfigKey: pv.key
    
        appConfigValue: string(pv.value)  // Convert all to string
    
        isKeyVaultRef: false
    
      }
    
    }]
    
    
    
    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.