SMS Blog
Azure Workbooks Using Terraform
Creating an Azure Workbook with Terraform: A Step-by-Step Guide
In today’s cloud-first world, effective monitoring and visualization of metrics are crucial for maintaining optimal system performance and security. Azure Workbooks offer a powerful way to create interactive dashboards for monitoring and analyzing Azure resources. When managing multiple subscriptions, deploying workbooks consistently across all of them can be time-consuming and error prone. Terraform, an infrastructure as code tool, provides a streamlined solution for this task.
This blog post walks you through the process of creating an Azure Workbook using Terraform, a popular Infrastructure as Code (IaC) tool that helps automate and manage your cloud infrastructure.
What Are Azure Workbooks?
Azure Workbooks provide a flexible platform for visualizing and analyzing data from various sources within Azure. They allow you to create rich, interactive reports and dashboards that can help in monitoring, troubleshooting, and making data-driven decisions. SMS previously created a few workbooks throughout a multi-subscription environment for one of our government customers. Some of these workbooks monitored Computer Metrics, Updates, DNS policies, as well as Agent Health. These workbooks made it possible to monitor resources at a per-subscription level or across all of the subscriptions in a single workbook.
Why Use Terraform for Azure Workbooks?
Terraform offers several advantages when managing Azure Workbooks:
- Consistency: Define and manage your workbooks as code to ensure consistency across environments.
- Automation: Automate the creation and updating of your workbooks, reducing manual intervention.
- Version Control: Track changes to your workbooks alongside your infrastructure code.
Prerequisites
Before you start, ensure you have:
- An active Azure subscription.
- Terraform installed on your local machine.
- Azure CLI installed on your local machine
- Basic understanding of Terraform and Azure Workbooks.
- Required permissions to create resources in Azure.
Step-by-Step Guide
1. Create/Find the workbook that is right for you
Begin by understanding what Azure Workbook is right for you. GitHub has a great repository of premade Workbooks that contain everything from giving app metrics to virtual machine performance and everything in between. (https://github.com/microsoft/Application-Insights-Workbooks/tree/master/Workbooks) These “sample” Workbooks will be in a JSON format which will make it very easy to apply to a new workbook. Once you have found a suitable workbook, login to your Azure account and create a new Workbook.
Click on Azure Workbook > + Create > + New
On the top of the page click on the “Advanced Editor” button that looks like </>. This button will allow you to paste the .json text from any repository to this specific Workbook.
Copy and paste the desired code from the repository and click “Apply”. Create a new JSON file, go back to the Advanced Editor page and copy everything inside of the “Gallery Template” and paste everything into the newly created JSON file.
Afterwards search inside of the .json file for the Subscription ID and replace it with “${SID}” so the like looks like:
“/subscriptions/${SID}”
Copy this file to the terraform folder for this specific deployment.
2. Create a Terraform Main Resource:
- A Terraform module is a reusable block of code that can be used across different projects. Create a new directory for your module.
- Create a folder on your machine for this Terraform Project
- Create a new file named main.tf and define the resource block for the Azure Workbook
- Create a parameter named “data_json” to point to the query.json file
Terraform resource "azurerm_workbook" "example" { name = "MyWorkbook" resource_group_name = azurerm_resource_group.example.name source = data.azurerm_workbook.example.source category = var.category data_json = jsonencode(templatefile("${path.module}/example/query.json", { SID = var.subscription_id })) }
- Create a data block to fetch the source of the workbook you want to deploy:
Terraform data "azurerm_workbook" "example" { name = "SourceWorkbookName" resource_group_name = azurerm_resource_group.example.name }
3. Create a Terraform Variables page
- In the same directory, create a new Terraform configuration file (e.g., variables.tf)
- Each variable is listed within this file
- Use the variable block to reference any variable that is necessary.
Terraform variable "subscription_id" { default = "" } variable "category" { default = "workbook" type = string description = "Name of workbook category" variable "location" { default = "usgovvirginia" type = string description = "Az Region" }
4. Create a Terraform Module:
- Within the folder on your machine for this Terraform Project, create another folder called “module”. Within the module folder, create a file called main.tf.
- Each module is used for a specific subscription.
- Use the module block to reference your workbook module and provide the necessary variables:
Terraform module "workbook_module" { source = "../" subscription_id = “1111111-aaaaaaaa-bbbbbbbb-cccccccc" default_tags = { “Admin-Email” = "contact@company.com" “Admin-Name” = “Contact” “Poc-Email” = "contact@company.com" “Poc” = “Contact” “env” = “prod” } }
- Define the resource group where you want to deploy the workbook:
Terraform resource "azurerm_resource_group" "example" { name = "my-resource-group" location = "westus2" }
5. Apply the Configuration:
- Run ‘terraform init’ at the main.tf where the Azure subscription modules are located to deploy the workbooks to the specified subscriptions
- terraform init: initializes a working directory and downloads the necessary provider plugins and modules and setting up the backend for storing you infrastructure’s state.
- Run ‘terraform plan’ at the main.tf where the Azure subscription modules are located to deploy the workbooks to the specified subscriptions
- terraform plan: creates an execution plan, which lets you preview the changes that Terraform plans to make to you infrastructure.
- Run ‘terraform apply’ at the main.tf where the Azure subscription modules are located to deploy the workbooks to the specified subscriptions
- terraform apply: executes the actions proposed in the Terraform plan
Bash terraform init terraform plan terraform apply
What about . . .
- Conditional Deployment: If you need to deploy the workbook only to specific subscriptions based on conditions, you can use conditional expressions within your Terraform configuration.
- Variables: Use variables to make your terraform code more flexible and reusable for multiple Azure Workbooks.
- State Management: Manage your Terraform state carefully to avoid conflicts and ensure consistency across deployments.
- Made a mistake: After checking the workbook that was created, running a
terraform destroy
command will delete the workbook if a mistake was made in the deployment
By following these steps and leveraging Terraform’s capabilities, you can efficiently deploy Azure Workbooks to multiple subscriptions, ensuring consistent configurations and reducing manual effort.