Terraform Tutorial — Terraform Registry Modules
The Terraform Registry hosts thousands of pre-built modules. Instead of writing everything from scratch, you can use community and HashiCorp-verified modules.
Finding Modules
- Browse registry.terraform.io/browse/modules
- Search by provider or keyword (e.g., “aws vpc”)
- Look for the Verified badge — maintained by HashiCorp or the provider
Popular AWS Modules
| Module | Source | Purpose |
|---|---|---|
| VPC | terraform-aws-modules/vpc/aws |
Complete VPC with subnets, NAT, routes |
| EKS | terraform-aws-modules/eks/aws |
Kubernetes cluster on AWS |
| S3 Bucket | terraform-aws-modules/s3-bucket/aws |
S3 with policies and lifecycle rules |
| Security Group | terraform-aws-modules/security-group/aws |
Flexible SG rules |
| RDS | terraform-aws-modules/rds/aws |
Managed database |
Using a Registry Module
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.4.0" # ← always pin the version
name = "production"
cidr = "10.0.0.0/16"
azs = ["us-east-1a", "us-east-1b", "us-east-1c"]
private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]
enable_nat_gateway = true
single_nat_gateway = true
tags = {
Environment = "production"
Terraform = "true"
}
}
Version Constraints
| Constraint | Meaning |
|---|---|
"5.4.0" |
Exact version |
"~> 5.4" |
>= 5.4.0, < 5.5.0 (patch updates only) |
">= 5.0" |
Any version 5.0 or newer |
">= 5.0, < 6.0" |
Major version 5 only |
💡 Tip: Use
~>for production. It allows patch updates (bug fixes) but prevents breaking changes.
Local vs Registry Modules
| Aspect | Local Module | Registry Module |
|---|---|---|
| Source | "./modules/vpc" |
"terraform-aws-modules/vpc/aws" |
| Versioning | Git tags / manual | Built-in version attribute |
| Maintenance | Your team | Community / HashiCorp |
| Customization | Full control | Limited to exposed variables |
| Trust | You wrote it | Review the code first |
What’s Next?
Practice building and using modules with the exercises.