Day 70 - Terraform Modules π
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
Terraform Modules π¦
Using Modules π οΈ
Step 1: Create a Module ποΈ
Step 2: Implement the Root Configuration π
Difference between Root Module and Child Module π
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! π
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