Terraform Modules

Terraform Modules

What are Terraform Modules?

A Terraform module is a reusable and encapsulated set of Terraform configurations that represent a piece of infrastructure. Think of modules as building blocks for your infrastructure, each serving a specific purpose or representing a particular component. This modular approach enhances code organization, reuse, and maintainability.

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.


Why Use Modules?

1. Reusability: Modules promote code reuse. Once you've defined a module, you can use it across multiple projects or environments, avoiding duplication and ensuring consistency.

2. Abstraction: Modules allow you to abstract complex infrastructure details into a single, easy-to-understand component. This abstraction simplifies your main Terraform configuration, making it more manageable.

3. Maintainability: As your infrastructure grows, maintaining a single, monolithic Terraform configuration becomes challenging. Modules facilitate a modular and organized structure, making it easier to understand, modify, and extend your codebase.

4. Collaboration: Modules foster collaboration among team members. When each module represents a specific functionality, team members can work on different modules simultaneously without stepping on each other's toes.


Anatomy of a Module

A Terraform module typically consists of the following elements:

  1. Variables: Parameters that allow users to customize the module based on their requirements.

  2. Resources: The actual infrastructure resources defined by the module. These can include instances, networks, databases, or any other supported resource.

  3. Outputs: Information exposed by the module that can be used by other parts of your Terraform configuration.

Here's a simple example of a Terraform module that creates an AWS EC2 Instnace:

Creating a Module

We created a modules directory in the last video, and now we'll create a new one named ec2.

A module usually contains at least 3 files:

  1. main.tf, which defines resources to create

  2. variables.tf, which defines variables the module expects to be given values for

  3. outputs.tf, which defines what data the modules provides for other terraform configuration to read

We'll see what that all means going forward.

We create those files for the ec2 server:

mkdir -p modules/ec2_instance
cd modules/ec2_instance
touch main.tf outputs.tf variables.tf

modules/ec2_instance/main.tf

provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "example" {
    ami = var.ami_value
    instance_type = var.instance_type_value
    subnet_id = var.subnet_id_value
    tags = {
    Name = "${var.instance_name}"
  }
}

modules/ec2_instance/resource.tf

variable "ami_value" {
    description = "value for the ami"
}

variable "instance_type_value" {
    description = "value for instance_type"
}

variable "subnet_id_value" {
    description = "value for the subnet_id"
}

variable "instance_name" {
  description = "Instance Name"
  default     = "Instance-From-Terraform-Module"
}

modules/ec2_instance/outputs.tf

output "public-ip-address" {
  value = aws_instance.example.public_ip
}

home/ubuntu/main.tf

provider "aws" {
  region = "us-east-1"
}

module "ec2_instance" {
  source = "./modules/ec2_instance"
  ami_value = "ami-0fc5d935ebf8bc3bc" 
  instance_type_value = "t2.micro"
  subnet_id_value = "subnet-03a5c793b3a9dbd13"
}

Run terraform init to initialize the Terraform project.

terraform validate

terraform plan

terraform apply - Run terraform apply to create instance

Instance Created From the EC2 Module


Write about different modules of Terraform.

Modules in Terraform serve as building blocks for your infrastructure code, allowing you to encapsulate and abstract infrastructure components. They promote code reusability, maintainability, and scalability. Let's explore some of the different types of modules commonly used in Terraform:

  1. Root Modules: A root module is a top-level module in a Terraform configuration. It typically represents an entire infrastructure or a major component of it. Root modules define and manage resources directly, and they may also call child modules to encapsulate specific functionality.

  2. Child Modules: Child modules are reusable modules that encapsulate a specific set of resources or functionality. They can be used within root modules or other child modules, enabling composition and modularity. Child modules provide a way to organize and abstract infrastructure components, making the configuration more manageable.

Published Modules: Publishing a module in Terraform refers to making it available for use by other users within the Terraform ecosystem. When you publish a module, others can easily consume and reuse it in their own infrastructure configurations.


Difference between Root Module and Child Module.

  • The root module represents the top-level configuration that directly manages resources and integrates different modules, while child modules encapsulate specific functionality or components, promoting reusability and modularization.

  • The root module is instantiated and invoked as the primary configuration, while child modules are called and included within the root module to compose the overall infrastructure configuration.

The root module can include and call child modules to encapsulate and modularize specific functionality or components. It integrates and composes different modules to construct the overall infrastructure configuration. Child modules are usually called and included within root modules or other child modules. They enable composition and modularity by allowing you to organize and abstract infrastructure components.


Are modules and Namespaces are same? Justify your answer for both Yes/No

No, modules and namespaces are not the same, although they serve different purposes within different contexts.

No, they are not the same:

  1. Purpose: Modules in Terraform are used to encapsulate and organize infrastructure configurations and resources, promoting reusability and modularity. They provide a way to package and abstract infrastructure components for easy composition and management. Modules allow you to define, configure, and deploy infrastructure resources across various cloud providers.

  2. Usage: Modules are invoked and used within Terraform configurations to create instances of reusable infrastructure components. They define input variables and output values, allowing users to customize the behavior of the module and consume its outputs.

On the other hand:

Yes, they can be considered similar in certain contexts:

  1. Namespace-like behaviour: When using modules within Terraform, there is a level of namespace-like behaviour that allows for logical separation and organization of resources. Each module can have its own set of input variables and output values, providing a degree of isolation and encapsulation.

  2. Reusability and abstraction: Both modules and namespaces facilitate reusability and abstraction. Namespaces help prevent naming conflicts by providing a unique context or scope for identifiers, while modules enable code reuse and encapsulation of infrastructure components.


Thanks for spending your valuable time in learning to enhance your career!๐Ÿ˜ƒ๐Ÿ™


Follow me on

Hashnode: kshitijaa.hashnode.dev

LinkedIn: linkedin.com/in/kshitija-bartakke-malwade-3..

ย