Get started with project bicep

project bicep ... an arm dsl

Chendrayan Venkatesan

3 minute read

References

Refer to the below links to know more about the project bicep

  1. GitHub

    1. Repository
    2. Docs
    3. Examples
    4. Tutorials
    5. Specifications
  2. YouTube

    1. Intro to Azure’s Project Bicep with Brendan Burns and team
    2. Project Bicep Demo at Ignite 2020 by Mark Russinovich
    3. Project Bicep and ARM Templates November 2020 Update

Introduction

As a first step, we need to install the bicep tools on our local computer. If you need to deploy the solution using Az DevOps, follow the steps given in the “Deploy Bicep Solution using Azure DevOps” section.

Install Bicep

$installPath = "$env:USERPROFILE\.bicep"
$installDir = New-Item -ItemType Directory -Path $installPath -Force
$installDir.Attributes += 'Hidden'
(New-Object Net.WebClient).DownloadFile("https://github.com/Azure/bicep/releases/latest/download/bicep-win-x64.exe", "$installPath\bicep.exe")
$currentPath = (Get-Item -path "HKCU:\Environment" ).GetValue('Path', '', 'DoNotExpandEnvironmentNames')
if (-not $currentPath.Contains("%USERPROFILE%\.bicep")) { setx PATH ($currentPath + ";%USERPROFILE%\.bicep") }
if (-not $env:path.Contains($installPath)) { $env:path += ";$installPath" }
bicep build main.bicep

Upon the successful installation, try the below-listed commands

Bicep version
Bicep help

Create project scaffolding

📦src
┣ 📂scripts
┣ 📂storage_account
┃ ┗ 📜storage_account.bicep
┣ 📜main.bicep

(storage_account.bicep) file

param storage_account_name string = 'stgbicepdev'

resource storage_account 'Microsoft.Storage/storageAccounts@2020-08-01-preview' = {
  name: 'stgbicepdev'
  location: 'eastus2'
  properties: {
    accessTier: 'Hot'
  }
  sku: {
    name: 'Standard_LRS'
    tier: 'Standard'
  }
  kind: 'BlobStorage'
}

(storage_account.bicep) file

targetScope = 'subscription'

param resource_group_name string = 'rg-bicep-dev'

resource rg 'Microsoft.Resources/resourceGroups@2020-06-01' = {
  name: resource_group_name
  location: 'eastus2'
}

module storage_account './storage_account/storage_account.bicep' = {
  name: 'storage_account'
  scope: resourceGroup(rg.name)
}

Build

Now it’s time for us to start with the first build

PS C:\repos\Project-Bicep\src> build bicep .\main.bicep

If the above command is successful, we should see a main.json file in the ‘SRC’ folder.

Test and Deploy

PS C:\repos\Project-Bicep\src> Test-AzDeployment -Location 'East US 2' -TemplateFile .\main.json -Verbose
PS C:\repos\Project-Bicep\src> New-AzDeployment -Location 'East US 2' -TemplateFile .\main.json -Verbose

How do we show the outputs?

# MODULE (storage_account.bicep)
output storage_account_id string = storage_account.id
# MAIN (main.bicep)
output storage_account_id string = storage_account.outputs.storage_account_id

output

Deploy project bicep using Azure DevOps

To deploy the bicep solutions using Az DevOps, a minor change is required in the project scaffolding. Below illustrated tree view helps you to get it done.

📦.azure-pipelines
┗ 📜storage_account.yml

Add a folder “.azure-pipelines,” and underneath it, add a file name “storage_account.yml.”

To install the bicep cli in the Az DevOps pipeline, we use the same PowerShell script file (Refer install bicep section in introduction). It’s nice to place it inside the script folder as shown below

📦scripts
┗ 📜BICEP.PS1

(storage_account.yml)

Replace the value of azureResourceManagerConnection and subscriptionId

trigger:
  branches:
    include:
      - 'main'

pool:
  vmImage: 'windows-latest'

stages:
  - stage: 'BUILD'
    jobs:
      - job: 
        steps:
          - task: PowerShell@2
            inputs:
              targetType: filePath
              filePath: '\scripts\BICEP.PS1'
          - task: CopyFiles@2
            inputs:
              SourceFolder: '.\SRC\'
              Contents: '*.json'
              TargetFolder: $(Build.ArtifactStagingDirectory)
          - task: PublishBuildArtifacts@1
            inputs:
              PathtoPublish: $(Build.ArtifactStagingDirectory)
              ArtifactName: 'BICEP'
  - stage: 'RELEASE'
    jobs:
      - job: 
        steps:
          - task: DownloadBuildArtifacts@0
            inputs:
              buildType: current
              downloadType: single
              artifactName: BICEP
              downloadPath: $(System.ArtifactsDirectory)
          - task: AzureResourceManagerTemplateDeployment@3
            inputs:
              azureResourceManagerConnection: '{SERVICE CONNECTION NAME}'
              deploymentScope: Subscription
              csmFile: '$(System.ArtifactsDirectory)\src\MAIN.json'
              location: 'eastus2'
              deploymentName: 'AZURE.WVD'
              subscriptionId: '{SUBSCRIPTION ID}'
comments powered by Disqus