Day 70 - Terraform Modules

Day 70 - Terraform Modules

  1. What are terraform modules?.

  2. Different type of terraform modules?.

  3. Difference between root module and child module?.

  4. Is modules and Namespaces are same? Justify your answer for both Yes/No .

As you manage your infrastructure with Terraform, you will create increasingly complex configurations. There is no intrinsic limit to the complexity of a single Terraform configuration file or directory, so it is possible to continue writing and updating your configuration files in a single directory. However, if you do, you may encounter one or more problems:

  • Understanding and navigating the configuration files will become increasingly difficult.

  • Updating the configuration will become more risky, as an update to one section may cause unintended consequences to other parts of your configuration.

  • There will be an increasing amount of duplication of similar blocks of configuration, for instance when configuring separate dev/staging/production environments, which will cause an increasing burden when updating those parts of your configuration.

  • You may wish to share parts of your configuration between projects and teams, and will quickly find that cutting and pasting blocks of configuration between projects is error prone and hard to maintain.

  • Engineers will need more Terraform expertise to understand and modify your configuration. This makes self-service workflows for other teams more difficult, slowing down their development.

1.What Are Terraform Modules?

A Terraform module is a set of Terraform configuration files in a single directory. Even a simple configuration consisting of a single directory with one or more .tf files is a module. When you run Terraform commands directly from such a directory, it is considered the root module. So in this sense, every Terraform configuration is part of a module. You may have a simple set of Terraform configuration files such as:

.
├── LICENSE
├── README.md
├── main.tf
├── variables.tf
├── outputs.tf

In this case, when you run terraform commands from within the minimal-module directory, the contents of that directory are considered the root module.

Here are some of the ways that modules help solve the problems listed above:

  • Organize configuration - Modules make it easier to navigate, understand, and update your configuration by keeping related parts of your configuration together. Even moderately complex infrastructure can require hundreds or thousands of lines of configuration to implement. By using modules, you can organize your configuration into logical components.

  • Encapsulate configuration - Another benefit of using modules is to encapsulate configuration into distinct logical components. Reduce the chances of simple errors like using the same name for two different resources.

  • Re-use configuration - Writing all of your configuration from scratch can be time consuming and error prone. 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. You can also share modules that you have written with your team or the general public, giving them the benefit of your hard work.

  • Provide consistency and ensure best practices - Modules also help to provide consistency in your configurations. Not only does consistency make complex configurations easier to understand, it also helps to ensure that best practices are applied across all of your configuration. For instance, cloud providers give many options for configuring object storage services, such as Amazon S3 or Google Cloud Storage buckets.

  • Self service - Modules make your configuration easier for other teams to use. The Terraform Cloud registry lets other teams find and re-use your published and approved Terraform modules.

2.Different type of terraform modules?

The Root Module

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.

Child Modules

A Terraform module (usually the root module of a configuration) can call other modules to include their resources into the configuration. A module that has been called by another module is often referred to as a child module.

Child modules can be called multiple times within the same configuration, and multiple configurations can use the same child module.

Published Modules

In addition to modules from the local filesystem, Terraform can load modules from a public or private registry. This makes it possible to publish modules for others to use, and to use modules that others have published.

The Terraform Registry hosts a broad collection of publicly available Terraform modules for configuring many kinds of common infrastructure. These modules are free to use, and Terraform can download them automatically if you specify the appropriate source and version in a module call block.

Also, members of your organization might produce modules specifically crafted for your own infrastructure needs. Terraform Cloud and Terraform Enterprise both include a private module registry for sharing modules internally within your organization.

3.Difference between root module and child module?.

Here are some differences between root modules and child modules:

  • Visibility of variables: A root module can declare and reference variables that are visible to all child modules. A child module can only declare and reference variables that are visible within that module.

  • Interaction with child module's contents: The root module can only interact with the child module's contents through the input variables and output values defined by the child module.

  • Deployment: The root module is the one that is actually deployed when the terraform apply command is run.

  • Number of child modules: A root module may contain many child modules or none at all.

  • Details for the child module: The root module contains specific details for the child module.

4.Is modules and Namespaces are same? Justify your answer for both Yes/No .

No, modules and namespaces are not the same concept in Terraform. However, they can share some similarities in certain contexts.

Here's some related information about modules and namespaces in Terraform:

  • Modules

    A self-contained unit of infrastructure code that encapsulates a set of related resources. Modules are used to organize and reuse infrastructure code.

  • Namespaces

    Used to organize and avoid naming conflicts between different identifiers in programming. Workspaces provide namespaces for resource creation with dedicated states.

Both modules and namespaces are used for organization and abstraction.

Conclusion:

Terraform modules are essential for building scalable, maintainable, and reusable infrastructure configurations. They offer several benefits, including code reuse, modularity, and improved collaboration among teams. By encapsulating infrastructure components and configurations into self-contained units, modules help streamline the provisioning and management of cloud resources across diverse environments and use cases.