Introduction to Terraform Modules
Understand the benefits of using modules to encapsulate and reuse common infrastructure patterns, improving code organization and maintainability.
Introduction to Terraform Modules is a free DevOps Bootcamp lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What are Terraform Modules?
A Terraform module is a self-contained package of Terraform configurations. Think of it like a function or a class in programming languages. It allows you to group related resources and manage them as a single unit.
Modules help you organize your infrastructure code and make it more manageable, especially for larger projects.
Why Use Terraform Modules?
Modules bring several key benefits to your infrastructure as code:
- Reusability: Define a piece of infrastructure once and use it multiple times across different projects or environments.
- Organization: Break down complex configurations into smaller, manageable, and logical components.
- Consistency: Ensure that resources are provisioned in a standardized way every time, reducing configuration drift and errors.
- Collaboration: Teams can develop and share modules, speeding up development and maintaining standards.
Module Structure Explained
A Terraform module is essentially a directory containing one or more .tf files. While flexible, a common structure includes:
main.tf: Defines the primary resources that the module creates.variables.tf: Declares input variables that customize the module's behavior.outputs.tf: Defines output values that the module exposes to its callers.versions.tf: Specifies Terraform and provider version constraints for the module.
This organized structure helps keep your code tidy and understandable.
Your First Module Code
Let's define a very simple module that creates an AWS S3 bucket. This code would typically live in its own directory, for example, modules/my_s3_bucket/main.tf.
This complete file could be run as a root module to see it in action, but its primary purpose here is to show the internal structure of a module.
provider "aws" {
region = "us-east-1" # Example region
}
variable "bucket_name" {
description = "Name for the S3 bucket."
type = string
}
variable "acl_setting" {
description = "ACL for the S3 bucket."
type = string
default = "private"
}
resource "aws_s3_bucket" "example_bucket" {
bucket = var.bucket_name
acl = var.acl_setting
tags = {
Environment = "Dev" # Example tag
}
}
output "bucket_id" {
description = "The ID of the S3 bucket."
value = aws_s3_bucket.example_bucket.id
}
output "bucket_arn" {
description = "The ARN of the S3 bucket."
value = aws_s3_bucket.example_bucket.arn
}Module Inputs: Variables
Input variables allow you to customize the behavior of your module without changing its core code. They are defined in a variables.tf file within the module directory.
When you call a module, you pass values to these variables, making the module highly reusable for different scenarios and environments. Think of them as function arguments.
variable "bucket_name" {
description = "Name for the S3 bucket."
type = string
}
variable "acl_setting" {
description = "ACL for the S3 bucket."
type = string
default = "private"
}Module Outputs: Exposing Data
Output values are used to expose data from a module to its calling configuration. This is how you can access important information about the resources created by the module.
Outputs are defined in an outputs.tf file within the module directory and can be referenced by other parts of your Terraform configuration or even by other modules.
output "bucket_id" {
description = "The ID of the S3 bucket."
value = aws_s3_bucket.example_bucket.id
}
output "bucket_arn" {
description = "The ARN of the S3 bucket."
value = aws_s3_bucket.example_bucket.arn
}Calling Your Custom Module
Now that we have our my_s3_bucket module defined, let's call it from a root configuration. The source attribute specifies where Terraform can find the module.
For local modules, this is a relative path to the module's directory. We pass values to the module's input variables and can access its exposed outputs.
provider "aws" {
region = "us-east-1"
}
module "my_website_bucket" {
source = "./modules/my_s3_bucket" # Path to your local module
bucket_name = "my-unique-website-bucket-12345"
acl_setting = "public-read"
}
output "website_bucket_arn" {
description = "ARN of the website S3 bucket."
value = module.my_website_bucket.bucket_arn
}
output "website_bucket_id" {
description = "ID of the website S3 bucket."
value = module.my_website_bucket.bucket_id
}Different Module Sources
Modules aren't just local! Terraform can fetch modules from various sources, giving you flexibility:
- Local Paths:
./modules/my_module(as seen in our example) - Terraform Registry:
hashicorp/vpc/aws(publicly available modules) - Git Repositories:
git::https://example.com/repo.git - S3 Buckets:
s3::https://s3-us-west-2.amazonaws.com/example-bucket/module.zip
The Terraform Registry is a great place to find pre-built, community-vetted modules for common infrastructure patterns.
The Practical Impact of Modules
Imagine managing hundreds of resources across multiple environments (development, staging, production). Without modules, you'd have massive, repetitive configuration files.
Modules allow you to define a "web server" or "database" pattern once. Then, you simply call that module for each instance, passing different variables for environment-specific settings. This drastically reduces errors, simplifies updates, and makes your infrastructure truly scalable.
Quick Check
You've learned about the structure and benefits of Terraform modules. Let's see if you can identify a key advantage.
Recap & Next Steps
In this lesson, we introduced Terraform modules as powerful tools for organizing and reusing your infrastructure code. You learned:
- Modules encapsulate resources into reusable units.
- They improve code organization, consistency, and reusability.
- Modules use
variables.tffor inputs andoutputs.tffor exposed data. - You can call modules from local paths, registries, or Git repositories.
Next, we'll dive deeper into creating your own reusable modules, exploring more advanced patterns and best practices.
Frequently asked questions
Is the “Introduction to Terraform Modules” lesson free?
Yes — the full text of “Introduction to Terraform Modules” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “Introduction to Terraform Modules”?
Understand the benefits of using modules to encapsulate and reuse common infrastructure patterns, improving code organization and maintainability. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Introduction to Terraform Modules” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Introduction to Terraform Modules
- Creating Reusable Modules
- Using Workspaces for Environments
- Publishing Modules to a Registry