Welcome to Terraform Modules, Infra Coders!
Hey there, Infra coders! So far, you’ve mastered installing Terraform, connecting it to a cloud provider, and understanding the all-important state file. Now, let’s level up with Terraform modules. Think of modules as reusable blueprints—like a recipe you can use to cook the same dish in different kitchens without rewriting the whole thing. Modules make your Terraform code cleaner, reusable, and easier to manage, especially when your projects get bigger or involve a team.
In this article, we’ll explore what modules are, why they’re awesome, how to create and use them, and some best practices to keep your code tidy. Let’s jump in!

What Are Terraform Modules?
A Terraform module is a set of Terraform configuration files grouped together to create a specific piece of infrastructure. Instead of writing the same code over and over for, say, an S3 bucket or a virtual machine, you bundle that code into a module. Then, you can reuse it across projects or environments (like dev, staging, or prod) with just a few tweaks.
For example, imagine you need an S3 bucket with specific settings in multiple projects. Without modules, you’d copy-paste the same code everywhere. With a module, you write the bucket code once, store it in a folder, and call it whenever you need it. It’s like having a LEGO block you can snap into any creation!
Why Use Modules?
Modules are a game-changer for several reasons:
- Reusability: Write once, use everywhere. No more copy-pasting!
- Consistency: Ensure the same setup (like an S3 bucket or server) is identical across projects.
- Simplicity: Break complex infrastructure into smaller, manageable pieces.
- Team Collaboration: Share modules with your team to standardize infrastructure.
- Less Maintenance: Update a module in one place, and all projects using it get the update.
How Do Modules Work?
A module is just a folder with Terraform files (like main.tf
, variables.tf
, or outputs.tf
). You call a module from your main Terraform configuration using a module
block. Here’s the basic flow:
- Create a Module: Write Terraform code in a folder (e.g.,
s3-module/
) to define a resource like an S3 bucket. - Use the Module: In your main project, reference the module and pass in custom settings (like the bucket name).
- Run Terraform: Terraform combines the module’s code with your configuration and creates the resources.
Let’s walk through creating and using a module to see it in action.
Step 1: Creating Your First Module
Let’s create a simple module to set up an AWS S3 bucket with some standard settings. Create a folder structure like this:
my-project/
├── main.tf
└── s3-module/
├── main.tf
├── variables.tf
└── outputs.tf
Write the Module Code
In the s3-module/
folder, add these files:
In s3-module/main.tf
:
resource "aws_s3_bucket" "bucket" {
bucket = var.bucket_name
tags = {
Environment = var.environment
}
}
In s3-module/variables.tf
:
variable "bucket_name" {
description = "Name of the S3 bucket"
type = string
}
variable "environment" {
description = "Environment for the bucket (e.g., dev, prod)"
type = string
default = "dev"
}
In s3-module/outputs.tf
:
output "bucket_id" {
description = "The ID of the created S3 bucket"
value = aws_s3_bucket.bucket.id
}
This module creates an S3 bucket with a name and environment tag you specify. The outputs.tf
file lets you retrieve the bucket’s ID for use elsewhere.
Step 2: Using the Module
Now, let’s use this module in your main project. In my-project/main.tf
, add:
provider "aws" {
region = "us-east-1"
}
module "my_s3_bucket" {
source = "./s3-module"
bucket_name = "my-unique-bucket-123"
environment = "prod"
}
output "bucket_id" {
value = module.my_s3_bucket.bucket_id
}
Here’s what’s happening:
- The
module
block calls thes3-module
folder usingsource = "./s3-module"
. - We pass in values for
bucket_name
andenvironment
. - The
output
block grabs the bucket ID from the module to display it after creation.
Step 3: Running the Module
Navigate to your my-project/
folder in your terminal and run these commands:
- Initialize: Downloads the AWS provider and sets up the module:
terraform init
- Plan: Checks what Terraform will create:
terraform plan
- Apply: Creates the S3 bucket:
terraform apply
Type
yes
to confirm, and Terraform will create the bucket using your module. You’ll also see the bucket ID in the output.

Where to Store Modules
You can store modules in a few places:
- Local Path: Like
./s3-module
, great for testing or small projects. - Git Repository: Host modules on GitHub or GitLab (e.g.,
source = "git::https://github.com/your-repo/s3-module.git"
) for team sharing. - Terraform Registry: Use public or private modules from
registry.terraform.io
(e.g.,source = "terraform-aws-modules/s3-bucket/aws"
).
For example, to use a public module from the Terraform Registry, you could replace your module with:
module "my_s3_bucket" {
source = "terraform-aws-modules/s3-bucket/aws"
version = "4.1.0"
bucket = "my-unique-bucket-123"
}
This pulls a community-tested S3 module, saving you time.
Best Practices for Terraform Modules
To keep your modules clean and effective, follow these tips:
- Keep Modules Focused: Each module should do one thing well (e.g., create an S3 bucket, not a bucket plus a database).
- Use Variables: Make modules flexible with variables for names, regions, or other settings.
- Document Everything: Add descriptions to variables and outputs to make your module user-friendly.
- Version Modules: If sharing modules via Git or Terraform Registry, use version tags (like
v1.0.0
) to track changes. - Test Modules: Test your module in a sandbox environment before using it in production.
- Avoid Hardcoding: Don’t put fixed values (like bucket names) in the module—use variables instead.
Cleaning Up
To remove the S3 bucket you created, run:
terraform destroy
Type yes
to confirm, and Terraform will delete the bucket. This keeps your AWS account tidy and cost-free.
What’s Next?
Great job, Infra coders! You’ve just built and used your first Terraform module. You’re now ready to make your infrastructure code reusable and organized. In the next article, we’ll dive into variables and outputs to make your Terraform setups even more flexible and powerful. Keep coding, and see you soon!
The post Modules in Terraform: Creating Reusable Infrastructure Code appeared first on TecAdmin.
Source: Read More