πŸ”™

Orchestrate Azure Bicep Deploys with lolite

Introduction to lolite

lolite is an orchestration tool for deploy Azure Bicep templates into Azure. lolite makes it easy to re-use bicep templates across Subscriptions and Resource Groups. lolite also makes it easy to use deployment outputs as inputs for other deployments.

In this post we will look at installing lolite, setting up a sample project and creating and deploying an Automation Account in Azure.

There is also a lolite-sample-project for reference.

Installing lolite

It is super easy to get lolite installed and setup. To install simply use pip:

pip3 install lolite

To verify the install you can run:

lolite -h

Which should output the following:

usage: lolite [-h]

lolite Usage:
        - deploy: deploy a single configuration
        - deploy-resource-group: deploy all config in a specific resource group
        - deploy-subscription: deploy all config in a specific subscription
        - deploy-account: deploy all config in the account / lolite project
        see GitHub for more details: https://github.com/NathanKewley/lolite 

optional arguments:
  -h, --help  show this help message and exit

You will also need to have the Azure CLI and Bicep installed.

Setting up a lolite Project

Once you have lolite installed create a new directory for your project. In this case called lolite-sample. At the root of this directory we need to create 2 folders: β€˜bicep’ and β€˜configuration’. This is the base structure of a lolite project:

lolite-sample
β”œβ”€β”€ bicep
└── configuration

The bicep folder will be the home to all of your bicep templates.

The configuration folder maps closely to the structure of your Azure account. The first level under configuration will be folders matching the names of your Azure Subscriptions and the level under will be the names of Resource Groups. To best explain this we will jump into building out a sample lolite project.

Sample lolite Project

Configuration

Starting with the configuration, in my case I will be targeting the lolite-test Azure Subscriptions:

This means that I will need a directory under the configuration folder that matches the name of the subscription exactly. This looks something like:

lolite-sample
β”œβ”€β”€ bicep
└── configuration
    └── lolite-test

The next step is setting up subfolders for the subscription to match the Resource Groups we want to deploy into. lolite will create the Resource Group for you if it does not already exist in Azure. In this instance I want to create an automation account in the lolite-test. the new folder structure is now:

lolite-sample
β”œβ”€β”€ bicep
└── configuration
    └── lolite-test
     Β Β  └── rg-aueast-automation

Each resource group need to be accompanied by a location.yaml file, this tells lolite where the Resource Group is located or need to be created. In this instance the deployment will be to australiaeast so the location.yaml file look like:

---
location: australiaeast

Now the project structure looks something like this:

lolite-sample
    β”œβ”€β”€ bicep
    └── configuration
        └── lolite-test
         Β Β  └── rg-aueast-automation
         Β Β      └── location.yaml

This is the last of the boring part, now we can create a configuration that can be deployed. Lets start with the automation account. Create sample-automation-account.yaml under the automation storage account, in this case rg-aueast-automation. This file will look something like the following:

---
bicep_path: automation_account.bicep

params:
  location: australiaeast
  appName: loliteAutomation
  skuName: Free

There are 2 main parts to this file.

Firstly the bicep_path points to a .bicep file relative to the bicep folder created in the root of the directory. This file contains the template that will be deployed.

The params block allows you to set the parameters to pass to the bicep template. This tells lolite to deploy this template with these parameters to the subscription and resource group specified by the configuration files location relative to the configuration folder. Configuration files can point to the same .bicep files as each other, this makes re-using bicep templates as easy as creating additional configurations for the deployments.

Bicep

This of course means we need to create the template bicep/automation_account.bicep:

param location string
param appName string
param skuName string

resource AutomationAccount 'Microsoft.Automation/automationAccounts@2015-10-31' = {
  name: appName
  properties: {
    sku: {
      name: skuName
    }
  }
  location: location
  tags: {}
}

Once this template is created the project structure looks something like the following:

lolite-sample
β”œβ”€β”€ bicep
β”‚Β Β  └── automation_account.bicep
└── configuration
    └── lolite-test
     Β Β  └── rg-aueast-automation
     Β Β      β”œβ”€β”€ location.yaml
     Β Β      └── sample-automation-account.yaml

Deploying

At this point we have something we are able to deploy. To deploy out our configuration we can run the deploy command in lolite passing it our .yaml configuration file as input:

lolite deploy lolite-test/rg-aueast-automation/sample-automation-account.yaml

This will then start the deploy and should output something like:

2021-04-20 21:42:22,778 - logging - DEBUG - Namespace(operation=['deploy'], suboperation='lolite-test/rg-aueast-automation/sample-automation-account.yaml')
2021-04-20 21:42:22,779 - logging - INFO - Deploying: lolite-test/rg-aueast-automation/sample-automation-account.yaml to lolite-test
2021-04-20 21:42:22,948 - logging - DEBUG - Setting Subscription: lolite-test
2021-04-20 21:42:38,959 - logging - INFO - Creating resource group: 'rg-aueast-automation' in australiaeast
2021-04-20 21:42:39,995 - logging - DEBUG - Deployment Name: lolite-test.rg-aueast-automation.sample-automation-account
2021-04-20 21:42:39,995 - logging - DEBUG - command: az deployment group create -f bicep/automation_account.bicep -g rg-aueast-automation \\ 
                                            --mode Incremental --name lolite-test.rg-aueast-automation.sample-automation-account \\
                                            --parameters location=australiaeast appName=loliteAutomation skuName=Free --output json
2021-04-20 21:43:18,816 - logging - DEBUG - Deploy Complete

Thats it! Check out the lolite GitHub page for some extra features and documentation!

πŸ”™