azure-bicep

How to use a property from parent resource in child?


How can I use the property nextVersion from resource mlEnvironment from my template below:

@description('The list of Azure ML workspace names to deploy environments to.')
param workspaceName string

@description('The list of environments to create. Each object should have: environmentName, environmentVersion.')
param environments array

resource mlWorkspace 'Microsoft.MachineLearningServices/workspaces@2025-06-01' existing = {
  name: workspaceName
}

resource mlEnvironment 'Microsoft.MachineLearningServices/workspaces/environments@2025-07-01-preview' = [
  for env in environments: if(length(environments)>0) {
    parent: mlWorkspace
    name: env.environmentName
    properties: {
      isArchived: false
  }
}]

resource mlEnvironmentVersion 'Microsoft.MachineLearningServices/workspaces/environments/versions@2025-07-01-preview' = [
  for (env, i) in environments: if(length(environments)>0) {
    parent: mlEnvironment[i]
    name: mlEnvironment[i].properties.nextVersion
    properties: {
      description: env.environmentDescription
      condaFile: env.condaFile
    osType: env.osType
    isAnonymous: false
    isArchived: false
    image: (empty(env.environmentImage) ? null : env.environmentImage)
  }
}]

If I do name: mlEnvironment[i].properties.nextVersion from template above, I get this error:

I tried using user defined functions but also didn't work.


Solution

  • You have to use modules to achieve that. It is not possible without using modules.

    main.bicep

    @description('The list of Azure ML workspace names to deploy environments to.')
    param workspaceName string
    
    @description('The list of environments to create. Each object should have: environmentName, environmentVersion.')
    param environments array
    
    resource mlWorkspace 'Microsoft.MachineLearningServices/workspaces@2025-06-01' existing = {
      name: workspaceName
    }
    
    resource mlEnvironment 'Microsoft.MachineLearningServices/workspaces/environments@2025-07-01-preview' = [
      for env in environments: if(length(environments)>0) {
        parent: mlWorkspace
        name: env.environmentName
        properties: {
          isArchived: false
      }
    }]
    
    module versions 'versions.bicep' = [for (env, i) in environments: if(length(environments)>0) {
      name: 'version-${env.environmentName}-${i}'
      params: {
        env: env
        workspaceName: workspaceName
        nextVersion: mlEnvironment[i].properties.nextVersion
      }
    }]
    
    

    versions.bicep

    param env object
    param workspaceName string
    param nextVersion string
    
    resource mlWorkspace 'Microsoft.MachineLearningServices/workspaces@2025-06-01' existing = {
      name: workspaceName
    }
    
    resource mlEnvironment 'Microsoft.MachineLearningServices/workspaces/environments@2025-07-01-preview' = {
      parent: mlWorkspace
      name: env.environmentName
      properties: {
        isArchived: false
      }
    }
    
    resource mlEnvironmentVersion 'Microsoft.MachineLearningServices/workspaces/environments/versions@2025-07-01-preview' = {
      parent: mlEnvironment
      name: nextVersion
      properties: {
        description: env.environmentDescription
        condaFile: env.condaFile
        osType: env.osType
        isAnonymous: false
        isArchived: false
        image: (empty(env.environmentImage) ? null : env.environmentImage)
      }
    }