> For the complete documentation index, see [llms.txt](https://devops-3.gitbook.io/devops/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://devops-3.gitbook.io/devops/docs/module-12-iac/modules.md).

# Modules

The capability to generate reusable infrastructure components known as modules is one of the primary characteristics that contribute to Terraform power.

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.

<figure><img src="https://921544542-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FiRQTWHUNVZ5tIxQJXclF%2Fuploads%2FT6fzTjcfMu4I7wlQACVZ%2F1_mYeKy7WYpEDlGx7vkr72IQ.webp?alt=media&amp;token=e5bdc8a5-b0e3-4c42-a424-350b633dfd3d" alt=""><figcaption><p>Modules</p></figcaption></figure>

### Benefits of Modules

* Reusability
* Code Reduction
* Organization
* Consistency
* Parameterization
* Management

Resources Vs Modules

A resource in Terraform describes a piece of infrastructure that is going to be created (e.g., a VPC, a subnet, an ec2 instance, etc).

A module is a collection of resources that are used together to achieve a reusable use case.

Terraform Modules Workflow

<figure><img src="https://921544542-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FiRQTWHUNVZ5tIxQJXclF%2Fuploads%2F3q5C2Ehbv7gTyHKJWr5L%2FPicture1-1.png?alt=media&amp;token=80faf8d8-4943-4535-ad82-025c0a5603b8" alt=""><figcaption><p>Workflow</p></figcaption></figure>

A typical module would resemble this:

```hcl
. 
ec2_instance
├── main.tf
├── variables.tf
└── outputs.tf
```

### Types of Modules

1. Root Module
2. Child Module
3. Local Module
4. Published Module

<figure><img src="https://921544542-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FiRQTWHUNVZ5tIxQJXclF%2Fuploads%2FAWACYH0JdMdS6aFa1crH%2FterraformModule.png?alt=media&amp;token=239c0ab3-4668-4272-a0c0-ecf17e4cfad3" alt=""><figcaption><p>Module Types</p></figcaption></figure>

### Define Module&#x20;

Within your primary Terraform setup, you can call and instantiate modules using the modules block.

```hcl
module "module_name" {
 source = "path_of_module"
 [other_arguments]
}
```

Example

```hcl
provider "aws" {
 region = "us-east-2"
}
module "test_server" {
 source = "./ec2_instance"
 instance_type = "t2.micro"
 ami_id        = "ami-98656754364"
}
output "instance_id" {
 value = module.test_server.instance_id
}
```

1. Root Module

The root module consists of all the resources defined in the .tf files in a Terraform configuration, meaning that all Terraform configurations have their own root module.&#x20;

Even if you are simply creating a main.tf that has just a locals block inside of it with a local variable, that is still considered a root module.

2. Child Module

Every module can call other modules, and all of the modules called inside another module are considered child modules.

Calling a child module means to include all the resources defined in that module in the current configuration. This is done by using a module block inside your Terraform configuration:

```hcl
module "webservers" {
    source = "../webserver"
}
```

3. Local Module

A local module is a module that wasn’t published in any registry and when it is sourced, it is using the path to that particular module.

4. Published Module

A published module refers to a module that has been pushed to a Terraform Registry, or even simply on a VCS and has a tag associated with it. When a published module is sourced, the URL of that module is used either from the registry or from the VCS itself
