Day 70 - Terraform Modules πŸš€

Nilkanth MistryNilkanth Mistry
4 min read

Welcome back to the #90DaysOfDevOps Challenge! πŸš€ Today, on Day 70, we're diving into Terraform modules, an essential concept in infrastructure-as-code (IAC) that allows us to encapsulate reusable pieces of infrastructure configurations. Let's get started! 🌟

Table of Contents

  1. Terraform Modules πŸ“¦

  2. Using Modules πŸ› οΈ

    • Step 1: Create a Module πŸ—οΈ

    • Step 2: Implement the Root Configuration 🌐

  3. Difference between Root Module and Child Module πŸ”„

  4. Modules and Namespaces πŸ“›


Terraform Modules πŸ“¦

Terraform modules are reusable packages of Terraform configurations that can be used to create multiple instances of the same resource or a set of resources. They serve as building blocks to compose your infrastructure, making your Terraform code more modular, maintainable, and scalable. 🎯

  • 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 in the configuration concisely. πŸ”„

  • 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. ♻️

Using Modules πŸ› οΈ

To use a Terraform module effectively, follow these steps:

Step 1: Create a Module πŸ—οΈ

Before we can use a module, we need to create one! Let’s consider an example where we want to set up an AWS S3 bucket with a predefined configuration. We’ll create a module called β€œs3_bucket” to encapsulate this resource. πŸͺ£

Create Directory Structure:

s3_bucket/
β”œβ”€β”€ main.tf
└── variables.tf

Define S3 Bucket Resource inmain.tf:

# s3_bucket/main.tf

resource "aws_s3_bucket" "bucket" {
  bucket = var.bucket_name
  acl    = var.acl
  tags   = var.tags
}

Declare Input Variables invariables.tf:

# s3_bucket/variables.tf

variable "bucket_name" {
  description = "The name of the S3 bucket"
  type        = string
}

variable "acl" {
  description = "Access control list for the S3 bucket"
  type        = string
}

variable "tags" {
  description = "Tags to apply to the S3 bucket"
  type        = map(string)
}

Our module is now ready for use! πŸŽ‰

Step 2: Implement the Root Configuration 🌐

Now, let’s create a root configuration file that will use our β€œs3_bucket” module to create an actual S3 bucket. In this example, we’ll create a file named main.tf for the root configuration:

# main.tf

provider "aws" {
  region = "us-west-2"
}

module "my_s3_bucket" {
  source      = "./s3_bucket"
  bucket_name = "my-awesome-bucket"
  acl         = "private"
  tags = {
    Environment = "Dev"
    Project     = "MyApp"
  }
}

In this root configuration, we specify the AWS provider and then call our β€œs3_bucket” module using the module block. We pass the required values for the input variables bucket_name, acl, and tags. πŸš€

Using modules in Terraform provides a way to organize and manage complex infrastructure while promoting reusability and maintaining a modular design. It allows teams to build infrastructure components once and share them across projects, leading to more efficient infrastructure management. πŸ”§

Difference between Root Module and Child Module πŸ”„

In Terraform, the Root Module refers to the main configuration file that is executed by Terraform. This file typically contains the code that defines the overall infrastructure and serves as the entry point for Terraform to execute the configuration. The root module can call other child modules and pass values to them as arguments. πŸš€

On the other hand, a Child Module is a separate directory containing Terraform configuration files that define a specific piece of infrastructure. Child modules can be reusable and are typically called by the root module or other child modules. They help in organizing the Terraform codebase and promoting code reusability. ♻️

To summarize:

  • Root Module: The starting point of execution. 🎬

  • Child Modules: Self-contained configurations that can be called and reused by the root module. πŸ”‚

Modules and Namespaces πŸ“›

No, modules and namespaces are not the same in Terraform.

Modules (as discussed above) are a way to organize and package Terraform configuration files. They allow you to encapsulate infrastructure resources into reusable components. Modules help in achieving a modular design and simplifying the infrastructure code. πŸ—οΈ

Namespaces, on the other hand, are a concept used in programming and computer systems to distinguish between different sets of identifiers, such as variables, functions, or resources, and avoid naming conflicts. In Terraform, namespaces are essential for maintaining unique names for resources and variables within a module or configuration. πŸ”‘

In summary:

  • Modules are used for code organization and reusability. πŸ“¦

  • Namespaces are used for maintaining unique identifiers within the code. 🏷️

By leveraging modules, you can create a hierarchy of reusable components, each with its own input and output variables, to create a more structured and maintainable infrastructure codebase. πŸ—‚οΈ


That’s it for today’s #90DaysOfDevOps Challenge. Stay tuned for Day 71, where we’ll review some useful Terraform interview questions! πŸ’‘

Feel free to share your progress and questions on LinkedIn and Twitter using the hashtag #90DaysOfDevOps. Happy coding! πŸŽ‰

0
Subscribe to my newsletter

Read articles from Nilkanth Mistry directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nilkanth Mistry
Nilkanth Mistry

Embark on a 90-day DevOps journey with me as we tackle challenges, unravel complexities, and conquer the world of seamless software delivery. Join my Hashnode blog series where we'll explore hands-on DevOps scenarios, troubleshooting real-world issues, and mastering the art of efficient deployment. Let's embrace the challenges and elevate our DevOps expertise together! #DevOpsChallenges #HandsOnLearning #ContinuousImprovement