# Terraweek Day5: Terraform Modules

### Introduction

* Terraform, an Infrastructure as Code (IaC) tool, has revolutionized the way infrastructure is managed in modern cloud environments. One of its key features is modules, which allow users to encapsulate and reuse infrastructure configurations efficiently.
    
* In this guide, we'll delve into the concept of Terraform modules, their benefits, and how to create, use, compose, and version them effectively.
    

### Understanding Terraform Modules

* Terraform modules are self-contained packages of Terraform configurations that represent a set of resources.
    
* They enable the abstraction of infrastructure components into reusable building blocks, promoting modularity, reusability, and maintainability in Terraform codebases.
    
* Modules can encapsulate anything from a single resource (e.g., a virtual machine) to a complex architecture (e.g., a microservices-based application).
    

### Benefits of Terraform Modules:

1. Reusability: Modules allow you to define infrastructure components once and reuse them across multiple projects or environments, saving time and reducing duplication of effort.
    
2. Modularity: By breaking down infrastructure into smaller, modular components, modules promote a more organized and manageable codebase.
    
3. Consistency: Using modules ensures consistency in infrastructure deployment across different environments, reducing the risk of configuration drift.
    
4. Collaboration: Modules facilitate collaboration among teams by providing a standardized way to share and consume infrastructure configurations.
    
5. Versioning: Modules support versioning, enabling you to track changes, manage dependencies, and ensure reproducibility in deployments.
    

## **Types of Modules**  

**Root Modules**

Every Terraform configuration has at least one module, known as its *root module*, which consists of the resources defined in the `.tf` files in the main working directory.  
  
**Child Modules**

  
A Terraform module (usually the root module of a configuration) can *call* other modules to include their resources into the configuration. A module that has been called by another module is often referred to as a *child module.*

![Terraform Modules. Terraform is the leading open-source… | by Techno Freak  | DevOps.dev](https://miro.medium.com/v2/resize:fit:1024/0*PgKqKDKz8vQ5xhPs.png align="left")

Child modules can be referenced in your code in two ways:  
(1) local  
(2) remote

* Local modules are stored in a directory beside your root module and allow you to group related parts of your code and reuse them. An example of this might be a default S3 bucket template that you want to deploy several times to your environment. Local modules are easy to manage as they are just another part of your (root) Terraform repo.
    
* Remote child modules on the other hand are stored in a remote location (e.g. a separate git repository). The primary benefit of remote modules is that they can be reused across multiple Terraform projects.
    

### Creating and Using Terraform Modules

Let's walk through a practical example of creating and using a Terraform module to provision an AWS S3 bucket.

1. Module Creation:
    

```typescript
// modules/s3_bucket/main.tf
variable "bucket_name" {}

resource "aws_s3_bucket" "bucket" {
  bucket = var.bucket_name
  acl    = "private"
}
```

2. Module Usage:
    

```typescript
// main.tf
module "my_bucket" {
  source     = "./modules/s3_bucket"
  bucket_name = "demo-module-bucket"
}
```

In this example, we define a Terraform module called "s3\_bucket" that provisions an S3 bucket on AWS. We parameterize the module with a variable for the bucket name. Then, we consume the module in our main Terraform configuration, passing the desired bucket name as an argument.

## **Terraform Registry**

* A module registry serves as a built-in platform for sharing Terraform modules, allowing them to be easily utilized in various configurations. It employs a specialized Terraform protocol that fully incorporates version control for these modules.
    
* Modules on the public Terraform Registry can be referenced using a registry source address of the form `<NAMESPACE>/<NAME>/<PROVIDER>`
    

Let's take an example of defining VPC as a registry source from the Terraform registry

```typescript
module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "5.6.0"
}
```

### Module Versioning

* Versioning is crucial for managing changes to Terraform modules and ensuring the stability of infrastructure deployments.
    
* Terraform modules can be versioned using version control systems like Git or by leveraging Terraform's built-in versioning mechanisms.
    
* When consuming modules, you can specify a specific version or use version constraints to ensure compatibility with your infrastructure code.
    

### Conclusion

Terraform modules are a powerful feature that promotes reusability, modularity, and consistency in infrastructure configuration management. By encapsulating infrastructure components into reusable building blocks, modules enable more efficient development, deployment, and maintenance workflows.
