Modules and Code Reuse
Learn how Terraform modules package reusable infrastructure, accept inputs, expose outputs, and keep configurations DRY and maintainable.
Modules and Code Reuse is a free DevOps Bootcamp lesson on CoddyKit — lesson 4 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.
Why Modules?
Copy-pasting the same resources across environments leads to drift and bugs. A module is a reusable, parameterized container of Terraform resources you can call many times with different inputs, keeping your code DRY.
Every Configuration Is a Module
The directory where you run terraform apply is the root module. Modules it calls are child modules. So you already use modules — you are just adding more.
Module Anatomy
A module is just a folder with .tf files, conventionally:
main.tf— the resources.variables.tf— input variables.outputs.tf— exposed values.
Defining Inputs
Inputs are declared as variables inside the module, letting callers customize behavior.
variable "bucket_name" {
type = string
}
variable "versioning" {
type = bool
default = false
}Using the Inputs
The module's resources reference its variables.
resource "aws_s3_bucket" "this" {
bucket = var.bucket_name
}Exposing Outputs
Outputs return useful values to the caller, such as IDs or ARNs.
output "bucket_arn" {
value = aws_s3_bucket.this.arn
}Calling a Module
The root module calls a child with a module block, passing inputs.
module "logs" {
source = "./modules/bucket"
bucket_name = "app-logs"
versioning = true
}Referencing Module Outputs
Access a module's outputs with module.NAME.OUTPUT elsewhere in your config.
resource "aws_s3_bucket_policy" "p" {
bucket = module.logs.bucket_arn
}Module Sources
The source can be a local path, a Git URL, or the public Terraform Registry.
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.0.0"
}Versioning Modules
Always pin a version for registry modules so upgrades are deliberate. Run terraform init after adding or changing modules to download them.
Designing Good Modules
Keep modules focused (one logical component), expose only the inputs you need, document outputs, and avoid hardcoding environment-specific values. Small, composable modules beat one giant module.
Quick Check
Test your modules knowledge.
Recap
Modules make Terraform reusable and DRY.
- A module is a folder of .tf files with variables (inputs) and outputs.
- Call it with a
moduleblock and asource; access results viamodule.NAME.OUTPUT. - Sources can be local, Git, or the registry; pin
versionand runinit. - Design small, focused, well-documented modules.
Frequently asked questions
Is the “Modules and Code Reuse” lesson free?
Yes — the full text of “Modules and Code Reuse” 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 “Modules and Code Reuse”?
Learn how Terraform modules package reusable infrastructure, accept inputs, expose outputs, and keep configurations DRY and maintainable. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Modules and Code Reuse” 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
- Providers and Resources
- Variables and Outputs
- State Management Fundamentals
- Modules and Code Reuse