Lucky
Lucky

Reputation: 303

Optional params in Bicep

In Bicep I have a main bicep which calls

  1. module that deploys app service
  2. module that deploys managed identity

App service module looks like below, it uses output from user assigned managed id and is assigned in identity for app service:

main bicep

module userAssignedManagedIdModule 'uam.bicep' = {
  name: uamanagedid
  params: {
    location: rgLocation
    name: name
  }
}

module asModule 'appservicetemplate.bicep' = {
  name: 'name'
  params: {    
    appServiceName: asName
    userassignedmanagedid: userAssignedManagedIdModule.outputs.managedIdentityId 
  }
  dependsOn: [ userAssignedMID ]
}

App service template

param UserAssignedIdentity string

resource appService 'Microsoft.Web/sites@2021-02-01' = {
  name: appServiceName
  location: rgLocation
  identity: {
    type: 'UserAssigned'
    userAssignedIdentities: {
      '${UserAssignedIdentity}':{}
    }
  }
  properties:{
    serverFarmId: appServicePlanId
    siteConfig:{
      alwaysOn: true
      ftpsState: 'Disabled'
    }
    httpsOnly: true    
  }
}

Bicep for managed identity - user assigned

param name string
param location string = resourceGroup().location

resource UserAssignedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' = {
  name: name
  location: location
}

output managedIdentityId string = UserAssignedIdentity.id

If I need an app service to be deployed without managed id I want to use the same bicep as module so I do not want this userassignedmanagedid to be a mandatory parameter. How do I make it happen?

Upvotes: 0

Views: 3492

Answers (1)

Thomas
Thomas

Reputation: 29736

Optional parameters are defined using a default value.

Then you can build the identity block dynamically:

param UserAssignedIdentity string = ''

resource appService 'Microsoft.Web/sites@2021-02-01' = {
  name: appServiceName
  location: rgLocation
  identity: empty(UserAssignedIdentity) ? {} : {
    type: 'UserAssigned'
    userAssignedIdentities: {
      '${UserAssignedIdentity}': {}
    }
  }
  properties: {
    serverFarmId: appServicePlanId
    siteConfig: {
      alwaysOn: true
      ftpsState: 'Disabled'
    }
    httpsOnly: true
  }
}

Upvotes: 2

Related Questions