πŸ”™

Orchestrate Azure Bicep Deploys with azurite

Introduction to azurite

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

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

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

Installing azurite

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

pip3 install azurite
                    

To verify the install you can run:

azurite -h
                    

Which should output the following:

usage: azurite [-h]

                    azurite 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 / azurite project
                            see GitHub for more details: https://github.com/NathanKewley/azurite 

                    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 azurite Project

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

azurite-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 azurite project.

Sample azurite Project

Configuration

Starting with the configuration, in my case I will be targeting the azurite-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:

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

The next step is setting up subfolders for the subscription to match the Resource Groups we want to deploy into. azurite 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 azurite-test. the new folder structure is now:

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

Each resource group need to be accompanied by a location.yaml file, this tells azurite 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:

azurite-sample
                        β”œβ”€β”€ bicep
                        └── configuration
                            └── azurite-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: azuriteAutomation
                      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 azurite 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:

azurite-sample
                    β”œβ”€β”€ bicep
                    β”‚Β Β  └── automation_account.bicep
                    └── configuration
                        └── azurite-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 azurite passing it our .yaml configuration file as input:

azurite deploy azurite-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='azurite-test/rg-aueast-automation/sample-automation-account.yaml')
                    2021-04-20 21:42:22,779 - logging - INFO - Deploying: azurite-test/rg-aueast-automation/sample-automation-account.yaml to azurite-test
                    2021-04-20 21:42:22,948 - logging - DEBUG - Setting Subscription: azurite-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: azurite-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 azurite-test.rg-aueast-automation.sample-automation-account \\
                                                                --parameters location=australiaeast appName=azuriteAutomation skuName=Free --output json
                    2021-04-20 21:43:18,816 - logging - DEBUG - Deploy Complete
                    

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

πŸ”™