Automate Azure Infrastructure with Terraform: A Beginner’s Guide

🌐 Introduction

If you’re new to Terraform and want to learn how to automate your Azure infrastructure, you’re in the right place! In this blog, we’ll walk you through how to create:

  • A Resource Group
  • A Log Analytics Workspace
  • Both using Terraform variables for flexibility and reusability.

And the best part? You don’t need to be an expert — this guide is beginner-friendly and hands-on.


🔧 What is Terraform?

Terraform is an open-source Infrastructure as Code (IaC) tool developed by HashiCorp. It lets you write code to define and manage cloud infrastructure in a safe, consistent, and repeatable way.


🎯 What Will We Build?

We’ll create:

  • A Resource Group in Azure (think of it like a container for your resources)
  • A Log Analytics Workspace, which is the foundation for enabling Microsoft Sentinel later

And we’ll make our code reusable by using Terraform variables.


📁 Step-by-Step Project Setup

Let’s get started!

✅ Step 1: Create a Working Folder

First, create a new folder on your computer for your Terraform project:

mkdir sentinel-deploy
cd sentinel-deploy

✅ Step 2: Create Required Files

Inside your folder, create these 3 files:

File NamePurpose
main.tfThe main Terraform configuration (what to build)
variables.tfHolds input values like resource names, locations
outputs.tf(Optional) Displays output values after deployment

You can create these using a text editor like VS Code, Notepad++, or even Notepad.


📄 File 1: main.tf

This is where you define what Terraform will create.

provider "azurerm" {
  features {}
}

# Create a new Resource Group
resource "azurerm_resource_group" "rg" {
  name     = var.resource_group_name
  location = var.location
}

# Create a Log Analytics Workspace
resource "azurerm_log_analytics_workspace" "law" {
  name                = var.workspace_name
  location            = var.location
  resource_group_name = azurerm_resource_group.rg.name
  sku                 = "PerGB2018"
  retention_in_days   = 30
}

✅ This file tells Terraform to:

  • Connect to Azure
  • Create a resource group
  • Create a Log Analytics Workspace inside that group

📄 File 2: variables.tf

This is where you define user-configurable variables and give them default values.

variable "location" {
  description = "Azure region for resources"
  default     = "South Central US"
}

variable "resource_group_name" {
  description = "Name of the Resource Group"
  default     = "sentinel-demo-rg"
}

variable "workspace_name" {
  description = "Name of the Log Analytics Workspace"
  default     = "sentinel-law-demo"
}

✅ This allows you to reuse the code in any environment by just changing values.


📄 File 3: outputs.tf (Optional)

This file is optional, but it helps you see useful information after your deployment.

output "sentinel_workspace_id" {
  value = azurerm_log_analytics_workspace.law.id
}

✅ This will print the Log Analytics Workspace ID to your terminal after running Terraform.


🛠️ Step 3: Initialize Terraform

Open your terminal or PowerShell and run:

terraform init

📌 This installs necessary plugins (like the Azure provider).


🔍 Step 4: Review the Plan

Before applying, see what Terraform plans to do:

terraform plan

📌 This gives you a dry-run preview of what will be created or changed.


🚀 Step 5: Apply the Plan

Now let’s deploy it!

terraform apply

Terraform will show you the plan again and ask for confirmation. Type yes and hit Enter.

✅ In a minute or two, you’ll have:

  • A new Resource Group in Azure
  • A Log Analytics Workspace inside it

🧹 Step 6 (Optional): Destroy the Resources

If you want to remove everything Terraform created, use:

terraform destroy

📌 Summary

What We DidTool Used
Defined Azure resources as codeTerraform
Made the code flexible with variablesvariables.tf
Created infra using simple commandsinit, plan, apply

🧠 Why This Is Powerful

You just automated part of your Azure infrastructure!

And the best part?

  • It’s repeatable
  • It’s auditable
  • It’s version-controllable (you can use Git)

This setup is also the foundation for Microsoft Sentinel deployments — now you’re ready to go deeper!


🙌 Next Steps

In the next blog, we’ll cover:

  • Enabling Microsoft Sentinel
  • Adding data connectors
  • Creating analytics rules and automation

💬 Got Questions?

Leave a comment below or reach out to me on LinkedIn or YouTube for one-on-one training on Microsoft Sentinel, Azure Logic Apps, and more!

Leave a Reply

Discover more from SecByte

Subscribe now to keep reading and get access to the full archive.

Continue reading