0Pricing
DevOps Bootcamp · Lesson

Creating Reusable Modules

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

Creating Reusable Modules is a free DevOps Bootcamp lesson on CoddyKit — lesson 2 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.

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.

Frequently asked questions

Is the “Creating Reusable Modules” lesson free?

Yes — the full text of “Creating Reusable 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 “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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

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