#90daysofdevops-Day-70: Terraform Modules

#90daysofdevops-Day-70: Terraform Modules

ยท

5 min read

  • 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

Explain the below in your own words and it shouldnt be copied from Internet ๐Ÿ˜‰

  • 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

Modules

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

  1. Module for creating EC2 instance

    This module contains instances_count which denotes number of instances to be created.

    ami denotes the Amazon machine image of OS to be set for the newly created instances.

    instance_type denotes the shape of the new instance that will contain CPU and memory.

    vpc_security_group_ids denotes the ingress and egress rules to be allowed for newly created instances.

# 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}"
  }
}
  1. Module for Variables

    This module contains variables which contain the values to be used in the resource block for creating EC2 instances.

# 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)
}
  1. Module for Output

    This module will show the output of the server id that is newly created.

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

Task: More on Terraform modules

  • Write about different modules of Terraform.

    Modules in Terraform serve as building blocks for your infrastructure code, allowing you to encapsulate and abstract infrastructure components. They promote code reusability, maintainability, and scalability. Let's explore some of the different types of modules commonly used in Terraform:

    1. Root Modules: A root module is a top-level module in a Terraform configuration. It typically represents an entire infrastructure or a major component of it. Root modules define and manage resources directly, and they may also call child modules to encapsulate specific functionality.

    2. Child Modules: Child modules are reusable modules that encapsulate a specific set of resources or functionality. They can be used within root modules or other child modules, enabling composition and modularity. Child modules provide a way to organize and abstract infrastructure components, making the configuration more manageable.

    3. Published Modules: Publishing a module in Terraform refers to making it available for use by other users within the Terraform ecosystem. When you publish a module, others can easily consume and reuse it in their own infrastructure configurations.

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

    No, they are not the same:

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

    2. 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:

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

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

ย