DEV Community 👩‍💻👨‍💻

Pavan
Pavan

Posted on

JSON Parameters in Bicep Template

Hello Techies,

I am sure you all must have heard Bicep new kid around the Microsoft Block to do Infrastructure As Code (IAC) (Domain Specific Language) to manage the deployments of Azure Resources. This is great learning module from Microsoft which covers basics to advanced

One thing I like about it is there are many ways to pass the parameters to the Bicep Template. [Check out his blog, he has got some amazing content on Azure DevOps] Long story short you can pass parameters through param files, same like the way we pass to Arm Templates, or they can be inline through az deployment group command or it can be combination.

Scenario: Lets say you want different environments to have different Storage Account Skus (Standard LRS for DEV and Standard RAGZRS for Production) Although bicep object param can do similar job but it is not as flexible as plain JSON. It is not possible to pass the Object Param through command inline (Atleast I didnt find a way). JSON string can be minified and can be managed through Azure DevOps variables.

We can acheieve this by using json Bicep Functions

Below example is to pass normal JSON Object(Not Arm Schema based) to the Bicep Template as inline Parameter.

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

#Using Json Function to parse the Json string to json object
var storageConfig = json(storageJsonString)

resource storage_account 'Microsoft.Storage/storageAccounts@2021-09-01'= {
  kind: storageConfig.kind
  name: storageConfig.name
  location: location
  sku: {
    name: storageConfig.sku
  }
  properties: {
    allowBlobPublicAccess: true
    accessTier: 'Hot'
  }
}

Enter fullscreen mode Exit fullscreen mode

Call this from PowerShell by passing inline JSON string parameter to this template.

$stg = '{\"name\":\"pa1pocstg\",\"sku\":\"Standard_LRS\",\"kind\":\"StorageV2\"}'
az deployment group create --resource-group $rg `
--template-file .\Bicep101\bicep-json.bicep `
--parameters storageJsonString=$stg

Enter fullscreen mode Exit fullscreen mode

Let me know what you think.

Top comments (0)

Want to help Forem improve its infrastructure and ops? 🤔

Forem is hiring a Senior Platform Engineer

Are you capable of chipping in across sysadmin, ops, and site reliability work, while supporting the open source stack that runs DEV and other communities?

This role might just be for you!

Apply now