Sitemap
DevOps.dev

Devops.dev is a community of DevOps enthusiasts sharing insight, stories, and the latest development in the field.

Terraform Modules

3 min readFeb 9, 2023

--

Terraform is the leading open-source Infrastructure as a code tools.It lets developers create infrastructure configurations for major cloud platforms like AWS, Azure, GCP, and IBM to specific tools and services like Cloudflare, Elastic stack, and GitLab.

Terraform Modules

A Terraform module is a collection of standard configuration files in a dedicated directory. Terraform modules encapsulate groups of resources dedicated to one task, reducing the amount of code you have to develop for similar infrastructure components.In many instances, you have to define the same configuration multiple times when creating infrastructure configurations or providing them across environments. Repeating the same code block is inefficient and can easily lead to mistakes.

Terraform Modules address this issue by providing predefined resource structures. Think of modules as a collection of grouped resources, including variables and outputs.

Press enter or click to view image in full size

If you run Terraform in this directory, those configuration files would be considered a root module. It means that this configuration is the base of your operation, a core that you can expand further.

Types of Terraform Modules

Terraform lets you utilize prebuilt modules available through the Terraform registry or create custom modules to suit their needs. There are thousands of free modules in the Terraform registry that cater to different providers and use cases.

Get Techno Freak’s stories in your inbox

Join Medium for free to get updates from this writer.

Check out for more info about other modules and syntax:https://developer.hashicorp.com/terraform/language/modules/syntax

Primary types of terraform modules:

  1. Root Module: This is the root of any configuration. Every Terraform configuration consists of the root module as the main directory that works with the .tf files. You can call any number of child modules or published modules from the root module.
  2. Sample Child Module (Local Module): Above is the configuration for the local module “web-app” referred to in the earlier example of the Root module. At a minimum, it consists of three files: the variables, output, and the config (main.tf).
  3. Child Module:Any module that can be called by another module (usually the Root module) is considered a child module.The Terraform registry which stores publicly available modules that can be used by any user in their configurations.
  4. Published Modules:Published Modules are modules pushed to a private or public repository. Terraform registry is the primary public repository. It hosts freely-accessible modules that can be used by anyone within their configurations. Terraform will automatically download the necessary modules from the registry when the appropriate source and version are defined in the module block.

Example of main.tf ec2 instance in an AWS environment

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

Problems solved by using terraform modules

  1. Human Error: When you copy-paste or create a group of resources from scratch, it’s easy to make a mistake, like rewriting or renaming something. To make sure that doesn’t happen, create a single Terraform module, test it, then use it in multiple places to check whether all of the elements are correct. It’s easier to check and test what you type into a single block than scroll, jumping from one place to another
  2. Code Repetition: Copy-pasting an entire stack of code whenever you need more than a single instance of something isn’t scalable or efficient. It’s a waste of many things, time in particular.
  3. Lack of Code clarity: Copy-pasting isn’t clean either. Good code is readable, and great code documents itself. A modular approach addresses all those pesky details. A configuration of connecting modules dedicated to particular tasks is much easier to read and understand.

Check out for more info about other modules and syntax:https://developer.hashicorp.com/terraform/language/modules/syntax

--

--

Techno Freak
Techno Freak

Devops &Full-Stack enthusiast . Helping People to learn about cloud and opensource . Learning bit by bit

DevOps.dev
DevOps.dev

Devops.dev is a community of DevOps enthusiasts sharing insight, stories, and the latest development in the field.