Modules are containers for multiple resources that are used together. A module consists of a collection of .tf and/or .tf.json files kept together in a directory
A module can call other modules, which lets you include the child module's resources in the configuration concisely.
Modules can also be called multiple times, either within the same configuration or in separate configurations, allowing resource configurations to be packaged and re-used.
Below is the format on how to use modules:
# Creating a AWS EC2 Instance
resource "aws_instance" "server-instance" {
# Define number of instance
instance_count = var.number_of_instances
# Instance Configuration
ami = var.ami
instance_type = var.instance_type
subnet_id = var.subnet_id
vpc_security_group_ids = var.security_group
# Instance Tagsid
tags = {
Name = "${var.instance_name}"
}
}
# Server Module Variables
variable "number_of_instances" {
description = "Number of Instances to Create"
type = number
default = 1
}
variable "instance_name" {
description = "Instance Name"
}
variable "ami" {
description = "AMI ID"
default = "ami-xxxx"
}
variable "instance_type" {
description = "Instance Type"
}
variable "subnet_id" {
description = "Subnet ID"
}
variable "security_group" {
description = "Security Group"
type = list(any)
}
# Server Module Output
output "server_id" {
description = "Server ID"
value = aws_instance.server-instance.id
}
🔶 Task-01: Write about different modules of Terraform.
Terraform is a powerful Infrastructure as Code (IaC) tool used for provisioning and managing cloud infrastructure resources. To make infrastructure management more modular and maintainable, Terraform organizes its configurations into different modules. Modules in Terraform allow you to encapsulate and reuse parts of your infrastructure code, promoting code reusability and reducing redundancy. Here are the different modules of Terraform:
Root Module:
The root module is the top-level configuration that initiates and orchestrates the provisioning of resources.
It typically contains variables, provider configurations, and calls to child modules.
It's the entry point for running
terraform apply
orterraform destroy
.
Child Modules:
Child modules are reusable and encapsulated configurations that define a set of related resources.
They can be used within the root module or other child modules.
Child modules allow you to abstract complex infrastructure into smaller, manageable units.
Provider Modules:
Terraform supports multiple cloud providers like AWS, Azure, GCP, and others.
Each provider has its module for configuring authentication and provider-specific settings.
Provider modules enable you to define which cloud provider you want to work with your configuration.
Custom Modules:
Custom modules are user-defined modules created to encapsulate and reuse configurations for a specific set of resources.
They abstract away the complexity of configuring resources and promote code maintainability.
Data Modules:
Data modules are used for querying data from existing resources, such as AWS S3 buckets or EC2 instances, without making any changes.
They provide read-only access to resource attributes and are helpful for data-driven configurations.
Nested Modules:
Nested modules refer to the use of modules within other modules, creating a hierarchical structure.
They allow for building complex infrastructure by combining smaller, reusable components.
Community Modules:
Terraform has a vast community that shares pre-built modules for various infrastructure components.
These community modules are available in the Terraform Registry and can be easily integrated into your configurations.
Module Sources:
Modules can be sourced from various locations, including local directories, Git repositories, and the Terraform Registry.
Module sources provide flexibility in how you manage and share modules with your team.
🔶 Task-02: Difference between Root Module and Child Module.
In Terraform, the Root Module and Child Module are two fundamental concepts used to structure and organize your infrastructure as code (IaC) configurations. They serve distinct roles and have different characteristics:
Root Module:
Entry Point: The Root Module is the top-level configuration and serves as the entry point for your Terraform execution, typically the directory where your main
.tf
files are located.Orchestrator: It initiates and orchestrates the provisioning and management of resources. When you run
terraform apply
orterraform destroy
, Terraform starts with the Root Module.Contains Variables: The Root Module often contains input variables that can be set to customize the configuration's behavior, making it more flexible.
Provider Configuration: Provider configurations for cloud providers (e.g., AWS, Azure) are usually defined in the Root Module to specify the target infrastructure.
Dependencies: It can depend on child modules, data sources, and provider configurations.
State Management: The state file (usually named
terraform.tfstate
) for the entire configuration, including all resources and modules, is generated and managed at the Root Module level.
Child Module:
Reusability: Child Modules are reusable and encapsulated configurations that define a set of related resources. They abstract the complexity of defining resources and promote code reusability.
Encapsulation: They encapsulate a specific set of resources and their associated configurations, making your code more organized and modular.
Hierarchical Structure: Child Modules can be used within the Root Module or other Child Modules, creating a hierarchical structure of modules.
Promote Maintainability: By breaking down complex infrastructure into smaller, manageable units, Child Modules promote code maintainability and readability.
Separation of Concerns: Each Child Module can have its own set of input variables, allowing users of the module to customize its behavior while keeping the module itself focused on a specific resource or set of resources.
Resource Abstraction: They abstract the details of resource provisioning, making it easier to manage and update resources independently.
In essence, the Root Module serves as the orchestrator and entry point for your Terraform configurations, while Child Modules are reusable components that encapsulate specific sets of resources and their configurations. This modular approach in Terraform helps in structuring, organizing, and reusing infrastructure code effectively, especially in complex environments.
🔶 Task-03: Are modules and Namespaces are same? Justify your answer for both Yes/No
No, modules and namespaces are not the same, and here's why:
No: Modules and namespaces serve different purposes and have distinct characteristics:
Modules:
Modules in Terraform are a way to encapsulate and reuse a set of related resources and configurations.
They promote modularity, code reuse, and maintainability by allowing you to define and manage infrastructure components separately.
Modules are typically used to abstract and package resource configurations for reuse in different parts of your infrastructure.
Namespaces:
Namespaces are used to organize and control the scope and visibility of identifiers (such as variables, functions, or objects) in programming and computing in general.
Namespaces help prevent naming conflicts by providing a way to isolate and group identifiers.
In some programming languages and systems, namespaces might also be used to structure code hierarchically.
Yes: In some contexts, you might use the term "namespace" informally to refer to Terraform modules' ability to provide a level of encapsulation and isolation. Modules allow you to create separate namespaces for different sets of resources and configurations within your Terraform code. This can help prevent naming conflicts between resources and variables used in different modules.
However, it's important to note that this informal use of the term "namespace" doesn't encompass the full range of capabilities and features typically associated with namespaces in programming languages or systems. Terraform modules provide encapsulation and organization but don't offer the same level of isolation as traditional namespaces in programming languages or operating systems.
In summary, while you might use the term "namespace" informally to describe how Terraform modules provide encapsulation and isolation, modules and namespaces are fundamentally different concepts with distinct purposes and characteristics.
Happy Learning :)
Stay in the loop with my latest insights and articles on cloud ☁️ and DevOps ♾️ by following me on Hashnode, LinkedIn (https://www.linkedin.com/in/chandreshpatle28/), and GitHub (https://github.com/Chandreshpatle28).
Thank you for reading! Your support means the world to me. Let's keep learning, growing, and making a positive impact in the tech world together.
#Git #Linux Devops #Devopscommunity #90daysofdevopschallenge #python #docker #Jenkins #Kubernetes #Terraform #AWS