- Welcome my DevOps blog./
- π°Posts/
- ποΈMy Trainings/
- Terraform Trainings and Certifications/
- Terraform on AWS with SRE & IaC DevOps/
- Terraform Modules/
Terraform Modules
Table of Contents
Modules can be downloaded locally from the Terraform Registry (and modified to your needs) or built from scratch.
Example s3_bucket Module #
Folder Structure #
terraform-project/
βββ main.tf
βββ variables.tf
βββ outputs.tf
βββ modules/
βββ s3_bucket/
βββ main.tf
βββ variables.tf
βββ outputs.tf
π File: modules/s3_bucket/variables.tf
variable "bucket_name" {
description = "Name of the S3 bucket"
type = string
}
variable "acl" {
description = "Access control list for the bucket"
type = string
default = "private"
}
variable "tags" {
description = "Tags to apply to the bucket"
type = map(string)
default = {}
}
π File: modules/s3_bucket/main.tf
resource "aws_s3_bucket" "this" {
bucket = var.bucket_name
acl = var.acl
tags = var.tags
}
π File: modules/s3_bucket/outputs.tf
output "bucket_id" {
description = "The ID of the bucket"
value = aws_s3_bucket.this.id
}
output "bucket_arn" {
description = "The ARN of the bucket"
value = aws_s3_bucket.this.arn
}
π File: Root Module: main.tf
module "my_bucket" {
source = "./modules/s3_bucket"
bucket_name = "my-terraform-bucket"
acl = "private"
tags = {
Environment = "Dev"
Owner = "Rob"
}
}
output "bucket_arn" {
value = module.my_bucket.bucket_arn
}
When to Provide Default Values? #
In Terraform, default values in a module’s
variables.tf file are optional, but they serve specific purposes depending on how you want your module to behave.To Make Inputs Optional
- If you want a variable to be optional for the user of the module, you must provide a default value.
- Without a default, the variable becomes required, and Terraform will throw an error if it’s not set.
variable "acl" {
description = "Access control list for the bucket"
type = string
default = "private" # Makes this optional
}
To Set Sensible Defaults
- Use defaults to provide common or recommended values so users donβt have to specify them every time.
- This improves usability and reduces boilerplate.
To Support Multiple Environments
- Defaults can help standardize behavior across environments (e.g., default tags, naming conventions).
To Avoid Breaking Changes
- When updating a module, adding a new variable without a default will break existing usage unless users update their code.
- Providing a default ensures backward compatibility.
When Not to Provide Defaults #
- If the variable is critical and must be explicitly set (e.g.,
bucket_name), omit the default to force the user to provide it. - If the value depends on external context (e.g., environment-specific settings), it’s better to require it.
Best Practice #
- Use defaults for convenience and safety.
- Avoid defaults for values that must be unique or environment-specific.
- Always document your variables with
descriptionto clarify intent.