Coder Perfect

In Bicep, how can I access/cast the module output to a specific object?

Problem

I have a bicep below that is returning keyvault. In parent bicep, I like to access the properties/functions in keyvault. However, I’m not sure how to accomplish this when utilizing it as a module.

    resource kv 'Microsoft.KeyVault/vaults@2019-09-01' existing = {
         name: kvName
         scope: resourceGroup(subscriptionId, kvResourceGroup )
       }
       output kv1 object=kv
   module kv './keyvault.bicep' = {
     name: 'get Secrets'
     params: {
       subscriptionId: subscriptionId
       kvResourceGroup: resourceGroupName
       kvName: keyVaultName
     }
   }
   var pwd= kv.outputs.kv1.getSecret('key')

Please advise on how to proceed.

Asked by sub

Solution #1

That, in a nutshell, is not supported.

There is, however, a suggestion to make resource referencing easier:

Assume you have keyvault installed. A key vault is created by the bicep module.

resource kv 'Microsoft.KeyVault/vaults@2019-09-01' = {
  name: kvName
  ...
}

output name string = kv.name

You might find a reference to key vault in parent.bicep like this:

module kvModule './keyvault.bicep' = {
  name: 'key-vault-${keyVaultName}'
  params: {
    kvName: keyVaultName
    ...
  }
}

resource kv 'Microsoft.KeyVault/vaults@2019-09-01' existing = {
  name: kvModule.outputs.name
}

There are a few things to consider in your case:

Answered by Thomas

Post is based on https://stackoverflow.com/questions/69584597/how-to-access-cast-the-module-output-to-spefic-object-in-bicep