Day 70 Task: Terraform Modules

Day 70 Task: Terraform Modules

ยท

6 min read

  • 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:

  1. 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 or terraform destroy.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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:

  1. 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.

  2. Orchestrator: It initiates and orchestrates the provisioning and management of resources. When you run terraform apply or terraform destroy, Terraform starts with the Root Module.

  3. Contains Variables: The Root Module often contains input variables that can be set to customize the configuration's behavior, making it more flexible.

  4. Provider Configuration: Provider configurations for cloud providers (e.g., AWS, Azure) are usually defined in the Root Module to specify the target infrastructure.

  5. Dependencies: It can depend on child modules, data sources, and provider configurations.

  6. 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:

  1. 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.

  2. Encapsulation: They encapsulate a specific set of resources and their associated configurations, making your code more organized and modular.

  3. Hierarchical Structure: Child Modules can be used within the Root Module or other Child Modules, creating a hierarchical structure of modules.

  4. Promote Maintainability: By breaking down complex infrastructure into smaller, manageable units, Child Modules promote code maintainability and readability.

  5. 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.

  6. 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:

  1. 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.

  2. 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 :)

If you find my blog valuable, I invite you to like, share, and join the discussion. Your feedback is immensely cherished as it fuels continuous improvement. Let's embark on this transformative DevOps adventure together! ๐Ÿš€ #devops #90daysofdevop #AWS

ย