Terraform Module Builder
Prerequisites & Dependencies
- Terraform CLI 1.5+ installed and configured
- Cloud provider credentials (AWS, GCP, Azure) via
~/.aws/credentials,gcloud, oraz cli - Text editor or IDE with HCL syntax highlighting
- Optional:
tflintandterraform fmtfor linting and formatting
Execution Steps
- Define the module directory structure:
variables.tf,inputs.tf,outputs.tf,main.tf, and an optionalREADME.md - Write
variables.tfwith clear input variables: names, types, defaults, and validation rules (validationblock) - Write
main.tfwith resource definitions that accept input variables and produce logical outputs - Write
outputs.tfwith descriptive output values that consumers can reference in other modules or scripts - Add a
README.mdexplaining the module's purpose, inputs, outputs, usage examples, and versioning scheme - Test the module locally:
terraform init,terraform plan, andterraform applyin a throwaway workspace - Version the module: create a
vX.Y.Zgit tag and push to a registry (Terraform Registry or private Git repo)
# variables.tf
variable "vpc_cidr" {
description = "CIDR block for the VPC"
type = string
default = "10.0.0.0/16"
}
variable "instance_type" {
description = "EC2 instance type"
type = string
default = "t3.micro"
}
# main.tf
resource "aws_vpc" "example" {
cidr_block = var.vpc_cidr
}
resource "aws_instance" "example" {
instance_type = var.instance_type
ami = data.aws_ami.default.id
vpc_id = aws_vpc.example.id
}
# outputs.tf
output "vpc_id" {
description = "ID of the created VPC"
value = aws_vpc.example.id
}
terraform init
terraform plan -var="vpc_cidr=10.1.0.0/16"
terraform apply -auto-approve