module block: 2. Module block

1. Overview:

  • A module:
    • A container for multiple resources that are used together
    • A collections of .tf and/or .tf.json in a directory
      • Each must defines a distinct set of config objects
      • Not in nested directory
  • The root module:
    • A Terraform config has atleast 1 module which is the root module
    • Contains all .tf files in the working directory
  • Child module:
    • A Terraform module can call other modules to include their resources in the configuration
  • Published modules:
    • Terraform can load modules from public (Terraform Registry) or private registry
  • Using modules:

2. Module block:

  • Calling a child module:
    • To cal a module means to include the contents of that module into the configuration with specific values for its Input variables
    • module "<LABEL>" {
         <module-specific-inputs>
         source = "<location-of-module-sources>"
         version = "<constraint>" # only available for modules listed in a registry
         count = <number> # mutually exclusive with `for_each`
         for_each = {          # mutually exclusive with `count`
             <KEY> = <VALUE>
          }
          for_each = [       # `for_each` accepts a map or a set of strings
           "<VALUE>",
           "<VALUE>"
          ]
         provider = "<alias.provider-configuration>"
         depends_on = [ <resource.address.reference> ]
    } ```
    • This block is called the calling module of the child module
      • The label right after is the local-name
    • Module call use arguments:

3. Module sources:

  • local path
  • registry:
    • source = "<NAMESPACE>/<NAME>/<PROVIDER>"
    • for HCP terraform private registry: source = "app.terraform.io/<NAMESPACE>/<NAME>/<PROVIDER>"
    • For Terraform enterprise: source = "<HOSTNAME>/<NAMESPACE>/<NAME>/<PROVIDER>"
    • For Github over https: source = "github.com/<ORGANIZATION>/<MODULE-FOLDER>"
    • For github over ssh: source = "git:github.com/<ORGANIZATION>/<MODULE-FOLDER>"

4. Module meta-arguments:

  • s

5. Module development: