If you’re starting with Infrastructure as Code (IaC), Terraform is one of the most popular tools out there. It allows you to define and manage your infrastructure through code, making deployments consistent, repeatable, and scalable. One powerful feature of Terraform is modules, which enable reusability and organization of your code. In this guide, we will walk through creating your first Terraform module.
What is a Terraform Module?
A Terraform module is a container for multiple resources that are used together. Modules allow you to create reusable, logical units of infrastructure that can be shared and used across your projects. Every Terraform configuration file (.tf file) implicitly acts as a module, but you can organize resources into dedicated modules to improve reusability.
Steps to Create a Terraform Module
Step 1: Set Up Your Project Directory
Start by setting up a directory structure for your project. Let’s assume you’re creating an AWS EC2 instance using Terraform.
project/
├── main.tf
├── variables.tf
├── outputs.tf
└── modules/
└── ec2_instance/
├── main.tf
├── variables.tf
├── outputs.tf
Here, the main.tf, variables.tf, and outputs.tf files in the ec2_instance directory will define your EC2 module, while the files in the root directory will call and configure the module.
Step 2: Write the Module Code
Inside the modules/ec2_instance directory, create a main.tf file. This file defines the EC2 resource:
# main.tf (module)
provider "aws" {
region = var.region
}
resource "aws_instance" "example" {
ami = var.ami
instance_type = var.instance_type
tags = {
Name = var.instance_name
}
}
In this example, we’re defining a single EC2 instance that takes parameters like the AMI ID, instance type, and instance name.
Next, create a variables.tf file to declare the variables the module will use:
# variables.tf (module)
variable "region" {
description = "The AWS region to launch the instance"
type = string
}
variable "ami" {
description = "AMI ID for the EC2 instance"
type = string
}
variable "instance_type" {
description = "Type of EC2 instance"
type = string
default = "t2.micro"
}
variable "instance_name" {
description = "Name tag for the instance"
type = string
}
Finally, in the outputs.tf file, output any values you may want to use later:
# outputs.tf (module)
output "instance_id" {
description = "The ID of the EC2 instance"
value = aws_instance.example.id
}
Step 3: Call the Module
In your root directory’s main.tf, you can now call your module:
# main.tf (root)
provider "aws" {
region = "us-east-1"
}
module "ec2_instance" {
source = "./modules/ec2_instance"
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
instance_name = "MyFirstInstance"
region = "us-east-1"
}
Here, we are specifying the location of the module (./modules/ec2_instance), and passing values for the variables ami, instance_type, instance_name, and region.
Step 4: Initialize and Apply
Now that everything is set up, initialize your project by running:
terraform init
Then apply your changes:
terraform apply
Terraform will ask you to confirm the creation of resources. Once confirmed, Terraform will create the EC2 instance based on your module configuration.
Step 5: Clean Up
When you’re done, you can destroy the infrastructure to avoid ongoing costs:
terraform destroy
Conclusion
You’ve now created your first Terraform module! Organizing your code into reusable modules is a best practice, especially for larger projects. You can share these modules across multiple environments or teams, improving consistency and reducing errors. As you continue with Terraform, you’ll find that modules make managing infrastructure much more efficient and scalable.
Happy coding!

Leave a comment