Day 70 - Terraform Modules   (Feb 4, 2024)

Day 70 - Terraform Modules (Feb 4, 2024)

🙏 Introduction:

In this blog, we will learn about Terraform Modules.

🔶What is Modules in Terraform ?

  • 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}"
  }
}

The code provided is an example of a Terraform configuration file that creates an EC2 instance in AWS using the aws_instance resource. Here is a brief description of the different sections in this code:

  • resource "aws_instance" "server-instance": This line declares a new resource of type aws_instance with the name "server-instance".

  • instance_count = var.number_of_instances: This line sets the number of instances to create based on the value of the number_of_instances variable which will be defined in Server Module Variables section.

  • ami = var.ami: This line sets the ID of the Amazon Machine Image (AMI) to use for the instance based on the value of the ami variable.

  • instance_type = var.instance_type: This line sets the instance type of the instance based on the value of the instance_type variable.

  • subnet_id = var.subnet_id: This line sets the ID of the subnet where the instance will be launched based on the value of the subnet_id variable.

  • vpc_security_group_ids = var.security_group: This line sets the security group(s) that will be associated with the instance based on the value of the security_group variable.

  • tags = { Name = "${var.instance_name}" }: This line sets a tag with the key "Name" and the value of the instance_name variable on the instance.

Server Module Variables

# 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)
}

The code provided is an example of a Terraform configuration file that defines several variables that can be used to configure the creation of EC2 instances in AWS. Here is a brief description of each variable:

  • number_of_instances: This variable defines the number of EC2 instances to create. It has a default value of 1 if no value is provided.

  • instance_name: This variable is used to set the name of the EC2 instance. It does not have a default value, meaning that the user must provide a value for this variable.

  • ami: This variable is used to set the ID of the Amazon Machine Image (AMI) to use for the EC2 instance. It has a default value of "ami-xxxx" if no value is provided.

  • instance_type: This variable is used to set the instance type of the EC2 instance. It does not have a default value, meaning that the user must provide a value for this variable.

  • subnet_id: This variable is used to set the ID of the subnet where the EC2 instance will be launched. It does not have a default value, meaning that the user must provide a value for this variable.

  • security_group: This variable is used to set the security group(s) that will be associated with the EC2 instance. It has a type of list(any) to allow the user to specify one or more security groups. It does not have a default value, meaning that the user must provide a value for this variable.

Server module Output:

# Server Module Output
output "server_id" {
  description = "Server ID"
  value       = aws_instance.server-instance.id
}

The code provided is an example of a Terraform configuration file that creates an EC2 instance in AWS using the aws_instance resource and outputs its ID using the output section. Here is a brief description of the different sections in this code:

  • output "server_id": This line declares a new output named "server_id".

  • value = aws_instance.server-instance.id: This line sets the value of the output to the ID of the EC2 instance created earlier with the aws_instance resource. The format aws_instance.server-instance.id refers to the attribute id of the resource aws_instance named "server-instance".

🎯Task: 1

Write about different modules Terraform

Terraform is a popular open-source infrastructure as code (IAC) tool that allows you to manage and provision cloud resources across multiple cloud providers. One of the key features of Terraform is its modular architecture, which allows you to break down your infrastructure code into smaller, reusable components called modules. This makes it easier to manage complex infrastructures and promote code reuse.

There are different types of modules in Terraform, including:

  • Root modules: The root module is the top-level module that serves as the entry point for your entire Terraform configuration. It typically includes your provider configuration and any variables that are needed to define your infrastructure resources.

  • Child modules: A child module is a sub-module that is nested within the root module or another child module. It can be used to group related resources and configurations together for easier management.

  • Published Modules: A published module in Terraform refers to a module that has been shared with the community through the Terraform Module Registry. The Terraform Module Registry is a public repository where users can share their modules with others to promote code reuse and collaboration..

Each module contains a set of resources and configurations that can be managed independently. This makes it easier to manage and test your infrastructure code, and promotes code reuse across your organization. You can create your own custom modules, or use existing modules from the Terraform Module Registry, which is a public repository of Terraform modules contributed by the community

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

Modules

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

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

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.

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.