Scenario

Terraform Scenario-Based Questions with Answers

1️⃣ How do you copy an existing AWS resource into Terraform?

✅ Use terraform import:

terraform import aws_instance.my_instance i-0abcd12345

2️⃣ How do you prevent accidental deletion of resources?

✅ Use prevent_destroy in lifecycle rules:

resource "aws_s3_bucket" "my_bucket" {
  bucket = "my-unique-bucket"

  lifecycle {
    prevent_destroy = true
  }
}

3️⃣ How do you handle sensitive data in Terraform?

✅ Use environment variables or terraform.tfvars and vault integration.

export TF_VAR_db_password="supersecret"

✅ Use sensitive = true in outputs:


4️⃣ How do you provision multiple resources dynamically?

✅ Use count or for_each:


5️⃣ How do you rollback changes in Terraform?

✅ Use a previous state file backup:


6️⃣ How do you deploy resources in multiple environments?

✅ Use workspaces:


7️⃣ How do you reference outputs from another module?

✅ Use module.<module_name>.<output_name>:


8️⃣ How do you update a Terraform module without breaking infrastructure?

✅ Run:


9️⃣ How do you avoid storing credentials in Terraform code?

✅ Use AWS CLI credentials or Vault. ✅ Use environment variables:


🔟 How do you apply changes to a specific resource?

✅ Use -target:


1️⃣1️⃣ How do you ensure resources are created in a specific order?

✅ Use depends_on:


1️⃣2️⃣ How do you debug Terraform errors?

✅ Enable logs:


1️⃣3️⃣ How do you set default values for variables?

✅ Use default:


1️⃣4️⃣ How do you test Terraform changes before applying?

✅ Use terraform plan:


1️⃣5️⃣ How do you migrate Terraform state to S3?

✅ Configure backend:

✅ Then run:


1️⃣6️⃣ How do you dynamically get the latest AMI for EC2?

✅ Use data "aws_ami":


1️⃣7️⃣ How do you delete a specific resource without affecting others?

✅ Use terraform destroy -target:


1️⃣8️⃣ How do you use remote modules in Terraform?

✅ Use source in the module:


1️⃣9️⃣ How do you perform drift detection in Terraform?

✅ Run:

✅ Use terraform refresh to update the state file.


2️⃣0️⃣ How do you rename a resource in Terraform without recreation?

✅ Use terraform state mv:


🚀 Final Thoughts

These scenario-based questions will help you demonstrate real-world expertise in Terraform during an interview. Let me know if you need more advanced scenarios!

Last updated