Terraform Modules

Terraform Modules

Day 70 of #90DaysOfDevOps

Tasks:

  • Write about different modules Terraform.

  • Difference between Root Module and Child Module.

  • Is modules and Namespaces are same? Justify your answer for both Yes/No.

  • Create an aws instance module.

Different modules Terraform:

Terraform has three main types of modules:

  • Root module: Initializes Terraform, configures providers, and declares resources.

  • Child module: A sub-module that is nested within the root module or another child module. It groups related resources and configurations together for easier management.

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

A Terraform module 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.

Modules can be loaded from the local filesystem, or a remote source. Terraform supports a variety of remote sources, including the Terraform Registry, most version control systems, HTTP URLs, and Terraform Cloud or Terraform Enterprise private module registries.

A module is the smallest versioned and shareable unit. It has an exact list of arguments, implement basic logic for such a unit to do the required function.

Difference between Root Module and Child Module:

In Terraform, a root module is responsible for initializing Terraform, configuring providers, and declaring resources.

Whereas a child module is a sub-module that is nested within the root module or another child module.

  • Root modules

    Describe the architecture to be deployed. They can contain many child modules or none at all. Root modules are the most opinionated.

  • Child modules

    Are reusable modules that are invoked in root modules. They group related resources and configurations together for easier management. Child modules act as a template for AWS infrastructure to ensure uniformity across AWS accounts.

Is modules and Namespaces are same?

No.

Namespace is the name of a namespace, unique on a particular hostname, that can contain one or more modules that are somehow related. On the public Terraform Registry the "namespace" represents the organization that is packaging and distributing the module.

Create a server instance with terraform module:

You can create multiple files to create modules, so that it looks clean code.

variables.tf

# Server Module Variables

variable "my_env" {
  description = "Define which environment you are working on"
  type        = string
}

variable "region" {
  description = "Define region"
  type        = string
}

variable "instance_count" {
  description = "Number of instances to create"
  type        = number
  default     = 1
}

variable "ami" {
  description = "EC2 AMI"
  type        = string
}

variable "instance_name" {
  description = "Name of the instance"
}

variable "instance_type" {
  description = "EC2 Instance Type"
  type        = string
}

variable "keyName" {
  description = "EC2 Key Pair Name"
  type        = string
}

variable "subnet_id" {
  description = "Subnet ID"
}

variable "security_group" {
  description = "security group"
  type        = list(any)
}

my_server.tf

# Creating a AWS EC2 Instance
resource "aws_instance" "server-instance" {
  # Define number of instance
  count = var.instance_count

  # Instance Configuration
  ami                    = var.ami
  instance_type          = var.instance_type
  key_name               = var.keyName
  subnet_id              = var.subnet_id
  vpc_security_group_ids = var.instance_name

  # Instance Tagsid
  tags = {
    Name = "${var.my_env}-${var.instance_name}"
  }
}

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

The template is ready to call from module block.

module "servers" {
  source = "./my_server"
  my_env = "dev"
  region = "us-east-1"
  instance_count = 5
  ami = "ami-xxx"
  instance_name = "web-server"
  instance_type = "t2.micro"
  keyName = "my-linux-key"
}

Thank you for reading!