Infrastructure as Code (IaC) has become a cornerstone of modern cloud management, and HashiCorp Terraform is a powerful tool for achieving this. Terraform allows you to create reusable components called modules, enabling you to build consistent and scalable infrastructure in Azure. In this article, we’ll guide you through the process of creating and using your first Terraform module for Azure resources within a sub-folder in your project.
Step 1: Set Up Your Terraform Project
First, ensure you have Terraform installed on your system. You can download it from the official website (https://www.terraform.io/downloads.html) and follow the installation instructions.
Create a new directory for your Terraform project and navigate to it in your terminal:
mkdir my-terraform-project
cd my-terraform-project
Step 2: Define Your Module Structure
Inside your project directory, create a sub-folder for your Azure module. Let’s create a simple module that provisions an Azure Storage Account. We’ll name the module folder storage-module
:
mkdir storage-module
cd storage-module
Step 2: Define Your Module Structure
Inside your project directory, create a sub-folder for your Azure module. Let’s create a simple module that provisions an Azure Storage Account. We’ll name the module folder storage-module
:
mkdir storage-module
cd storage-module
Step 3: Create the Module Configuration
Within the storage-module
folder, create a new file named main.tf
. This file will contain the configuration for your Azure Storage Account:
# main.tf
resource "azurerm_storage_account" "example" {
name = "b59storageaccount"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
account_tier = "Standard"
account_replication_type = "LRS"
}
In this example, we’re using the azurerm_storage_account
resource to define an Azure Storage Account with specific settings. Keep in mind, this is a really simple example of a module, as it does not include any module parameters or input variables. A module this simple will allow you to modularize your Terraform project code, but parameterizing the module would allow for additional code reuse across the project.
Further Reading: The “Terraform Modules: Create Reusable Infrastructure as Code” article contains an expanded explanation on creating and using Terraform Modules.
Step 4: Initialize and Validate
Navigate back to the root directory of your project and initialize Terraform to download the necessary Azure provider plugins:
terraform init
Ensure there are no errors in your configuration by running:
terraform validate
Step 5: Use Your Module
Now that your module is ready, you can use it in your main Terraform configuration. Create a new file named main.tf
in your project’s root directory and use the module you’ve created:
# main.tf
module "my_storage_module" {
source = "./storage-module"
}
resource "azurerm_resource_group" "example" {
name = "myResourceGroup"
location = "East US"
}
In this example, we first declare a resource group, which the storage account depends on. Then, we use the module
block to include your storage-module
.
By following these steps, you’ve created your first Terraform module for Azure resources. This modular approach simplifies your Terraform configurations, enhances reusability, and helps maintain a structured and efficient IaC project in Azure.