Skip to main content
How to Use Terraform Modules - Build Reusable Infrastructure

How to Use Terraform Modules - Build Reusable Infrastructure

Key Takeaway

Create and use Terraform modules for reusable infrastructure code. Covers module structure, inputs, outputs, versioning, and the Terraform Registry.

Table of Contents

Introduction

This comprehensive guide covers everything you need to know with practical, copy-paste examples for your Terraform projects.

Prerequisites

  • Terraform v1.5+ installed
  • Cloud provider credentials configured
  • Basic HCL knowledge

Step-by-Step Guide

Core Concepts

Understanding this topic is essential for writing production-ready Terraform code that scales across teams and environments.

Configuration Example

terraform {
  required_version = ">= 1.5"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = var.region
}

Implementation

variable "environment" {
  type        = string
  description = "Deployment environment"
  default     = "dev"
  
  validation {
    condition     = contains(["dev", "staging", "prod"], var.environment)
    error_message = "Must be dev, staging, or prod."
  }
}

locals {
  name_prefix = "myapp-${var.environment}"
  common_tags = {
    Environment = var.environment
    ManagedBy   = "terraform"
    Project     = var.project_name
  }
}

resource "aws_instance" "app" {
  ami           = data.aws_ami.latest.id
  instance_type = local.is_prod ? "t3.large" : "t3.micro"
  tags          = merge(local.common_tags, { Name = "${local.name_prefix}-app" })
}

Advanced Usage

For production environments, consider these patterns:

# Dynamic blocks for flexible configuration
resource "aws_security_group" "app" {
  name = "${local.name_prefix}-sg"
  
  dynamic "ingress" {
    for_each = var.ingress_rules
    content {
      from_port   = ingress.value.port
      to_port     = ingress.value.port
      protocol    = "tcp"
      cidr_blocks = ingress.value.cidrs
    }
  }
}

Best Practices

  1. Use version constraints — pin providers and modules
  2. Separate environments — use workspaces or directory structure
  3. Enable remote state — S3 + DynamoDB for teams
  4. Run plan first — always review before applying
  5. Use modules — DRY your infrastructure code
  6. Tag everything — consistent tagging for cost tracking

Common Mistakes

  • Hardcoding values instead of using variables
  • Not using remote state in team environments
  • Ignoring terraform plan output before applying
  • Missing lifecycle blocks on critical resources
  • Not pinning provider versions

Hands-On Courses

Conclusion

Apply these patterns in your next Terraform project for cleaner, more maintainable infrastructure code. Bookmark this guide for quick reference.

🚀

Level Up Your Terraform Skills

Hands-on courses, books, and resources from Luca Berton

Luca Berton
Written by

Luca Berton

DevOps Engineer, AWS Partner, Terraform expert, and author. Creator of Ansible Pilot, Terraform Pilot, and CopyPasteLearn.