1️⃣ What is the difference between Terraform and CloudFormation?
✅ Terraform: Open-source, multi-cloud, supports mutable & immutable infrastructure.
✅ CloudFormation: AWS-specific, tightly integrated with AWS services.
👉 Terraform is preferred for multi-cloud & hybrid environments.
2️⃣ How does Terraform handle state management?
👉 Terraform stores state in a .tfstate file, tracking infrastructure changes. It can be stored locally or in a remote backend like S3, Azure Blob, or Terraform Cloud for collaboration.
3️⃣ What is the purpose of a Terraform backend?
👉 A backend defines where the state file is stored. It enables:
✅ Remote state storage (e.g., S3, GCS, Azure Blob)
✅ State locking (prevents conflicts in team environments)
✅ Enhanced security & collaboration
4️⃣ How does Terraform ensure idempotency?
👉 Terraform follows a declarative approach, meaning running the same configuration multiple times results in the same infrastructure without unintended changes.
5️⃣ What is the difference between terraform plan and terraform apply?
✅ terraform plan → Shows what will change before applying.
✅ terraform apply → Executes the planned changes and modifies infrastructure.
👉 Always review terraform plan before running apply in production!
6️⃣ How do you manage sensitive data in Terraform?
👉 Use Terraform Vault, AWS Secrets Manager, or environment variables instead of hardcoding secrets in .tf files. You can also use terraform.tfvars but never commit it to Git!
7️⃣ What are Terraform modules and why use them?
👉 Modules are reusable Terraform configurations that help organize code, improve scalability, and reduce duplication.
✅ Example: A VPC module can be reused across multiple environments (dev, staging, prod).
8️⃣ What is terraform import and how does it work?
👉 terraform import allows existing infrastructure to be brought under Terraform management without recreating it.
✅ Example: terraform import aws_instance.example i-1234567890abcdef0
🚨 Note: Terraform only imports state, not configuration. You must manually create .tf files!
9️⃣ How do you handle drift detection in Terraform?
👉 Use terraform plan to detect changes made outside Terraform.
👉 Enable state locking & automation (GitOps) to prevent manual changes.
🔟 What is the difference between count and for_each in Terraform?
✅ count → Used for simple resource replication (indexed).
✅ for_each → Used when managing complex resources like maps & sets.
👉 Example:
resource "aws_s3_bucket" "example" {
for_each = toset(["dev", "staging", "prod"])
bucket = "my-bucket-${each.key}"
}
Top comments (0)