Day 23: Terraform Providers & Modules – Structuring Your Code (Azure Cloud)
🔹 Imagine this: You're deploying multiple Azure resources—Virtual Machines, Storage Accounts, Networking components, and Databases.
Now, imagine doing this manually every time for different environments—Development, Testing, and Production. Sounds repetitive, time-consuming, and error-prone, right?
What if you could write once, reuse everywhere, and scale effortlessly?
This is exactly what Terraform Providers & Modules help us achieve!
In this newsletter, we will cover:
✅ Terraform Providers – Connecting Terraform to Azure
✅ Terraform Modules – Making Code Reusable & Scalable
✅ Step-by-Step Implementation with Azure
🔹 What is a Terraform Provider?
A Terraform Provider is a plugin that allows Terraform to interact with cloud services (like Azure, AWS, GCP, Kubernetes, etc.).
For Azure, we use the azurerm provider to manage Azure resources.
🔹 Example – Configuring Azure Provider in Terraform
provider "azurerm" {
features {}
}
💡 Real-World Use Case:
A DevOps team managing Azure resources can use the Azure provider to:
✔ Automate the provisioning of Virtual Machines, Networking, and Storage
✔ Deploy Infrastructure across multiple environments (Dev, QA, Prod)
✔ Maintain consistency in cloud deployments
Terraform Module:
A Terraform module is a reusable, self-contained collection of Terraform configuration files that manage a specific piece of infrastructure.
It groups multiple resources together into a single logical unit that can be used and reused across different projects and environments.
Think of it as a function in programming—just like a function allows code reuse and modularity, a Terraform module enables infrastructure reuse and better organization.
Why Use Terraform Modules in Azure?
Using Terraform modules in Azure brings several benefits:
✅ 1. Reusability Modules allow you to define Azure infrastructure once and reuse it across multiple projects/environments.
🔹 Example: Instead of writing the same Azure Virtual Machine (VM) setup multiple times, you create a reusable Azure VM module.
✅ 2. Maintainability Organizing Terraform configurations into modules makes it easier to manage, update, and troubleshoot Azure infrastructure.
🔹 Example: If a networking configuration (Azure Virtual Network) needs to change, you only update the VNet module rather than modifying multiple configurations.
✅ 3. Scalability Modules help scale infrastructure by allowing consistent provisioning of resources across different environments (e.g., Dev, QA, Prod).
🔹 Example: A VNet module can create different-sized networks dynamically based on input variables.
✅ 4. Consistency & Standardization Using modules ensures Azure infrastructure follows best practices and compliance requirements.
🔹 Example: A security module can enforce specific Azure Role-Based Access Control (RBAC) policies across teams.
✅ 5. Easier Collaboration Teams can work on different modules independently, improving collaboration and parallel development.
🔹 Example: The networking team manages the Azure Network module, while the application team configures the Azure App Service module.
✅ 6. Faster Deployment Using pre-built modules speeds up Azure infrastructure provisioning, reducing deployment time.
Structure of a Terraform Module
A Terraform module typically consists of:
🛠 Step-by-Step: Creating a Reusable Terraform Module for Azure Virtual Network, VM, and Storage
Let’s modularize the deployment of an Azure Virtual Network, VM, and Storage.
🔹 Step 1: Set Up Directory Structure
Create the following folder structure:
terraform-modules/
├── modules/
│ ├── vnet/
│ │ ├── main.tf # Virtual Network resource
│ │ ├── variables.tf # Input variables
│ │ ├── outputs.tf # Outputs
│ ├── vm/
│ │ ├── main.tf # Virtual Machine resource
│ │ ├── variables.tf # Input variables
│ │ ├── outputs.tf # Outputs
│ ├── storage/
│ │ ├── main.tf # Storage Account resource
│ │ ├── variables.tf # Input variables
│ │ ├── outputs.tf # Outputs
├── main.tf # Calls the modules
├── variables.tf # Global variables
├── outputs.tf # Global outputs
├── providers.tf # Azure provider configuration
├── terraform.tfvars # Variable values
🔹 Step 2: Configure the Provider
Create providers.tf in the root directory:
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.0"
}
}
}
provider "azurerm" {
features {}
}
👉 Explanation:
🔹 Step 3: Create a Virtual Network Module
Inside modules/vnet/, create main.tf, variables.tf, and outputs.tf.
🔹 main.tf (VNet Module)
resource "azurerm_virtual_network" "vnet" {
name = var.vnet_name
location = var.location
resource_group_name = var.resource_group_name
address_space = [var.address_space]
}
👉 Explanation:
🔹 variables.tf (VNet Module)
variable "vnet_name" {}
variable "location" {}
variable "resource_group_name" {}
variable "address_space" {}
👉 Explanation:
Recommended by LinkedIn
🔹 outputs.tf (VNet Module)
output "vnet_id" {
value = azurerm_virtual_network.vnet.id
}
👉 Explanation:
🔹 Step 4: Create a Virtual Machine Module
Inside modules/vm/, create main.tf, variables.tf, and outputs.tf.
🔹 main.tf (VM Module)
resource "azurerm_virtual_machine" "vm" {
name = var.vm_name
location = var.location
resource_group_name = var.resource_group_name
network_interface_ids = [var.nic_id]
vm_size = var.vm_size
storage_os_disk {
name = "${var.vm_name}-os-disk"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
os_profile {
computer_name = var.vm_name
admin_username = var.admin_username
admin_password = var.admin_password
}
os_profile_linux_config {
disable_password_authentication = false
}
}
👉 Explanation:
🔹 variables.tf (VM Module)
variable "vm_name" {}
variable "location" {}
variable "resource_group_name" {}
variable "vm_size" {}
variable "nic_id" {}
variable "admin_username" {}
variable "admin_password" {}
🔹 outputs.tf (VM Module)
output "vm_id" {
value = azurerm_virtual_machine.vm.id
}
👉 Explanation:
🔹 Step 5: Create a Storage Account Module
Inside modules/storage/, create main.tf, variables.tf, and outputs.tf.
🔹 main.tf (Storage Module)
resource "azurerm_storage_account" "storage" {
name = var.storage_account_name
resource_group_name = var.resource_group_name
location = var.location
account_tier = "Standard"
account_replication_type = "LRS"
}
🔹 variables.tf (Storage Module)
variable "storage_account_name" {}
variable "resource_group_name" {}
variable "location" {}
🔹 outputs.tf (Storage Module)
output "storage_account_id" {
value = azurerm_storage_account.storage.id
}
🔹 Step 6: Call the Modules from Root
Modify main.tf in the root directory to call these modules.
🔹 main.tf (Root)
module "vnet" {
source = "./modules/vnet"
vnet_name = var.vnet_name
location = var.location
resource_group_name = var.resource_group_name
address_space = var.address_space
}
module "vm" {
source = "./modules/vm"
vm_name = var.vm_name
location = var.location
resource_group_name = var.resource_group_name
vm_size = var.vm_size
nic_id = "NIC_ID" # Replace with actual value
admin_username = var.admin_username
admin_password = var.admin_password
}
module "storage" {
source = "./modules/storage"
storage_account_name = var.storage_account_name
resource_group_name = var.resource_group_name
location = var.location
}
👉 Explanation:
🔹 Step 7: Define Variables in Root
Modify variables.tf in the root directory.
variable "location" {}
variable "resource_group_name" {}
variable "vnet_name" {}
variable "address_space" {}
variable "vm_name" {}
variable "vm_size" {}
variable "admin_username" {}
variable "admin_password" {}
variable "storage_account_name" {}
🔹 Step 8: Apply Terraform Configuration
Run the following commands in the terminal:
terraform init # Initialize Terraform
terraform plan # Preview changes
terraform apply # Deploy infrastructure
🔹 Real-World Use Cases of Terraform Modules in Azure
✔ Multi-Environment Deployments: Use modules to deploy identical infrastructure for Dev, QA, and Prod.
✔ Infrastructure Standardization: Teams can create reusable modules for VMs, Networking, Storage, ensuring consistency across projects.
✔ Easier Maintenance: Updating the module automatically applies changes across all environments.
🔹 Key Takeaways
✔ Terraform Providers allow Terraform to interact with cloud services like Azure.
✔ Terraform Modules help organize and reuse code, making infrastructure deployment efficient.
✔ Modular Terraform Code simplifies multi-environment management, reducing errors and increasing automation.
By implementing Terraform Modules, you can create scalable, maintainable, and efficient cloud infrastructure deployments.
📢 Next Up: 🔹 Day 24 – Terraform State Management – Handling Changes
🔹 How does Terraform track your deployed infrastructure?
🔹 What happens when you change your Terraform code?
🔹 How can you prevent accidental deletions in Terraform?
Stay tuned for Terraform State Management & Best Practices in Day 24!
If you found this useful, follow me for more deep-dive DevOps content!
🔗 Follow Shruthi Chikkela for daily insights
📩 Subscribe to my 100-day DevOps challenge for expert-level content
#Terraform #Azure #DevOps #InfrastructureAsCode #CloudComputing #IaC #TerraformModules #AzureDevOps #Learnwithshruthi