Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

How to Write Reusable Terraform Modules for AWS

This guide shows how to pack your Terraform code into a module that you can use again and again.

Web Hosting2 min read8 steps2 screenshots

What is Terraform?

Terraform is a tool that builds cloud things from text files. Instead of clicking in a web page, you write what you want, and Terraform creates it. AWS (Amazon Web Services) is a cloud company that rents out servers and storage. A module is a folder of Terraform files that you can reuse, like a recipe card. You write it once and use it many times with different inputs.

Terraform modules layout

Plan the folder

A simple module has these files:

  • main.tf: the things to create.
  • variables.tf: the inputs, like blanks in a form.
  • outputs.tf: the answers you send back.
  • versions.tf: the Terraform and provider versions.

Steps

  1. Make a folder:
mkdir -p modules/s3-bucket

This creates a home for your module.

  1. Create modules/s3-bucket/variables.tf with the inputs:
variable "bucket_name" {
  description = "Name of the bucket"
  type        = string
}

variable "tags" {
  description = "Labels to add"
  type        = map(string)
  default     = {}
}

These define two inputs. The tags input is optional because it has a default.

  1. Create modules/s3-bucket/main.tf:
resource "aws_s3_bucket" "this" {
  bucket = var.bucket_name
  tags   = var.tags
}

This creates one S3 bucket, which is a storage box in AWS.

  1. Create modules/s3-bucket/outputs.tf:
output "bucket_arn" {
  value = aws_s3_bucket.this.arn
}

This shares the bucket's unique AWS ID, called an ARN.

  1. Create modules/s3-bucket/versions.tf:
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">= 5.0"
    }
  }
}

This says which AWS provider the module needs. Do not put the provider region or login details inside the module.

  1. Use the module from a root file called main.tf:
provider "aws" {
  region = "us-east-1"
}

module "logs_bucket" {
  source      = "./modules/s3-bucket"
  bucket_name = "my-unique-logs-bucket-123"
  tags        = { env = "dev" }
}

This calls the module. Call it again with other values to make more buckets.

  1. Run the tool:
terraform init
terraform plan

Init downloads what is needed. Plan shows what would be created without changing anything.

  1. Apply when the plan looks right:
terraform apply

This creates the resources. AWS may charge for them, so check your AWS bill settings.

Good habits

  • Keep a module small and focused on one job.
  • Give every variable a description and a type.
  • Use defaults only when they are safe.
  • Never put passwords or keys in module files.
  • Pin provider versions.
Tip: Run terraform fmt and terraform validate to tidy and check your code.

Quick recap

  • A module is a reusable folder of Terraform code.
    Terraform modules layout-1
  • Use variables for inputs and outputs for results.
  • Call it with a module block and a source.
  • Always run plan before apply.