DEV Community

Pavan
Pavan

Posted on

4 2

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.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (1)

Collapse
 
roy77 profile image
roy

@chintupawan hi i keep getting formatting issues. What Devops pipeline task do you use? gr roy

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs