DevOps Bootcamp · Lesson

Creating Reusable Modules

Learn to author your own Terraform modules, defining inputs, outputs, and resources to build modular and flexible configurations.

Lesson 2 of 411 steps

Creating Reusable Modules is a free DevOps Bootcamp lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, and your progress syncs across the web and the CoddyKit app. The DevOps Bootcamp course includes 4 lessons in total.

Build Your Own Terraform Modules

In the previous lesson, we learned about using existing Terraform modules. Now, let's dive into creating your very own reusable modules!

Authoring modules helps you encapsulate common infrastructure patterns, making your configurations cleaner, more consistent, and easier to maintain.

Organizing Your Module Files

A Terraform module is essentially a collection of .tf files stored in a dedicated directory.

At its simplest, a module directory will contain:

  • variables.tf: Defines input variables for the module.
  • main.tf: Contains the actual resource declarations.
  • outputs.tf: Defines output values from the module.

You can also include versions.tf for provider requirements and README.md for documentation.

Module Inputs: Variables

To make your module flexible, you'll define input variables. These allow users of your module to customize its behavior without changing the module's core code.

Variables are defined using a variable block, specifying a description, type, and optionally a default value.

Example: Defining a Module Variable

Here's how you might define a variable for an instance type within your module's variables.tf file:

# variables.tf
variable "instance_type" {
  description = "The EC2 instance type to use."
  type        = string
  default     = "t2.micro"
}

Module Resources: The Core Logic

The heart of your module lies in its resource declarations. These are the actual cloud resources that the module will provision.

You'll place these resource blocks, like an AWS EC2 instance or an S3 bucket, in your module's main.tf file.

Crucially, resources within the module will often use the input variables you've defined.

Resource Declaration Using Inputs

Let's declare an EC2 instance in main.tf, using our instance_type variable:

# main.tf
resource "aws_instance" "web_server" {
  ami           = "ami-0abcdef1234567890" # Example AMI ID
  instance_type = var.instance_type
  tags = {
    Name = "MyWebServer"
  }
}

Module Outputs: Sharing Information

Just as modules take inputs, they can also provide output values. These are useful for exporting information about the resources created by the module.

For example, you might want to output the public IP address of an EC2 instance or the ARN of an S3 bucket.

Outputs are defined in outputs.tf using an output block.

Defining a Module Output Value

Here's how to define an output for the public IP of our EC2 instance in outputs.tf:

# outputs.tf
output "instance_public_ip" {
  description = "The public IP address of the EC2 instance."
  value       = aws_instance.web_server.public_ip
}

Calling Your New Module

Once your module is defined, you can use it in a root configuration (or another module) just like any other module.

You'll specify the source (path to your module directory) and pass values to its input variables.

# root_config/main.tf
module "my_web_server_module" {
  source        = "../my-ec2-module" # Path to your module
  instance_type = "t3.small"         # Override default
}

output "web_server_ip" {
  value = module.my_web_server_module.instance_public_ip
}

Module Components Check

You've learned about the key files in a Terraform module. Which file is primarily used to define values that can be passed into the module?

Recap: Crafting Reusable Modules

Great job! You now know how to author your own Terraform modules.

  • Modules are directories containing .tf files.
  • Use variables.tf for inputs.
  • Place resource definitions in main.tf.
  • Define outputs in outputs.tf to expose data.

This skill is crucial for building scalable, maintainable, and consistent infrastructure as code.

Free to start

Learn DevOps Bootcamp with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
142
Lessons
568

Frequently Asked Questions

Is the “Creating Reusable Modules” lesson free?

Yes — any 3 lessons of the DevOps Bootcamp learning path, including “Creating Reusable Modules”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The DevOps Bootcamp course includes 4 lessons in total.

What will I learn in “Creating Reusable Modules”?

Learn to author your own Terraform modules, defining inputs, outputs, and resources to build modular and flexible configurations. 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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Creating Reusable 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

  1. Introduction to Terraform Modules
  2. Creating Reusable Modules
  3. Using Workspaces for Environments
  4. Publishing Modules to a Registry
← Back to DevOps Bootcamp