Day 3: Terraform Infrastructure as Code (IaC) – Modular Approach for Azure Resources
Terraform is a powerful tool for managing cloud infrastructure as code (IaC). When working with a provider like Azure, you may need to create and manage resources such as Virtual Networks (VNets), Azure Functions, and Azure Kubernetes Service (AKS). A monolithic approach—where all configurations are defined in a single Terraform file—might seem simple at first but quickly becomes unmanageable as the infrastructure grows in complexity.
Instead, a modular approach, much like microservices in application development, provides a more scalable, maintainable, and reusable way to organize infrastructure. Terraform modules allow us to structure configurations into reusable components that simplify both development and management.
Why Use Modules in Terraform for Azure?
Reusability: Modules enable you to create components once and reuse them across multiple environments or projects.
Maintainability: Breaking infrastructure into modules simplifies updates and reduces complexity in your Terraform configurations.
Collaboration: Teams can work on separate modules without conflicts, fostering better collaboration.
Scalability: Modular architecture makes it easier to scale individual components or adapt to changing requirements.
Abstraction: Modules encapsulate complexity, exposing only necessary inputs and outputs while hiding implementation details.
Example: Modularizing Azure Resources
Suppose you are building an Azure project named xyz
. Instead of defining configurations for VNets, Azure Functions, and AKS in a single Terraform file, you can create a module for each:
VNet Module: Handles the creation of Virtual Networks, subnets, and Network Security Groups (NSGs).
Azure Functions Module: Manages Azure Function Apps, associated storage accounts, and Application Insights.
AKS Module: Provisions an Azure Kubernetes Service cluster and configures node pools and networking.
Each module will reside in its own directory, with dedicated main.tf
, variables.tf
, and outputs.tf
files.
Implementing a Modular Architecture
Directory Structure
Here’s an example structure for your project:
cssCopyEditterraform/
├── modules/
│ ├── vnet/
│ │ ├── main.tf
│ │ ├── variables.tf
│ │ ├── outputs.tf
│ ├── azure_functions/
│ │ ├── main.tf
│ │ ├── variables.tf
│ │ ├── outputs.tf
│ └── aks/
│ ├── main.tf
│ ├── variables.tf
│ ├── outputs.tf
├── main.tf
├── variables.tf
└── outputs.tf
Calling Modules
In the root main.tf
file, you call each module with the required inputs:
hclCopyEditmodule "vnet" {
source = "./modules/vnet"
address_space = ["10.0.0.0/16"]
location = "East US"
resource_group_name = "my-resource-group"
}
module "azure_functions" {
source = "./modules/azure_functions"
function_name = "my-function-app"
location = "East US"
resource_group_name = "my-resource-group"
}
module "aks" {
source = "./modules/aks"
cluster_name = "my-aks-cluster"
location = "East US"
resource_group_name = "my-resource-group"
dns_prefix = "my-aks-dns"
}
Each module encapsulates the logic for managing its resources. For example, the vnet
module may define subnets and NSGs, while the aks
module focuses on creating the Kubernetes cluster and configuring its node pools.
Benefits of Using Modules in Azure
Organized Codebase: Modular design keeps your Terraform configuration clean and structured.
Consistency Across Environments: Use modules to standardize infrastructure components like networking or Kubernetes clusters.
Ease of Updates: Isolate changes to a specific module without impacting others.
Scalable Deployments: Add or modify resources independently using modules, ensuring seamless scaling.