Terraform Tutorial — Advanced Roadmap
Topics you should learn next after mastering the basics.
1. Advanced Expressions
| Expression | Purpose |
|---|---|
for_each |
Create resources from a map or set |
dynamic blocks |
Generate repeated nested blocks |
try() / can() |
Graceful error handling |
lookup() / merge() |
Map manipulation |
| Type constraints | optional(), object() with defaults |
resource "aws_security_group_rule" "ingress" {
for_each = var.ingress_rules
type = "ingress"
from_port = each.value.from_port
to_port = each.value.to_port
protocol = each.value.protocol
cidr_blocks = each.value.cidr_blocks
security_group_id = aws_security_group.main.id
}
2. Provisioners
| Type | When to Use |
|---|---|
local-exec |
Run a script on your machine after resource creation |
remote-exec |
Run a script on the remote resource via SSH |
file |
Copy files to a remote resource |
Warning: Provisioners are a last resort. Prefer user data scripts, cloud-init, or configuration management tools (Ansible, Chef).
3. Dynamic Blocks
resource "aws_security_group" "dynamic_example" {
name = "dynamic-sg"
dynamic "ingress" {
for_each = var.ingress_rules
content {
from_port = ingress.value.from_port
to_port = ingress.value.to_port
protocol = ingress.value.protocol
cidr_blocks = ingress.value.cidr_blocks
}
}
}
4. Terraform + Packer
Use Packer to build golden AMIs, then reference them in Terraform:
data "aws_ami" "custom" {
most_recent = true
owners = ["self"]
filter {
name = "name"
values = ["my-app-*"]
}
}
resource "aws_instance" "app" {
ami = data.aws_ami.custom.id
instance_type = "t3.medium"
}
5. Advanced Module Patterns
- Module composition — small, focused modules composed together
- Terraform Registry — publish and consume public modules
- Module versioning — pin module sources to tags or commits
- Testing —
terraform test(built-in), Terratest (Go-based)
6. CI/CD Integration
| Tool | Integration |
|---|---|
| GitHub Actions | hashicorp/setup-terraform action |
| GitLab CI | Terraform Docker image in pipeline |
| Jenkins | Terraform plugin or shell steps |
| Atlantis | Pull-request automation for Terraform |