> For the complete documentation index, see [llms.txt](https://notes.nomanaziz.me/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://notes.nomanaziz.me/devops/infrastructure-as-a-code-iac/terraform/12.-modules.md).

# 12. Modules

### Introduction

* 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
* Modules are main way to package and reuse resource configurations with terraform
* Input variables are passed in via module block

***

### Types

#### 1. Root Module

Default module containing all .tf files in main working directory

#### 2. Child Module

A sepearte external module referred to from a .tf file

***

### Module Sources

#### Local paths

```hcl
module "web-app" {
  source = "../web-app"
}
```

#### Terraform Registry

```hcl
module "web-app" {
  source = "hashicorp/consul/aws"
  version = "0.1.0"
}
```

#### Github

* via HTTPS
* via SSH
* Generic i.e `source = "git::ssh//username@example.com/storage.git"`

#### Others

* Bitbucket
* Generic Git
* HTTP URLs
* S3 buckets

***

### Good Module Characteristics

* Raise abstraction level from base resource types
* Group resources in a logical fashion
* Exposes input variables to allow necessary customization + composition
* Provide useful defaults
* Returns outputs to make further integrations possible

***
