Hitesh Sahu
Hitesh SahuHitesh Sahu
  1. Home
  2. β€Ί
  3. posts
  4. β€Ί
  5. …

  6. β€Ί
  7. 4 Module

Loading ⏳
Fetching content, this won’t take long…


πŸ’‘ Did you know?

πŸ¦₯ Sloths can hold their breath longer than dolphins 🐬.

πŸͺ This website uses cookies

No personal data is stored on our servers however third party tools Google Analytics cookies to measure traffic and improve your website experience. Learn more

Loading ⏳
Fetching content, this won’t take long…


πŸ’‘ Did you know?

🍌 Bananas are berries, but strawberries are not.
Terraform

    AI-AgenticAI

    AI-DeepLearning

    AI-GenAI

    AI-Infrastructure

    AI-Machine-Learning

    AI-Math

    AWS

    Azure

    Hobbies

    kubernetes

    Management

    Programming

    Terraform
    • Terraform Certification Path

    • Terraform Basics

    • Terraform Configuration Management

    • TF Modules: How to Use & Create

    • TF State & Backend Management

    • Terraform Core Workflow & Commands

    • IaC Concepts & TF Overview

    • TF Cloud Capabilities & Workflow

    • TF CMD Cheatsheet

    • Terraform Index


    Z_Appendix

    0-root

Cover Image for TF Modules: How to Use & Create
Terraform

TF Modules: How to Use & Create

Learn how to create and use reusable TF modules for scalable, maintainable, and shareable infrastructure configurations. Best practices and versioning tips included.

Terraform
Cloud
Infrastructure as Code
IaC
DevOps
← Previous

Terraform Basics

Next β†’

TF State & Backend Management

Terraform Modules: How to Use & Create

Terraform modules - 11%

Objective

  • Contrast and use different module source options including the public Terraform Module Registry
  • Interact with module inputs and outputs
  • Describe variable scope within modules/child modules
  • Set module version

Modules

Small, reusable Terraform configurations that let you manage a group of related resources as if they were a single resource.

A collection of resources that make up a specific piece of infrastructure

  • βœ… Initialize & download (providers + modules) using: terraform init
  • βœ… Refresh/download just the modules (without a full re-init) using: terraform get

        β”œβ”€β”€ LICENSE
        β”œβ”€β”€ README.md
        β”œβ”€β”€ main.tf
        β”œβ”€β”€ variables.tf
        β”œβ”€β”€ outputs.tf

Modules Usage

  • Organize configuration
  • Encapsulate configuration
  • Re-use configuration

Loading Modules

Terraform cache these modules in the .terraform/modules subdirectory in the current working directory

Modules can either be loaded from the local filesystem directory


  module "website_s3_bucket" {
   source = "./modules/aws-s3-static-website-bucket"
  }

or a remote source such as

  • Terraform Registry
  • version control systems,
  • HTTP URLs
  • Terraform Cloud
  • Terraform Enterprise private module registries.

Verified Module

Reviewed by Hashicorp & maintined by contributors

  • Verified badge is shown for official modules
  • Unverified module does not mean low quality and vice versa

Note: Only the official or verified modules are visible in Terraform Registry search results

Child Module

A module that is called by another configuration is referred to as a child module of that configuration.

  module "servers" {
      source = "./app-cluster" # Child Module
  }

Output

module.<MODULE NAME>.<OUTPUT NAME>

Access IP address of

module.web_server.instance_ip_addr.

Supported Arguments

  • The source argument is mandatory for all modules.
  • The version argument is recommended for modules from a registry.
  • Meta-arguments
    • count - Creates multiple instances of a module from a single module block.
    • for_each - Creates multiple instances of a module from a single module block.
    • providers - Passes provider configurations to a child module.
    • depends_on - Creates explicit dependencies between the entire module and the listed targets.

Public Module

downloadeble from Terrform registry

  • Accesses by <Namespace>/<Name>/<Provider>

Publishing a Public module

  • βœ… The module must be on GitHub and must be a public repo. Not needed for private module
  • βœ… x.y.z tags for releases: at least one release tag must be present e.g. v1.0.4 and 0.9.2
  • βœ… Module repositories must use this three-part name format, terraform-<PROVIDER>-<NAME> e.g. terraform-aws-vpc
  • Follow standard module structure.

Private Module

Downloadable from Terrform Cloud after terrform login or using API token in CLI

  • Accesses by <HostName>/<Namespace>/<Name>/<Provider>

Module Sources

Local paths

  #local module 
  module "servers" {
    source = "./app-cluster"
  }

From Terraform Registry


  # Public module
  # <Namespace>/<Name>/<Provider>
  module "consul" {
    source  = "hashicorp/consul/aws"
    version = "0.0.5"
  }

  # Private Module

  # <HostName>/<Namespace>/<Name>/<Provider>
  module "consul" {
    source  = "app.terraform.io/example-corp/k8s-cluster/azurerm"
    version = "1.1.0"
  }

Github


  # HTTP
  module "consul" {
    source = "github.com/hashicorp/example"
  }
  
  # SSH
  module "consul" {
    source = "git@github.com:hashicorp/example.git"
  }

  # select a specific tag
  module "vpc" {
    source = "git::https://example.com/vpc.git?ref=v1.2.0"
  }
  
  #  Generic Git repo with HTTP
  module "vpc" {
    source = "git::https://example.com/vpc.git"
  }

  #  Generic Git repo with SSH
  module "storage" {
    source = "git::ssh://username@example.com/storage.git"
  }

  # directly select a commit using its SHA-1 hash
  module "storage" {
    source = "git::https://example.com/storage.git?ref=51d462976d84fdea54b47d80dcabbf680badcdb8"
  }

BitBucket

  module "consul" {
   source = "bitbucket.org/hashicorp/terraform-consul-aws"
  }

HTTP

  module "vpc" {
    source = "https://example.com/vpc-module?archive=zip"
  }

Supported Zipped format

  • zip
  • tar.bz2 and tbz2
  • tar.gz and tgz
  • tar.xz and txz

S3 Bucket


  module "consul" {
    source = "s3::https://s3-eu-west-1.amazonaws.com/examplecorp-terraform-modules/vpc.zip"
  }

GCP bucket


  module "consul" {
    source = "gcs::https://www.googleapis.com/storage/v1/modules/foomodule.zip"
  }

Hitesh Sahu
Written by Hitesh Sahu, a passionate developer and blogger.

Wed Feb 25 2026

Share This on

← Previous

Terraform Basics

Next β†’

TF State & Backend Management

Terraform/4-Module
Let's work together
+49 176-2019-2523
hiteshkrsahu@gmail.com
WhatsApp
Skype
Munich πŸ₯¨, Germany πŸ‡©πŸ‡ͺ, EU
Playstore
Hitesh Sahu's apps on Google Play Store
Need Help?
Let's Connect
Navigation
Β  Home/About
Β  Skills
Β  Work/Projects
Β  Lab/Experiments
Β  Contribution
Β  Awards
Β  Art/Sketches
Β  Thoughts
Β  Contact
Links
Β  Sitemap
Β  Legal Notice
Β  Privacy Policy

Made with

NextJS logo

NextJS by

hitesh Sahu

| Β© 2026 All rights reserved.