Terraform Modules

Terraform Modules

·

6 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 an AWS EC2 instance:

# Create AWS EC2 Instance
resource "aws_instance" "server-instance" {

  # Set the number of instances
  instance_count = var.number_of_instances

  # Configure the instance
  ami                    = var.ami
  instance_type          = var.instance_type
  subnet_id              = var.subnet_id
  vpc_security_group_ids = var.security_group

  # Add a tag with the instance name
  tags = {
    Name = "${var.instance_name}"
  }
}

This code example demonstrates how to use Terraform to create an EC2 instance on AWS.

  1. Resource Declaration:

    • Declares a new instance named "server-instance" using the aws_instance resource.
  2. Instance Count:

    • Specifies how many instances to create based on the variable var.number_of_instances (to be defined in the Server Module Variables section).
  3. AMI Selection:

    • Chooses the Amazon Machine Image (AMI) using the ID specified in the var.ami variable.
  4. Instance Type:

    • Sets the type of the instance based on the value stored in var.instance_type.
  5. Subnet Assignment:

    • Determines the subnet where the instance will be launched using the ID from var.subnet_id.
  6. Security Group Configuration:

    • Sets the security group(s) associated with the instance based on the value in var.security_group.
  7. Tagging:

    • Adds a tag with the key "Name" and the value from the var.instance_name variable to the instance.

Server Module Variables:

# Server Module Variables

variable "number_of_instances" {
  description = "How many instances to create"
  type        = number
  default     = 1
}

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

variable "ami" {
  description = "ID of the Amazon Machine Image (AMI)"
  default     = "ami-xxxx"
}

variable "instance_type" {
  description = "Type of the EC2 instance"
}

variable "subnet_id" {
  description = "ID of the subnet for launching the EC2 instance"
}

variable "security_group" {
  description = "Security group(s) associated with the EC2 instance"
  type        = list(any)
}
  • This code showcases a Terraform configuration file with various variables for configuring the creation of EC2 instances on AWS. Let's simplify the description of each variable:

    1. Number of Instances:

      • Defines the quantity of EC2 instances to create. Default is 1 if no value is given.
    2. Instance Name:

      • Sets the name of the EC2 instance. User input is necessary as there's no default value.
    3. AMI ID:

      • Specifies the Amazon Machine Image (AMI) ID for the EC2 instance. Default is "ami-xxxx" if not provided.
    4. Instance Type:

      • Determines the type of the EC2 instance. User input is required, and there's no default value.
    5. Subnet ID:

      • Sets the ID of the subnet where the EC2 instance will be launched. User input is mandatory with no default value.
    6. Security Groups:

      • Configures the security group(s) for the EC2 instance. It accepts one or more security groups and requires user input, lacking a default value.

Server module Output:

# Output Section

output "server_id" {
  description = "ID of the created server"
  value       = aws_instance.server-instance.id
}
  • This code illustrates a Terraform configuration file for generating an AWS EC2 instance using the aws_instance resource and displaying its ID through the output section. Here's a simplified breakdown:

    1. Output Declaration:

      • Declares a new output named "server_id" using the output section.
    2. Output Value Setting:

      • Sets the output value to the ID of the previously created EC2 instance with aws_instance.server-instance.id. This format refers to the "id" attribute of the resource named "server-instance" of type aws_instance.

Task-01

Explain the below in your own words.

1.Write about different modules Terraform.

Terraform is a widely used tool for managing and setting up cloud resources from different cloud providers. One cool thing about Terraform is how it's built: it lets you divide your setup code into smaller parts called modules, which you can use over and over again. This helps a lot when dealing with complex setups.

There are a few types of modules in Terraform:

  1. Root Modules:

    • These are like the main modules where you start your Terraform setup. They contain the basic information like how to connect to your cloud provider and any details needed to describe your resources.
  2. Child Modules:

    • These are smaller modules inside the main one. They're useful for grouping related things together, making it easier to handle.
  3. Published Modules:

    • These are modules shared with everyone on the Terraform Module Registry. This registry is a public spot where people share their modules so that others can use and improve them.

Each module has its own set of resources and settings that can be handled on its own. This makes it simpler to manage and test your setup, and it encourages using code in more than one place. You can either create your modules or use ones that others have shared on the Terraform Module Registry. It's like a big collection of helpful tools created by the community.

2.Difference between Root Module and Child Module.

The main contrast between a root module and a child module in Terraform is their job in the overall setup.

  • Root Module:

    • This is like the boss module in a Terraform setup. It's the starting point for everything and contains essential details like how to connect to your cloud provider and any info needed for your resources. It's in charge of kicking off Terraform, setting up providers, and stating what resources are needed.
  • Child Module:

    • This is a smaller module inside the main one. It's used to group related things together, making it easier to handle. A child module is often made to wrap up a bunch of resources that do a specific job. Child modules are like reusable building blocks that you can use in different setups.

Child modules are added inside a root module using a module block. This block says where the module comes from and what input details it needs. The source can be a local file or something far away, like a version control system or the Terraform Module Registry.

One more thing to note: root modules can talk about variables that everyone can see, including child modules. But, a child module can only talk about variables that stay inside its own module.

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

No, modules and namespaces are not the same concept, although they may share some similarities in certain contexts.

  • Modules:

    • In Terraform, a module is like a mini-package of infrastructure code. It bundles together a bunch of related resources that you can use over and over in different setups. Modules make it easier to use the same code in multiple places, promoting a tidy and reusable way to handle complex setups.
  • Namespaces:

    • A namespace is like a system for organizing names or labels to avoid confusion. Think of it as a way to group things with similar names so they don't get mixed up. In programming, namespaces are used to prevent naming clashes between different parts of the code, like modules, functions, or variables. They help keep everything organized under a common label.

So, even though modules and namespaces both help with organization and making things clearer, they do slightly different jobs. Modules in Terraform are about organizing and reusing infrastructure code, while namespaces are about keeping names organized and avoiding conflicts in programming.


Thanks for reading until here. See you in the next one.