Terraform Modules Tutorial Flashcards
Modules help organize configuration
Modules make it easier to navigate, understand, and update your configuration by keeping related parts of your configuration together.
Modules help encapsulate configuration
Encapsulation can help prevent unintended consequences, such as a change to one part of your configuration accidentally causing changes to other infrastructure, and reduce the chances of simple errors like using the same name for two different resources.
Modules help you reuse configuration
Using modules can save time and reduce costly errors by re-using configuration written either by yourself, other members of your team, or other Terraform practitioners who have published modules for you to use.
What is a Terraform module
a set of Terraform configuration files in a single directory.
a simple configuration consisting of a single directory with one or more .tf files is a module.
what is a root module
When you run Terraform commands directly from a directory, it is considered the root module
calling modules
your configuration can use module blocks to call modules in other directories. When Terraform encounters a module block, it loads and processes that module’s configuration files.
sometimes referred to as a “child module” of that configuration.
Module best practices - naming
Name your provider terraform-PROVIDER-NAME
You must follow this convention in order to publish to the Terraform Cloud or Terraform Enterprise module registries.
Modules Best Practice - always think modules
Start writing your configuration with modules in mind.
Modules best practice - encapsulation and organization
organizing your configuration in terms of modules from the beginning will significantly reduce the burden of maintaining and updating your configuration as your infrastructure grows in complexity.
When building a module consider 3 things (best practices)
Encapsulation: Group infra that is always deployed together
Privileges: Restrict modules to privilege boundaries. Infra in module should belong to one group.
Volatility: Separate long-lived infrastructure from short-lived.
Guidelines for MVP (Minimum Viable Product)
Target 80% of use cases.
Forget the edge cases.
Avoid conditional expressions. An MVP should have a narrow focus
Minimize the number of variables.
Maximize the number of outputs.
Nesting Modules
A nested module is a reference to invoke another module from the current module.
Where do you find and explore terraform Modules?
The Terraform Registry makes it simple to find and use modules.
What is the syntax for referencing a registry module?
NAMESPACE/NAME/PROVIDER // for example module "consul" { source = "hashicorp/consul/aws" version = "0.1.0" }
What is the syntax for referencing a private registry module?
HOSTNAME/NAMESPACE/NAME/PROVIDER // for example module "vpc" { source = "app.terraform.io/example_corp/vpc/aws" version = "0.9.3" }