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 into the configuration in a concise way.
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.
Modules in Terraform:
1. Modules:
Modules in Terraform are self-contained, reusable components of infrastructure configurations. They can consist of one or more resources and are defined in separate directories.
Modules allow you to abstract and encapsulate specific infrastructure components, such as a web server, a database, a VPC, or any other logical grouping of resources.
Modules can be reused in multiple parts of your Terraform configuration, promoting consistency and reducing code duplication.
Modules accept input variables and produce output values, making them customizable and composable.
2. Root Module:
The root module is the entry point of your Terraform configuration. It's essentially the main configuration file (typically named
main.tf
) that sets up your Terraform project.The root module often defines providers, declares variables, and calls child modules.
It represents the top-level execution context where you configure your infrastructure, set up provider configurations, and orchestrate the use of various child modules.
3. Child Modules:
Child modules are self-contained configurations defined in separate directories within your project.
They encapsulate specific pieces of infrastructure, such as a database cluster or a load balancer.
Child modules are called from the root module and accept input variables that customize their behavior.
They promote reusability, maintainability, and separation of concerns in your Terraform configuration.
Difference between Root Module and Child Module:
The key differences between the root module and child modules are:
Root Module:
The root module is the top-level configuration file.
It is the entry point for your Terraform project.
The root module orchestrates the use of child modules.
It typically sets up provider configurations, defines variables, and calls child modules.
Child Modules:
Child modules are self-contained configurations.
They are defined in separate directories.
Child modules encapsulate specific infrastructure components.
They can be called multiple times from the root module and from other modules, promoting reusability.
Are Modules and Namespaces the Same?
No, modules and namespaces are not the same in Terraform.
Modules:
As described above, modules are a way to encapsulate and reuse configurations.
Modules help you structure your code and promote code organization, reusability, and modularity.
Modules are designed to create a logical separation of resources.
Namespaces:
In Terraform, a namespace typically refers to a named scope within a particular configuration file or module. It's used to group and organize resources and variables.
Namespaces are a way to avoid naming conflicts by scoping identifiers.
They don't necessarily represent a reusable, self-contained unit of infrastructure.
In summary, modules and namespaces serve different purposes in Terraform. Modules are a way to structure and organize your code for reuse, while namespaces help organize resources and variables within a particular scope to avoid naming conflicts.
I'm confident that this article will prove to be valuable, helping you discover new insights and learn something enriching.
Thank you :)