Creating SQL database workload in Fabric with Terraform: A Step-by-Step Guide (Preview)
Infrastructure as Code (IaC) tools like Terraform have revolutionized the way developers and organizations deploy and manage infrastructure. With its declarative language and ability to automate provisioning, Terraform reduces human error, ensures consistency, and speeds up deployment across cloud and on-premises environments. In this document, we’ll explore how you can create SQL databases workloads in Microsoft Fabric using Terraform.
Why Use Terraform for Fabric Workloads?
Terraform, developed by HashiCorp, is a powerful open-source IaC tool that simplifies infrastructure management. It’s particularly useful for creating resources in Microsoft Fabric workloads thanks to its ability to ensure repeatable, scalable, and consistent deployments. Whether you’re scaling applications or updating configurations, Terraform offers a streamlined solution to manage the entire lifecycle of your projects.
Prerequisites
Before diving into creating a SQL database in Fabric, ensure you have the following:
- An existing Fabric capacity, if you don’t have one, start a Fabric trial.
- Ensure the ‘User can create Fabric items’ switch and SQL database (preview) option are enabled.
- Permission to create SQL databases as well as Fabric items.
- Download Terraform – Install | Terraform | HashiCorp Developer.
Steps
1. Prepare Your Environment
After downloading Terraform, extract the files to a directory of your choice. Open a command prompt and navigate to this folder. Then, create a new working directory using the following text: mkdir terraform_demo && cd terraform_demo
2. Log In to Fabric
Authenticate Fabric tenant by running the following command: az login –scope https://fabric.microsoft.com/.default
Select your user account and subscription number from the list displayed.
3. Create Required Files
In your terraform_demo folder, create the following files using a notepad, VS Code or any editor of your choice. Each file will contain specific configurations for your SQL database and Fabric workspace.
- variables.tf: Define variables for workspace name, database name, and capacity.
- terraform.tfvars: Assign values to the variables defined in variables.tf.
- workspace.tf: Define the Fabric workspace resource.
- providers.tf: Specify the required Terraform provider configuration.
- sqldatabase.tf: Define the SQL database resource to be created.
- outputs.tf: Output workspace and SQL database identifiers for verification.
Example Content for Files:
File – variables.tf
variable “fabric_workspace_name” {
description = “Name for the Workspace to be created or already exists.”
type = string
}
variable “fabric_sql_database_name” {
description = “Name for the SQL database to be created.”
type = string
}
variable “fabric_capacity_name” {
description = “Fabric capacity where workspace/SQL database need to be created.”
type = string
}
File – terraform.tfvars
fabric_workspace_name = “<Enter workspace Name>”
fabric_sql_database_name = “<Enter SQL database Name”
fabric_capacity_name = “<Enter capacity Name>”
File – workspace.tf:
data “fabric_capacity” “capacity” {
display_name = var.fabric_capacity_name
}
resource “fabric_workspace” “example_workspace” {
display_name = var.fabric_workspace_name
description = “Getting started workspace”
capacity_id = data.fabric_capacity.capacity.id
}
File – providers.tf
terraform {
required_version = “>= 1.8, < 2.0”
required_providers {
fabric = {
source = “microsoft/fabric”
version = “1.2.0”
}
}
}
provider “fabric” {
preview = true
}
File – sqldatabase.tf
resource “fabric_sql_database” “example_sql” {
workspace_id = fabric_workspace.example_workspace.id
display_name = var.fabric_sql_database_name
}
File – outputs.tf
output “capacity_id” {
value = data.fabric_capacity.capacity.id
}
output “sql_database_id” {
value = fabric_sql_database.example_sql.id
}
output “sql_connection_string”{
value = fabric_sql_database.example_sql.properties.connection_string
}
4. Initialize Terraform
In the command prompt, navigate to the folder where you have created the files, as completed in the previous steps.
..\terraform init
A message will generate confirming that Terraform has been successfully initialized.
5. Create an execution plan
..\Terraform plan -out main.tfplan
This command will create an execution plan but will not execute it. The optional -out parameter would allow you to create an output file for the plan.
5. Apply Terraform Configuration
..\terraform apply main.tfplan
This command will review the file created in previous command and apply the configurations.
6. Test and Verify
Log into Microsoft Fabric to verify that the workspace and SQL database have been created.
7. Clean up resources
..\terraform plan -destroy -out main.destroy.tfplan
..\terraform apply main.destroy.tfplan
Conclusion
Using Terraform to create and manage SQL databases in Fabric workloads simplifies the process, ensuring consistent and error-free deployments. By leveraging the power of Infrastructure as Code, you can save time, reduce manual effort, and scale your configurations with ease.
Start your journey with Terraform today and experience the efficiency and reliability it brings to infrastructure management as well as Dev ops.
Terraform Resources for SQL database – fabric_sql_database | Resources | microsoft/fabric | Terraform | Terraform Registry