Scroll to top
© 2024, Global Digital Services LLC.

Create Terraform module to management IaC


Fernando Noguera - March 28, 2023 - 0 comments

In this blog, we’ve explored how to create a Terraform module that provisions an Azure resources. We’ve seen how Terraform modules can help us create reusable code and abstract away the details of how infrastructure is created, with Terraform modules, we can improve the consistency of our infrastructure, reduce errors, and save time.

Creating Terraform modules is an essential skill for any infrastructure engineer working with Terraform. By following the steps outlined in this blog post, you’ll be well on your way to creating your own Terraform modules and managing your infrastructure with ease.

¿What is a Terraform Module?

A Terraform module is a collection of resources that can be used together to create a specific piece of infrastructure. Modules allow you to abstract away the details of how the infrastructure is created and provide a simple, reusable interface for managing the infrastructure.

Benefits of Using Terraform Modules

There are several benefits to using Terraform modules:

  1. Reusability: Modules allow you to write reusable code that can be used across multiple Terraform configurations.
  2. Consistency: Modules make it easier to ensure that your infrastructure is consistent across different environments.
  3. Collaboration: Modules can be shared with other members of your team or the wider community, making it easier to collaborate and share knowledge.
  4. Maintainability: Modules make it easier to maintain your infrastructure code over time, as you can make changes in one place and have those changes reflected across all configurations that use the module.
Creating a Terraform Module

To create a Terraform module with Azure, you’ll need to create a new directory and add your Terraform code to it. The directory should contain a main.tf file, which defines the resources you want to create, as well as any variables or outputs that you want to use.

Let make a example, create a Terraform module about Storage Account in Azure, please follow these steps:

  1. Create a new directory for your Terraform module, e.g., terraform-azure-storage-account.
  2. Create a new file named main.tf in the directory.
  3. Define the provider for Azure in the main.tf file:
provider "azurerm" {
features {}
}
  1. Define input variables in a file named variables.tf:
variable "resource_group_name" {
type = string
}
variable "storage_account_name" {
type = string
}
variable "location" {
type = string
}
  1. Define the storage account resource in the main.tf file:
resource "azurerm_storage_account" "example_storage_account" {
name = var.storage_account_name
resource_group_name = var.resource_group_name
location = var.location
account_tier = "Standard"
account_replication_type = "LRS"
}
  1. Define output variables in a file named outputs.tf:
output "storage_account_id" {
value = azurerm_storage_account.example_storage_account.id
}
output "storage_account_primary_blob_endpoint" {
value = azurerm_storage_account.example_storage_account.primary_blob_endpoint
}

With this module, you can easily create a new Azure Storage Account by specifying the input variables:

module "example_storage_account" {
source = "./terraform-azure-storage-account"
resource_group_name = "example-resource-group"
storage_account_name = "examplestorageaccount"
location = "eastus"
}

And then use the output variables in other Terraform resources or modules, if you need more information check the official documentation.

Contact us for more information or visit our blog.

Related posts