Day 70 - Terraform Modules
What are Terraform modules?
Terraform modules are reusable, self-contained packages of Terraform configurations that encapsulate a specific set of resources or functionality. They enable you to organize and modularize your infrastructure code, promoting code reuse, maintainability, and scalability.
Why use Terraform modules?
Code Reusability: Modules allow you to write reusable infrastructure code that can be easily shared across projects or teams. By encapsulating common configurations and resources into modules, you can avoid duplication and promote consistency in your infrastructure deployments.
Abstraction and Encapsulation: Modules provide a level of abstraction by hiding the implementation details of resources behind well-defined inputs and outputs. This abstraction makes it easier to use and understand the module without diving into the implementation details.
Scalability: With modules, you can scale your infrastructure deployments efficiently. You can instantiate multiple instances of a module, each with its own configuration, to create multiple similar resources. This makes it easy to manage and deploy complex infrastructure setups without duplicating code.
Separation of Concerns: Modules enable you to separate different aspects of your infrastructure, such as networking, storage, and compute, into modular components. This separation helps in better organization and management of your infrastructure codebase.
Example:
Let's consider an example where you need to provision a VPC (Virtual Private Cloud) infrastructure in AWS. Instead of writing all the VPC-related configurations in a single Terraform file, you can create a reusable module for VPC.
The VPC module may include configurations for creating subnets, security groups, route tables, and other VPC-related resources. It encapsulates all the necessary details and provides well-defined inputs and outputs to interact with the module.
Using modules in Terraform:
To use a module in Terraform, you need to define a module block in your Terraform configuration file, providing the source of the module and any necessary input variables. You can then reference the module outputs to interact with the resources created by the module.
For example:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
cidr_block = "10.0.0.0/16"
subnets = {
public = ["10.0.1.0/24", "10.0.2.0/24"]
private = ["10.0.3.0/24", "10.0.4.0/24"]
}
# Other module inputs
}
resource "aws_instance" "app_server" {
# Reference module outputs
vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.subnet_ids["private"]
# Other instance configurations
}
In this example, the terraform-aws-modules/vpc/aws module is used to create a VPC infrastructure. The module is instantiated with the desired inputs, such as CIDR block and subnet configurations. The module outputs, such as VPC ID and subnet IDs, are then referenced in the aws_instance resource to associate the EC2 instance with the VPC.
- Difference between Root Module and Child Module.
Root Module: The root module is the top-level configuration file in your Terraform project. It serves as the entry point for Terraform to understand and execute your infrastructure code. The root module typically represents the main infrastructure stack or environment you are provisioning.
Child Module: A child module is a self-contained package of Terraform configurations that focuses on a specific aspect or resource within your infrastructure. It encapsulates a set of related resources and configurations that can be reused across different projects or environments.
Root Module Example:
main.tf (root module)
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}
provider "aws" {
region = "us-west-2"
}
module "vpc" {
source = "./modules/vpc"
vpc_name = "my-vpc"
cidr = "10.0.0.0/16"
}
module "ec2" {
source = "./modules/ec2"
instance_count = 2
instance_type = "t2.micro"
subnet_ids = module.vpc.subnet_ids
}
Child Module Example:
modules/vpc/main.tf (child module)
resource "aws_vpc" "main" {
cidr_block = var.cidr
tags = {
Name = var.vpc_name
}
}
resource "aws_subnet" "public" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.0.0/24"
}
resource "aws_subnet" "private" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
}
output "subnet_ids" {
value = [aws_subnet.public.id, aws_subnet.private.id]
}
- Are modules and Namespaces are same? Justify your answer for both Yes/No
No, modules and namespaces are not the same in Terraform.
Modules: In Terraform, modules are used to encapsulate reusable infrastructure components or configurations. They allow you to organize and abstract your infrastructure code into smaller, modular pieces, which can be shared across different projects or environments. Modules promote code reusability and help in maintaining consistent infrastructure patterns.
Namespaces: In Terraform, namespaces are used to differentiate resources that have the same name but exist in different environments or regions. Namespaces provide a mechanism to avoid naming conflicts and ensure resource isolation.
they serve different purposes:
Modules focus on code reuse and abstraction, allowing you to package and share infrastructure components.
Namespaces focus on creating distinct scopes for resources, enabling you to manage multiple instances of the same resource with different configurations or in different environments.
If you want explore all the module please check it out :
https://github.com/patelajay745/terraform
If you have any queries/suggestions please write in comment. See you another day with another challenge.



