How-to: Create a Custom Terraform Module

Abdul Rafee Wahab
4 min readApr 21, 2023
Source: Link

Background

Custom Terraform Modules provide several benefits to infrastructure automation and management.

They are reusable code blocks that abstract infrastructure configuration into modular and easily repeatable pieces.

Some benefits of using custom Terraform modules

Simpler Codebase

With a custom Terraform module, you can abstract complex infrastructure logic into a smaller and more manageable codebase. This makes it easier to write and maintain Terraform code, reduces the risk of errors, and increases the overall reliability of your infrastructure.

Easier Collaboration

Terraform modules make it easier to collaborate on infrastructure projects. Instead of having to share large and complex Terraform files, collaborators can simply share smaller and more manageable modules. This makes it easier to maintain and update the infrastructure over time.

Reusability

Custom Terraform modules are reusable, which means that you can reuse them across multiple infrastructure projects. This saves time and effort, as you don’t need to write new code each time you want to set up similar infrastructure.

Consistency

Custom Terraform modules ensure that your infrastructure is consistent and follows best practices. By using a module, you can enforce standards and guidelines across your infrastructure, ensuring that your infrastructure is scalable, secure, and efficient.

Easy Testing

Custom Terraform modules are easier to test than complex Terraform files. By breaking down infrastructure into smaller modules, you can test each module in isolation, making it easier to detect and fix issues before they become critical.

You should consider creating custom Terraform modules if:

You’re building complex infrastructure

Custom Terraform modules are especially useful for complex infrastructure projects that involve many resources and configurations.

You’re working on multiple infrastructure projects

If you’re working on multiple infrastructure projects, creating custom Terraform modules can save time and effort by allowing you to reuse code across projects.

You want to enforce standards and best practices

Custom Terraform modules can help you enforce standards and best practices across your infrastructure, ensuring consistency and reliability.

You want to simplify your Terraform codebase

Custom Terraform modules can help you break down complex infrastructure into smaller and more manageable pieces, making it easier to write and maintain Terraform code.

Let’s create a Custom Terraform Module!

Step 1: Create a Directory for Your Module

The first step is to create a directory for your module. You can do this in your terminal using the mkdir command, followed by the name of your module directory.

mkdir my-terraform-module

Step 2: Define the Module Inputs and Outputs

The next step is to define the inputs and outputs for your module. Inputs are variables that the user of the module can set to configure it, while outputs are values that the module returns to the user.

Create a new file named variables.tf within the my-terraform-module directory, and define the input variables for your module using the variable block.

Here’s an example:

variable "aws_region" {
type = string
default = "us-east-1"
}

variable "instance_type" {
type = string
default = "t2.micro"
}

Next, create another file named outputs.tf within the my-terraform-module directory, and define the output values for your module using the output block. Here's an example:

output "instance_id" {
value = aws_instance.my_instance.id
}

output "instance_public_ip" {
value = aws_instance.my_instance.public_ip
}

Step 3: Define the Resources

The next step is to define the resources that your module creates. Resources are the actual infrastructure components that Terraform manages.

Create a new file named main.tf within the my-terraform-module directory, and define the resources for your module. Here's an example:

resource "aws_instance" "my_instance" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = var.instance_type

tags = {
Name = "my-instance"
}
}

Step 4: Add Provider Configuration

If your module uses resources from a specific cloud provider, you’ll need to configure the provider. Create a new file named providers.tf within the my-terraform-module directory, and add the provider configuration. Here's an example:

provider "aws" {
region = var.aws_region
}

Step 5: Create a README

Create a README.md file in your module directory that explains how to use your module. This should include information about the input variables, output values, and any other configuration that the user needs to provide.

Here’s an example README:

# Terraform Module: My Module

This module creates a sample EC2 instance in AWS.

## Usage


module "my_module" {
source = "github.com/arwahab/my-terraform-module"

aws_region = "us-east-1"
instance_type = "t2.micro"
}

output "instance_id" {
value = module.my_module.instance_id
}

output "instance_public_ip" {
value = module.my_module.instance_public_ip
}

Inputs

Outputs

And that’s a wrap! 🙂

Thanks for following along. Feel free to comment below with any questions/inputs.

--

--

Abdul Rafee Wahab

Tech guy. I like building cool software, & also leading others in building cool things. All views shared are my own.