This is Part 5 of the Terraform Associate Exam Cram. It covers the following Terraform Associate Certification exam objectives:
< Prev:
Objective 4 - Use Terraform outside the core workflow
Next >:
Objective 6 - Use the core Terraform workflow
A Terraform module is a set of Terraform configuration files in a single directory.
Every Terraform configuration has at least one module, known as its root module, which consists
of the resources defined in the .tf files in the main working directory.
To call a module means to include the contents of that module into the configuration with specific values
for its input variables. Modules are called from within other modules using module blocks.
A module that includes a module block is the calling module of the child module.
module block configuration:
module keyword is the module's local name, which
the calling module can use to refer to this instance of the module using the format
module.<MODULE_NAME>.source = "<PATH-TO-MODULE>", where <PATH-TO-MODULE>
is a relative (with ./ or ../ prefix) or absolute path to local module source code.
For example: source = "./vpc".source = "<NAMESPACE>/<NAME>/<PROVIDER>", e.g.,
source = "hashicorp/consul/aws".source = "<HOSTNAME>/<NAMESPACE>/<NAME>/<PROVIDER>",
for example: source = "app.terraform.io/example_corp/vpc/aws".source = "github.com/<ORGANIZATION>/<MODULE-FOLDER>"
for GitHub over HTTPS, e.g., source = "github.com/org/repo/", or
source = "git:github.com/<ORGANIZATION>/<MODULE-FOLDER>" for GitHub over SSH.git:: prefix followed by any valid Git URL, including the protocol.hg:: prefix followed by a valid Mercurial URL.bitbucket.org prefix.source = "https://example.com/module.zip".s3:: prefix followed by an S3 bucket object URL -
source = "s3::https://s3.amazonaws.com/bucket/module.zip".gcs:: prefix followed by a GCS bucket object URL.
Whenever a new module is added to a configuration, Terraform must install the module before it can be used. Both the
terraform get and terraform init commands will install and update modules. The
terraform init command will also initialize backends and install plugins.
When installing a remote module, Terraform will download it into the .terraform directory in your
configuration's root directory. Run terraform init after modifying the source argument
or the version argument for a remote module.
When installing a local module, Terraform will instead refer directly to the source directory. Because of this,
Terraform will automatically notice changes to local modules without having to re-run terraform init
or terraform get.
Terraform Registry
The public Terraform Registry makes it simple to find and use modules. It is integrated directly into Terraform, so a Terraform configuration can refer to any module published in the registry.
The registry supports module versioning, automatically generates documentation, provides version histories, shows examples and READMEs, and more. It extracts information about the module from the module's source (Git and GitHub). The module name, provider, documentation, inputs/outputs, and dependencies are all parsed and available via the UI or API, as well as the same information for any submodules or examples in the module's source repository.
Similarly to the root module, child modules will define and use variables. Module's variables are used to supply input parameters and often called module's input variables.
Comparing Terraform modules to function definitions in traditional programming languages:
Inputs:
variable blocks inside a module:module block:
Variables declared in modules that are not given a default value are required, and so must be set whenever
the module is called (vpc_name and cidr_block in the example). Variables with a default value
are optional and may be omitted in the module definition block (region in the example).
Outputs:
output blocks to selectively export certain
values to be accessed by the calling module:module.<MODULE_NAME>.<OUTPUT_NAME>, e.g., network = module.vpc.vpc_id.Variable scope rules:
module block.
Use the version argument in the module block to specify version requirements.
For example:
Version - one or more version constraint conditions separated by
commas: version = "<operator> <version>, ..."
= (or no operator): Exact version number. Cannot be combined with other conditions.
For example: = 3.5.0!=: Excludes an exact version number, e.g., != 3.5.0>, >=, <, <=: Compares to a specified version,
for example: >= 3.0>= 3.0, <= 3.5: Defines version range~>: Allows only the right-most version component to increment,
e.g., ~> 3.5 means >= 3.5.0 and < 4.01.2.0-beta.
Terraform does not match pre-release versions on >, >=, <, <=,
or ~> operators
Version constraints are supported only for modules installed from a module registry, such as the public
Terraform Registry or HCP Terraform's private module registry. Other module sources can provide their own
versioning mechanisms within the source string itself, or might not support versions at all. In particular,
modules sourced from local file paths do not support version.
Terraform will use the newest installed version of the module that meets the constraint. If no acceptable versions are installed, it will download the newest version that meets the constraint.
source = path_to_local_module and the directory does not exist?
locals block in terraform?
< Prev:
Objective 4 - Use Terraform outside the core workflow
Next >:
Objective 6 - Use the core Terraform workflow
More Terraform Tutorials:
Terraform Associate Exam Cram
Understanding Terraform Variable Precedence
Terraform Value Types Tutorial
Terraform count Explained with Practical Examples
Terraform for_each Tutorial with Practical Examples
Exploring Terraform dynamic Blocks with GCP Examples
Working with External Data in Terraform
Terraform Modules FAQ