12. Modules

Introduction

  • Modules are containers for multiple resources that are used together. A module consists of a collection of .tf and/or .tf.json files kept together in a directory

  • Modules are main way to package and reuse resource configurations with terraform

  • Input variables are passed in via module block


Types

1. Root Module

Default module containing all .tf files in main working directory

2. Child Module

A sepearte external module referred to from a .tf file


Module Sources

Local paths

module "web-app" {
  source = "../web-app"
}

Terraform Registry

module "web-app" {
  source = "hashicorp/consul/aws"
  version = "0.1.0"
}

Github

  • via HTTPS

  • via SSH

  • Generic i.e source = "git::ssh//username@example.com/storage.git"

Others

  • Bitbucket

  • Generic Git

  • HTTP URLs

  • S3 buckets


Good Module Characteristics

  • Raise abstraction level from base resource types

  • Group resources in a logical fashion

  • Exposes input variables to allow necessary customization + composition

  • Provide useful defaults

  • Returns outputs to make further integrations possible


Last updated