How to Create a Module in Terraform: Simple Guide
To create a module in Terraform, organize your resource configurations into a separate folder with input variables and outputs, then call it from your main configuration using the
module block. This helps reuse code and manage infrastructure efficiently.Syntax
A Terraform module is a folder with configuration files. You define resources inside it. Use variables.tf for inputs and outputs.tf for outputs. To use the module, call it with a module block specifying the source folder and input values.
- module block: Calls the module and passes inputs.
- source: Path to the module folder.
- variables: Inputs the module needs.
- outputs: Values the module returns.
terraform
module "example_module" { source = "./modules/example" variable_name = "value" } // Inside modules/example/variables.tf variable "variable_name" { type = string } // Inside modules/example/main.tf resource "aws_s3_bucket" "bucket" { bucket = var.variable_name } // Inside modules/example/outputs.tf output "bucket_id" { value = aws_s3_bucket.bucket.id }
Example
This example shows a simple module that creates an AWS S3 bucket. The main configuration calls the module and passes the bucket name. The module creates the bucket and outputs its ID.
terraform
// main.tf module "s3_bucket" { source = "./modules/s3_bucket" bucket_name = "my-unique-bucket-123" } output "bucket_id" { value = module.s3_bucket.bucket_id } // modules/s3_bucket/variables.tf variable "bucket_name" { type = string } // modules/s3_bucket/main.tf resource "aws_s3_bucket" "this" { bucket = var.bucket_name } // modules/s3_bucket/outputs.tf output "bucket_id" { value = aws_s3_bucket.this.id }
Output
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs:
bucket_id = <bucket-unique-id>
Common Pitfalls
Common mistakes when creating modules include:
- Not using
variables.tffor inputs, making modules less flexible. - Hardcoding values inside the module instead of passing them as variables.
- Forgetting to output useful values for use outside the module.
- Incorrect
sourcepath when calling the module.
Always keep modules reusable and parameterized.
terraform
/* Wrong way: hardcoded bucket name inside module */ resource "aws_s3_bucket" "this" { bucket = "fixed-bucket-name" } /* Right way: use variable for bucket name */ variable "bucket_name" { type = string } resource "aws_s3_bucket" "this" { bucket = var.bucket_name }
Quick Reference
| Concept | Description |
|---|---|
| module block | Calls a module and passes inputs |
| source | Path or URL to module folder |
| variables.tf | Defines input variables for module |
| outputs.tf | Defines outputs from module |
| main.tf | Contains resource definitions inside module |
Key Takeaways
Create modules as folders with variables.tf, main.tf, and outputs.tf files.
Use the module block with source and inputs to call modules.
Pass all configurable values as variables for reusability.
Always output useful values from modules for external use.
Check source paths carefully to avoid module loading errors.