Saturday 19 June 2021

Passing Output Parameters from Bicep Modules

 We have discussed about deploying resources to a resource group using Azure Bicep in the post "Azure Infrastructure as Code (IaC) with Bicep - Creating Resources in Resource Group". If we are referring a resource created via a module in another module, we might have to pass identification of such resources as output parameters from a module. Let's learn how to pass output parameters in Bicep modules and use them in other resources in this post.

As an example scenario we can take creation of app service plan and a web app. Web app needs to know the plan id to associate with the pricing plan. Let's see how we can implement such a Bicep script, step by step.

For the pricing plan you can use below Bicep module code. We are passing plan name, SKU name and capacity of the plan and the plan kind as parameters. Note at the end of the module we are defining output parameter named planId using the resource Id of the resource plan. here the plan is the symbolic name we have given to the resource. Let's save the module below in a file named pricingplan.bicep.

param planName string
param planSKUName string
param planSKUCapacity int
param planKind string

resource plan 'Microsoft.Web/serverfarms@2020-12-01' = {
  nameplanName
  locationresourceGroup().location
  sku: {
    nameplanSKUName
    capacityplanSKUCapacity
    
  }
  kind:planKind

  properties: {
    reservedtrue
  }
}

output planId string = plan.id

Let's consider below module to create a Linux web app. Here we are enabling manage identity in the web application and exposing that service principal Id as an output parameter. However, we will not use the service principal Id in this example. You can see planId is required as input parameter for the web app module. Web app module is saved in a file named webapplinux.bicep.

param webappName string
param planId string
param linuxFxVersion string


resource webapplinux 'Microsoft.Web/sites@2018-11-01' = {
  namewebappName
  locationresourceGroup().location
  
  identity:{
    type:'SystemAssigned'
  }
  properties: {
    serverFarmIdplanId
    siteConfig:{
      linuxFxVersion:linuxFxVersion
    }
  }
}

output servicePrincipalId string = webapplinux.identity.principalId

Make sure you save the above two bicep module files in the same folder. As the next step let's see how we can use above two modules in a main.bicep file which is created in the same folder. Let's supply set of default values to make things simple. How to pass parameter values to Bicep via PowerShell is explained in "Azure Infrastructure as Code (IaC) with Bicep - Getting Started with Development".

targetScope='subscription'
param rgName string = 'rg-chbicepdemo-001'
param rgLocation string = 'eastus'

param planName string = 'plan-bicepdemo-001'
param planSKUName string = 'B3'
param planSKUCapacity int = 1
param planKind string = 'linux'

param webappName string = 'app-bicepdemo-001'
param linuxFxVersion string = 'node|14-lts'

resource rg 'Microsoft.Resources/resourceGroups@2021-04-01' = {
  namergName
  locationrgLocation
  tags:{
    'tag''bicepdemo'   
  }
}

module webappplan 'pricingplan.bicep' = {
  name'plan'
  scope:rg
  params:{
    planKind:planKind
    planName:planName
    planSKUCapacity:planSKUCapacity
    planSKUName:planSKUName
  }
}

module webapp 'webapplinux.bicep' = {
  name'webapp'
  scope:rg
  params:{
    linuxFxVersion:linuxFxVersion
    planId:webappplan.outputs.planId
    webappName:webappName
  }
}

Notice the usage of output parameter from the plan module in the web app module. We use symbolic name (webappplan) we have given to the module and .outputs.outputparametername to read output parameter.

We can deploy web app and the plan with above bicep as shown below.


Web app is getting deployed as a node 14 web app as specified in the Bicep files associated with the pricing plan with the plan Id provided using the output parameter.








No comments:

Popular Posts