Excellent follow-up, Pavan — you're absolutely right to push deeper here. There are several important Terraform concepts, commands, behaviors, and real-world scenarios that weren't fully covered yet, especially those like drift , state locking , partial apply , and dependency handling — all of which Amazon interviewers love to dig into at the Senior DevOps level .
Here's a list of advanced and often-missed Terraform concepts with when to use them , including the one you mentioned: drift .
1. Drift Detection
What : When real infrastructure changes outside of Terraform.
Detect via : terraform plan
Fix via :
terraform apply (reconciles)
terraform refresh (syncs state without apply)
Use When :
Resources are manually updated (e.g., EC2 type changed via console)
External scripts or autoscaling mutate infrastructure
2. State Locking
What : Prevents simultaneous terraform apply operations.
Use When :
Team collaboration — multiple users or CI pipelines
How :
Enabled by default in remote backends like:
Error Example : Error acquiring the state lock
3. Partial Apply
What : Applying only a subset of planned changes.
How :
Manually select changes during interactive terraform apply
Use When :
You want to deploy risky resources manually
4. Targeted Operations ( -target )
What : Deploy specific resource(s) only.
Use When :
Faster iteration during dev
Safe rollout of individual pieces
5. Remote Backends
What : Store state remotely (S3, Terraform Cloud, Consul).
Bonus : Can also enable remote operations (e.g., Terraform Cloud runs)
6. Custom Conditions (Pre/Post)
What : Prevent invalid configurations at runtime
Use When :
Business rules (e.g., block public S3 in prod)
What : HashiCorp’s SaaS for Terraform management
Use When :
You need remote apply, policy enforcement (Sentinel), audit logging
8. Sentinel Policies
What : Policy as Code engine in Terraform Enterprise
Use When :
Enforce org-wide policies (e.g., enforce tags, block public SGs)
Use When :
You need to:
Move resources between modules
Remove orphaned resources
10. Tainting Resources
Use When :
A resource is corrupted , but Terraform doesn't detect it as changed.
11. Plan and Apply Separation
Use When :
You want manual approval, especially in CI/CD.
Typical Flow :
terraform plan -out=tfplan
12. Custom Providers / Third-Party Providers
Use When :
You manage infra not officially supported (e.g., custom APIs, SaaS services)
Force dependency beyond reference
Resources are implicitly parallel
Manage deletion, creation timing
Prevent destroy or force replace
Use different providers in the same config
Multi-account/multi-region setups
14. Dynamic Blocks
Use When :
You have variable sets of nested blocks like IAM statement or SG ingress.
15. Expressions & Functions
Key Examples :
join(), lookup(), merge(), compact(), length(), contains()
Use When :
You’re dynamically generating values or defaults
What : Syncs state with real infra without changing infra
Use When :
You want to inspect drift but not apply changes
17. Workspaces vs. Separate State
Workspaces :
Lightweight env management (terraform workspace)
Separate State Files :
Preferred for prod-grade isolation (via different backends or directories)
Use When :
You want true separation of dev, stage, prod
18. State Versioning (e.g., S3)
Use When :
You need to rollback state due to corruption or bad apply
Tip :
Enable versioning on S3 bucket storing state
Use When :
You want cost tracking, compliance, automated scripts
20. Concurrency in CI/CD
Use When :
You want to avoid apply collisions
Tip :
Use state locking via DynamoDB
Serialize pipelines with mutex locks (e.g., terragrunt run-all with locking)
Identify and fix config drift between state and real infra
Prevent concurrent applies
Apply specific resources safely
Apply selected changes interactively
Force recreation without config change
Store state safely in S3, Terraform Cloud
Recover from state corruption or misapplies
Enforce infra rules at runtime
Handle variable nested blocks cleanly
Safer CI/CD with approval gates
Enforce critical resource ordering
DRY, orchestrate multi-environment deployments
Would you like a Markdown/PDF cheat sheet with all these?
Or maybe a diagram of Terraform architecture in a CI/CD flow , showing modules, remote state, drift detection, etc.?
Last updated 6 months ago