Terraform is an incredibly powerful tool for managing infrastructure as code. It not only supports multiple cloud providers such as Amazon Web Services (AWS) and Microsoft Azure but also allows you to create reusable components known as “modules.” These modules can be used to encapsulate complex setups into manageable, reusable, and simplified pieces. This post will guide you through the basics of using Terraform modules, with practical examples in both AWS and Azure environments.

What Are Terraform Modules?

Terraform modules are containers for multiple resources that are used together. A module can include resources, input variables, output values, and even other modules. Using modules helps to keep Terraform code DRY (Don’t Repeat Yourself), organized, and easy to maintain and update.

Creating a Basic Module

Before diving into the cloud-specific examples, let’s define a simple module structure. A typical Terraform module contains the following:

  • Input Variables: Parameters used to customize the module.
  • Resources: Infrastructure components created by the module.
  • Output Values: Data returned by the module, often used by other parts of your Terraform configuration.

Here’s a simple directory structure for a module:

modules/
│
├── aws_ec2_instance/
│   ├── main.tf
│   ├── variables.tf
│   └── outputs.tf
│
└── azure_vm/
    ├── main.tf
    ├── variables.tf
    └── outputs.tf

Example 1: AWS EC2 Instance Module

This example will create a module for deploying an EC2 instance in AWS.

modules/aws_ec2_instance/main.tf

resource "aws_instance" "example" {
  ami           = var.ami
  instance_type = var.instance_type
  tags = {
    Name = var.instance_name
  }
}

modules/aws_ec2_instance/variables.tf

variable "ami" {
  description = "The AMI to use for the instance"
}

variable "instance_type" {
  description = "The type of instance to start"
}

variable "instance_name" {
  description = "The name tag of the instance"
  default     = "ExampleInstance"
}

modules/aws_ec2_instance/outputs.tf

output "instance_id" {
  value = aws_instance.example.id
}

Using the AWS EC2 Module

In your main Terraform configuration:

module "ec2_instance" {
  source         = "./modules/aws_ec2_instance"
  ami            = "ami-123456"
  instance_type  = "t2.micro"
}

Example 2: Azure VM Module

This example will create a module for deploying a virtual machine in Azure.

modules/azure_vm/main.tf

resource "azurerm_virtual_machine" "main" {
  name                  = "vm-${var.vm_name}"
  location              = var.location
  resource_group_name   = var.resource_group
  network_interface_ids = [azurerm_network_interface.main.id]
  vm_size               = "Standard_DS1_v2"

  storage_image_reference {
    id = var.image_id
  }

  os_profile {
    computer_name  = var.vm_name
    admin_username = var.admin_username
    admin_password = var.admin_password
  }
}

modules/azure_vm/variables.tf

variable "vm_name" {
  description = "Name of the VM"
}

variable "location" {
  description = "Azure region to deploy the VM"
}

variable "resource_group" {
  description = "Resource group name where the VM will be created"
}

variable "image_id" {
  description = "The image ID to use for the VM"
}

variable "admin_username" {
  description = "Admin username for the VM"
}

variable "admin_password" {
  description = "Admin password for the VM"
}

modules/azure_vm/outputs.tf

output "vm_id" {
  value = azurerm_virtual_machine.main.id
}

Using the Azure VM Module

In your main Terraform configuration:

module "azure_vm" {
  source          = "./modules/azure_vm"
  vm_name         = "testvm"
  location        = "East US"
  resource_group  = "MyResourceGroup"
  image_id        = "/subscriptions/.../MyImage"
  admin_username  = "adminuser"
  admin_password  = "SecurePassword123"
}

Conclusion

Terraform modules provide an efficient way to manage infrastructure components across different cloud environments, making your Terraform code more modular, reusable, and maintainable. By using modules for common resources like VMs in AWS and Azure, you can standardize infrastructure components across projects and teams, reducing errors and deployment times. Whether you’re managing a small cloud environment or a large-scale multi-cloud architecture, Terraform modules can help streamline your infrastructure management processes.