IaC With
Azure Bicep

Rainer Stropek | @rstropek

Introduction

Rainer Stropek

  • Passionate software developers for 25+ years
    Β 
  • Microsoft MVP, Regional Director
    Β 
  • Trainer, Teacher, Mentor
    Β 
  • πŸ’• community

Advantages of IaC

  • Repeatable
    • For e.g. staging (dev/test/prod)
  • Shareable
    • Share blueprints, good practices
  • Trackable
    • Track changes in Git
    • Azure tracks executions and results
  • Easy to automate
    • CI/CD integration
    • Less "click-ops"

Advantages of Templates Over Scripts

  • Declarative Language
    • You define whatΒ you want, not how to get there
    • Somewhat similar to SQL
    • Validated before execution
    • If you miss functionality: Extensible with PS or Bash scripts πŸ”—
  • Idempotency
    • You can deploy the template many times
    • Template represents the desired state
    • Only changes are applied
    • If you are unsure: What-if function πŸ”—
  • Automatic ordering and parallelization
    • Don't worry of order, ARM will figure it out based on dependencies

Templates

  • Azure native functionality: ARM Templates πŸ”—
    • JSON format πŸ˜…
    • Huge gallery of quickstart templates πŸ”—
  • New language to make ARM Templates easier to write: Bicep
    • NotΒ a replacement of ARM Templates, ARM becomes "IL"
    • ​Domain-specific language optimized for the task πŸ‘, not just JSON
    • Modular πŸ”—
    • Much better tooling support (e.g. VSCode)
    • Some limitations compared to ARM Templates (e.g. no support for user-defined functions πŸ”—)
    • Possibility to decompile ARM Templates into Bicep πŸ”—

Alternatives

  • Open-source IaC platforms like Terraform πŸ”—
    • Use ARM behind the scenes
    • Well-suited in multi-cloud organizations
  • Code-based IaC
  • Graphical solutions
  • There is no silver bullet!
    • No "best practice" applicable to all orgs and projects!
    • Many orgs will be polyglot
    • Important that you do IaC at all!

Advantages of Azure Templates

  • Valid for ARM Templates and Bicep
    • Because Bicep is a transparent abstraction on top of the ARM platform
  • "Day 0 support" for all resource providers
    • You can deploy anyΒ Azure resource with ARM/Bicep, even preview
  • Important: ARM Provider ReferenceΒ πŸ”—
    • Versioned resource schema, based on Azure Rest API Specs πŸ”—
    • Example: Storage Account πŸ”—
    • JSON/Bicep view
  • No state DB or state files required
    • All state is stored directly in Azure

Bicep
101

Bicep Tooling

  • Bicep CLI πŸ”—
    • Support built-in for Azure CLI and PowerShell Az module
  • Bicep Linter πŸ”—
  • Bicep VSCode extension πŸ”—
  • Bicep Playground πŸ”—
    • Decompile ARM Templates into Bicep
    • Access Azure Quickstart templates and turn them into Bicep

Structure

  • Similar to ARM Templates πŸ”—
    • DSL instead of JSON
    • Different expression language
  • Structure of a Bicep template
    • Parameters to make templates generally usable
    • Variables to define reusable values
    • Resource definitionsΒ for your Azure resources
    • OutputsΒ to return values from template execution

Structure

// this file can only be deployed at a subscription scope
targetScope = 'subscription'

@description('Name of the Resource Group to create')
param rgName string = 'DemoResourceGroup'

@description('Location for the Resource Group')
param rgLocation string = 'westeurope'

var dept = 'IT'

resource demoRG 'Microsoft.Resources/resourceGroups@2021-01-01' = {
  name: rgName
  location: rgLocation
  tags: {
    Dept: dept
    Environment: 'Test'
  }
}

output resourceID string = demoRG.id

Language Features

var list = 'a,b,c,d'
var arrayFromString = split(list, ',')

var find = 'findThisInString'
var found = contains(find, 'This')
var index = indexOf(find, 'This')
var indexNotFound = indexOf(find, 'NotFound')
var len = length(find)
var substr = substring(find, index, (len - index))

output arrayFromString array = [for i in arrayFromString: {
  element: i
}]

output found string = found == true ? 'Found "this"' : 'Did not find "This"'


output index int = index
output indexNotFound int = indexNotFound

output substr string = substr

Functions πŸ”—

Loops

Inline if

Language Features

Demo
Time!

Thank you!

Rainer Stropek | @rstropek

Azure Bicep

By Rainer Stropek

Azure Bicep

  • 758