Skip to main content

Command Palette

Search for a command to run...

Day 70: Unveiling Terraform Modules: A Structural Approach to Infrastructure as Code❄🚀

Day#70 Of 90 Days Of DevOps Challenge

Published
4 min read
Day 70: Unveiling Terraform Modules: A Structural Approach to Infrastructure as Code❄🚀
V

Experienced QA professional with a passion for manual and automation testing. Proficient in DevOps practices, ensuring seamless integration and continuous delivery. Dedicated to ensuring top-notch software quality and efficiency. Eager to contribute my skills to Hashnode's vibrant tech community. Let's collaborate and create exceptional experiences!

👋Introduction

Welcome to Day 70 of our Terraform expedition.

In this blog post, we will delve into the intricate world of Terraform modules.

We will explore the concept of modules, and their role in structuring infrastructure as code, differentiate between the primary root module and child modules, and address a common query: Are modules and namespaces interchangeable?

Let's embark on this journey.

💻Understanding Terraform Modules

Terraform modules can be likened to the fundamental building blocks of infrastructure configuration.

They offer a systematic way to encapsulate a set of interrelated resources, variables, and outputs into a reusable package.

This modular approach enables you to maintain a well-organized and scalable infrastructure codebase.

A Terraform module typically constitutes a directory that houses one or more .tf or .tf.json files.

These files define the specific resources, variables, and outputs relevant to the module's particular functionality.

The versatility of modules extends from simple single-resource modules to intricate modules encompassing a blend of resources, data sources, and variables.

📑The Distinction: Root Module vs. Child Module

📌The Root Module

The root module serves as the gateway to your Terraform configuration.

It stands as the pinnacle configuration where you initiate commands like terraform apply or terraform plan.

Typically, the root module symbolizes your entire infrastructure or a substantial part of it.

In the root module, you are tasked with defining variables, data sources, and resource blocks responsible for the creation and management of your infrastructure.

Exemplifying the Root Module:

# Creating an AWS EC2 Instance in the root module
resource "aws_instance" "server-instance" {
  # ... (Instance configuration)
}

# Root Module Variables and Outputs
variable "number_of_instances" {
  # ...
}

output "server_id" {
  # ...
}

📌The Child Module

Child modules, in contrast, are reusable components that you can incorporate within your root module.

These modules reside in separate directories and are accessible from the root module.

This modular design promotes reusability, ensuring you refrain from redundant code repetition across diverse projects or configurations.

Illustrating a Child Module (aws_instance):

# Creating an AWS EC2 Instance in a child module
resource "aws_instance" "server-instance" {
  # ... (Instance configuration)
}

# Child Module Variables and Outputs
variable "number_of_instances" {
  # ...
}

output "server_id" {
  # ...
}

By crafting a child module for the AWS EC2 instance, you can conveniently reuse this module in various configurations, fostering a more systematic and efficient codebase.

📚Modules vs. Namespaces: A Distinctive Pair

No, modules and namespaces are not synonymous. Let's elucidate the differentiation between these two concepts:

✔Modules

Modules chiefly concern the organization and structuring of your Terraform code.

They empower you to break down your infrastructure into manageable components, thereby easing the process of maintenance and scalability.

Modules are the bedrock for code reusability and modularity.

✔Namespaces

Namespaces, on the other hand, pertain to how resources receive their names within Terraform.

A namespace essentially serves as a label or prefix that you can attach to resource names.

This becomes particularly invaluable when multiple instances of the same resource type coexist in your configuration.

It ensures each resource possesses a distinctive and meaningful identifier.

For instance, within your AWS EC2 instance resource definition, the Name tag is configured as ${var.instance_name}, with instance_name representing a variable. This naming approach guarantees that each instance is endowed with a unique identifier, constituting a form of namespace.

In summation, modules center around code organization, while namespaces focus on ensuring resource names remain distinct and meaningful.

Both play diverse roles in your Terraform configuration.

💥Conclusion

Terraform modules are a potent tool for organizing and reusing infrastructure code.

Grasping the distinction between root and child modules is imperative for harnessing the full potential of modules.

Additionally, modules and namespaces are distinct concepts within Terraform, each bearing a unique role in your infrastructure code.

To get hands-on experience with modules, you can utilize the following Terraform CLI commands:

  • For initializing a Terraform configuration (typically in your root module directory):

      terraform init
    
  • For generating an execution plan for your configuration:

      terraform plan
    
  • For applying your Terraform configuration (usually in your root module directory):

      terraform apply
    

These commands will enable you to put your module knowledge into practical use. Happy Terraforming!

Happy learning & may your DevOps journey be filled with exciting discoveries🎊

Thank you for joining us on this exciting Day 70 of the 90 Days of DevOps challenge. I hope you found the information helpful and insightful.💥

So please keep yourself updated with my latest insights and articles on DevOps 🚀 by following me on :

Hashnode: vishaltoyou.hashnode.dev

LinkedIn: linkedin.com/in/vishalphadnis

Terraform Modules