Hitesh Sahu
Hitesh SahuHitesh Sahu
  1. Home
  2. ›
  3. posts
  4. ›
  5. …

  6. ›
  7. 4 Module

Loading ā³
Please wait...

šŸŖ 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

Cover Image for Terraform Module: how to use and create

Terraform Module: how to use and create

Reusable, modular Terraform configurations that can be shared and versioned

Hitesh Sahu
Hitesh Sahu

Mon Sep 29 2025

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

  • āœ… Init using: terraform get

  • āœ… Downoloaded using: terraform init

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

Used to

  • Organize configuration
  • Encapsulate configuration
  • Re-use configuration

Loading Modules

erraform 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.

Varified Module

Reviewed by Hashicorp & maintined by contributors

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

Note: Only the official or varified 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

Downloadeble 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 SSSH
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"

}