How to Use Terraform Modules - Build Reusable Infrastructure
Create and use Terraform modules for reusable infrastructure code. Covers module structure, inputs, outputs, versioning, and the Terraform Registry.
Terraform
Learn to read Terraform plan output like a pro. Understand create, update, destroy symbols, moved blocks, and how to catch issues before applying changes.
The terraform plan command is your safety net before making infrastructure changes. Understanding its output is critical for preventing costly mistakes. This guide teaches you to read plan output efficiently.
terraform plan
terraform plan -out=tfplan # Save plan to file
terraform plan -target=aws_instance.web # Plan specific resource
terraform plan -var="env=prod" # With variablesTerraform uses symbols to indicate what will happen:
| Symbol | Meaning | Color |
|---|---|---|
+ | Create | Green |
- | Destroy | Red |
~ | Update in-place | Yellow |
-/+ | Destroy and recreate | Red/Green |
<= | Read (data source) | Cyan |
Terraform will perform the following actions:
# aws_instance.web will be updated in-place
~ resource "aws_instance" "web" {
id = "i-0abc123"
~ instance_type = "t3.micro" -> "t3.small"
tags = {
"Name" = "web-server"
}
}
# aws_security_group.allow_http will be created
+ resource "aws_security_group" "allow_http" {
+ arn = (known after apply)
+ id = (known after apply)
+ name = "allow-http"
+ vpc_id = "vpc-12345"
+ ingress {
+ cidr_blocks = ["0.0.0.0/0"]
+ from_port = 80
+ protocol = "tcp"
+ to_port = 80
}
}
Plan: 1 to add, 1 to change, 0 to destroy.Plan: 2 to add, 1 to change, 3 to destroy.This tells you:
# aws_db_instance.main will be destroyed
- resource "aws_db_instance" "main" {Why it happens: Changed an attribute that forces replacement (like engine_version).
# aws_instance.web must be replaced
-/+ resource "aws_instance" "web" {
~ ami = "ami-old" -> "ami-new" # forces replacementThe # forces replacement comment is critical — it means the resource will be destroyed and recreated.
+ id = (known after apply)This is normal for computed attributes. But watch for:
~ security_groups = (known after apply)If a value you set changes to "known after apply", something may be wrong.
# Save plan
terraform plan -out=tfplan
# Review saved plan
terraform show tfplan
# Apply saved plan (no confirmation needed)
terraform apply tfplan
# JSON output for automation
terraform plan -json -out=tfplan
terraform show -json tfplanReading terraform plan output is a skill that prevents infrastructure disasters. Always review the plan summary, watch for unexpected destroys and force replacements, and save plans before applying in production.
Create and use Terraform modules for reusable infrastructure code. Covers module structure, inputs, outputs, versioning, and the Terraform Registry.
Learn the best way to organize Terraform projects. Covers file structure, modules, environments, and naming conventions for scalable infrastructure code.
Master Terraform lifecycle meta-arguments. Covers prevent_destroy, create_before_destroy, ignore_changes, and replace_triggered_by with examples.
Use Terraform dynamic blocks to eliminate repetitive nested blocks. Covers security group rules, IAM policies, and tag generation patterns.