Azure / PSRule.Rules.Azure

Rules to validate Azure resources and infrastructure as code (IaC) using PSRule.
https://azure.github.io/PSRule.Rules.Azure/
MIT License
394 stars 85 forks source link

[BUG] `Unable to expand resources because the source file ... The function "references" was not found.` when using languageVersion 2.0 #2922

Closed GABRIELNGBTUC closed 4 months ago

GABRIELNGBTUC commented 5 months ago

Existing rule

No response

Description of the issue

When referencing the outputs of a collection of deployed modules, it seems that PSRule is unable to recognize the references function used to access the properties of the modules.

Error messages

[error]Failed to expand bicep source '/home/vsts/work/1/s/main.bicep'. Exception calling "GetBicepResources" with "2" argument(s): "Unable to expand resources because the source file '/home/vsts/work/1/s/main.bicep' was not valid. An error occurred evaluating expression '[map(references('acc_loop'), lambda('acc', createObject(format('{0}', lambdaVariables('acc').outputs.acc.value), createObject('name', lambdaVariables('acc').outputs.acc.value))))]' line 257. The function "references" was not found."

Reproduction

module1.bicep

param acc int = 0

metadata name = 'Create a Windows Virtual Machine'

//
// Parameters
//
@minLength(1)
@maxLength(64)
@description('Name of the Virtual Machine')
param vm_name string = replace(resourceGroup().name, '-rg-', '-vm-')

@description('Location of the Virtual Machine')
param location string = resourceGroup().location

@description('Tags for the Virtual Machine')
param tags object = resourceGroup().tags

@description('Id of the Network Interface to attach to the Virtual Machine')
param network_interface_id string

@minLength(1)
@maxLength(16)
@description('Username for Virtual Machine admin')
param admin_username string

@description('Admin password')
@secure()
param admin_password string

@minLength(1)
@maxLength(80)
@description('Name of the Operating System Disk for the Virtual Machine')
param os_disk_name string = replace(resourceGroup().name, '-rg-', '-osdisk-')

@description('ID of the Data Disk for the Virtual Machine')
param data_disk_id string

@minLength(1)
@maxLength(80)
@description('Name of the Data Disk for the Virtual Machine')
param data_disk_name string

@description('Virtual Machine Size')
param vm_size string = 'Standard_D2s_v3'

@description('Size in GB of Operating System Disk')
param disk_size_gb int = 127

@description('Storage Account Type of Managed Disk')
@allowed([
  'Standard_LRS'
  'Premium_LRS'
  'Premium_ZRS'
  'StandardSSD_LRS'
  'StandardSSD_ZRS'
  'UltraSSD_LRS'
])
param storage_account_type string = 'StandardSSD_LRS'

@allowed([
  'None'
  'SystemAssigned'
])
param identity string = 'None'

//
// Resources
//
resource virtual_machine 'Microsoft.Compute/virtualMachines@2021-07-01' = {
  name: vm_name
  location: location
  tags: tags
  identity: {
    type: identity
  }
  properties: {
    hardwareProfile: {
      vmSize: vm_size
    }

    networkProfile: {
      networkInterfaces: [
        {
          id: network_interface_id
           properties: {
             deleteOption: 'Detach'
           }
        }
      ]
    }

    osProfile: {
      adminUsername: admin_username
      adminPassword: admin_password
      computerName: take(vm_name, 15)
      windowsConfiguration: {
        enableAutomaticUpdates: true
        provisionVMAgent: true
        timeZone: 'Romance Standard Time'
      }
    }

    storageProfile: {
      osDisk: {
        createOption: 'FromImage'
        deleteOption: 'Detach'
        diskSizeGB: disk_size_gb
        managedDisk: {
          storageAccountType: storage_account_type
        }
        name: os_disk_name
        osType: 'Windows'
      }
      dataDisks: [
        {
          lun: 0
          createOption: 'Attach'
          deleteOption: 'Detach'
          caching: 'ReadOnly'
          writeAcceleratorEnabled: false
          managedDisk: {
            id: data_disk_id
          }
          diskSizeGB: 40
          name: data_disk_name
        }
      ]

      imageReference: {
        publisher: 'MicrosoftWindowsServer'
        offer: 'WindowsServer'
        sku: '2022-datacenter-azure-edition'
        version: 'latest'
      }
    }
    licenseType: 'Windows_Server'
  }
}

@description('Name of the created VM')
output output_vm_name string = virtual_machine.name

output acc string = guid(string(acc))

main.bicep

module acc_loop 'module1.bicep' = [for (item, index) in range(0, 5): {
  name: 'deploy-${index}'
  params: {
    acc: index
    admin_username: 'sqdsqdqqs'
    admin_password: uniqueString('sqdsqdsqsq')
    data_disk_id: 'sqdsqsq'
    data_disk_name: 'sqdsqdssq'
    network_interface_id: 'qsdsdq'
  }

}]

var output_array = map(acc_loop, acc => {
  '${acc.outputs.acc}': {
    name: acc.outputs.acc
  }
})

output output array = output_array

The compoilation of main.bicep fails with:

##[error]Failed to expand bicep source '/home/vsts/work/1/s/main.bicep'. Exception calling "GetBicepResources" with "2" argument(s): "Unable to expand resources because the source file '/home/vsts/work/1/s/main.bicep' was not valid. An error occurred evaluating expression '[map(references('acc_loop'), lambda('acc', createObject(format('{0}', lambdaVariables('acc').outputs.acc.value), createObject('name', lambdaVariables('acc').outputs.acc.value))))]' line 257. The function "references" was not found."

Version of PSRule

2.9.0

Version of PSRule for Azure

1.37.0

Additional context

No response

BernieWhite commented 5 months ago

Thanks for reporting the issue @GABRIELNGBTUC