Day 70 of #90daysofdevops
Hey Techies! Welcome to this blog
In this blog, we are going to start with Terraform Modules.
What Is a Terraform Module?
In Terraform, Modules are groups of .tf files that are kept in a different directory from the configuration as a whole. A module’s scope encompasses all of its resources. So, if the user needs information about the resources that a module creates, the module must be explicitly stated. To do this, declare an output on the module that exposes the necessary data and permits references to that output from outside the module.
A typical module would resemble this:
.
├── LICENSE
├── README.md
├── main.tf
├── variables.tf
├── outputs.tf
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
}
Types of Terraform Modules
With Terraform, you are most likely to come across one of three main categories of modules:
Root Module
Child Module
Published Modules
Modules – Local and Remote
Both local and distant sources can be used to load modules. The majority of version control systems, HTTP URLs, the Terraform Registry, and private module registries in Terraform Cloud or Terraform Enterprise are just a few of the external sources that Terraform supports.
What does a module do?
By building logical abstraction on top of a resource set, you may use modules in terraform. To put it another way, a module enables the grouping of resources for subsequent reuse—possibly repeatedly.
Assume we have a cloud-hosted virtual server with various functionalities. Which group of resources would best sum up that server? For instance:
A virtual machine built from a picture
A block device with a connected storage space that is a specific size.
A public static IP address that is assigned to the server’s virtual network interface
A set of firewall guidelines that should be attached to the server
Other factors, such as an extra network interface or block device.
Let’s now imagine that you will frequently need to establish this server with a certain set of resources. You don’t want to keep repeating the same setup code, do you? That’s where modules come in incredibly handy.
Why Modules?
Organise Configuration: Easier to navigate, understand, and update your configuration
Encapsulate Configuration: Helps prevent unintended consequences
Re-use Configuration: Share modules that you have written with your team or the general public
Consistency: help to provide consistency in your configurations
Module: Input
Serve as parameters for a module in terraform
Input variables are the API of the Modules
For variables in child modules, the calling module should pass values in the module block.
Each input variable accepted by a child module must be declared using a variable block
the variable declaration can also include a default argument
resource "aws_instance" "appserver" {
ami = var.ami
instance_type = "t2.medium"
tags = {
name = "${var.app_region}-app-server"
}
}
module "primaryapp" {
source = "./modules/appstorage"
app_region = "us-east-2"
ami = "ami-0010d386b82bc06f0"
}
Module: Output
The module can also return values
access module output variables using the module.<MODULE_NAME>.<OUTPUT_NAME>
Description argument is used to inform the module user what is contained in the output
value argument takes an expression whose result is to be returned to the user
output "subnetid" {
value = "aws_instnace.appserver.subnet_id"
}
Task
- Write about different modules Terraform.
The Root Module
Every Terraform configuration has at least one module, known as its root module, which consists of the resources defined in the .tf
files in the main working directory.
Child Modules
A Terraform module (usually the root module of a configuration) can call other modules to include their resources into the configuration. A module that has been called by another module is often referred to as a child module.
Child modules can be called multiple times within the same configuration, and multiple configurations can use the same child module.
Published Modules
In addition to modules from the local filesystem, Terraform can load modules from a public or private registry. This makes it possible to publish modules for others to use, and to use modules that others have published.
The Terraform Registry hosts a broad collection of publicly available Terraform modules for configuring many kinds of common infrastructure. These modules are free to use, and Terraform can download them automatically if you specify the appropriate source and version in a module call block.
Also, members of your organization might produce modules specifically crafted for your own infrastructure needs. Terraform Cloud and Terraform Enterprise both include a private module registry for sharing modules internally within your organization.
For more visit Terraform Modules.
- Difference between Root Module and Child Module.
Understanding the distinction between root and child modules is crucial for effective Terraform project organization.
Root Module:
Role: Defines the top-level configuration.
Scope: Encompasses the entire Terraform project.
Use Cases: Specifies providers, sets global configurations, and orchestrates the use of child modules.
Example: Defines the overall infrastructure, including networks, security groups, and compute resources.
Child Module:
Role: Represents a modular unit within the root module.
Scope: Focuses on specific resources or functionalities.
Use Cases: Encapsulates resources like databases, virtual machines, or entire application stacks.
Example: Manages a specific component, such as a database cluster or a load balancer.
- Is modules and Namespaces are same? Justify your answer for both Yes/No
The concepts of modules and namespaces are related but distinct in Terraform.
Modules:
Definition: Reusable units of infrastructure.
Role: Encapsulates resources and configurations.
Use Cases: Promotes code organization and reusability.
Namespaces:
Definition: A way to organize and group resources.
Role: Defines a scope for resource names.
Use Cases: Avoids naming conflicts and organizes resources within a specific context.
Justification: While both modules and namespaces contribute to organizing infrastructure code, they serve different purposes. Modules focus on code structure and reusability, while namespaces are more about avoiding naming conflicts within a specific context. In essence, modules provide a structural approach to managing code, while namespaces provide a way to avoid naming collisions.
Thank you so much for taking the time to read till the end! Hope you found this blog informative and helpful.
Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.
Happy Learning!
~kritika :)
Connect with me: LinkedIn