Thursday, 21 August 2025

Terraform

 Q1. What is Terraform?

Answer:

Terraform is an open-source Infrastructure as Code (IaC) tool developed by HashiCorp. It allows you to define infrastructure (cloud, on-prem, hybrid) using a declarative configuration language (HCL – HashiCorp Configuration Language).

---

 Q2. What are the advantages of Terraform?

Answer:

 Cloud-agnostic (works with AWS, Azure, GCP, etc.)

 Declarative & idempotent (same config → same result)

 Supports provisioning, scaling, and versioning of infrastructure

 State management with tracking of resources

 Large ecosystem (providers & modules)

---

 Q3. What is the difference between Terraform and Ansible?

Answer:

 Terraform: Declarative, used for provisioning infrastructure.

 Ansible: Procedural, used mainly for configuration management.

 Terraform handles infrastructure lifecycle, while Ansible handles software lifecycle.


---

 Q4. What is Terraform state?

Answer:

Terraform maintains a state file (`terraform.tfstate`) that records information about managed resources. It allows Terraform to know what already exists and what changes are needed.

---

 Q5. What are Terraform providers?

Answer:

Providers are plugins that allow Terraform to interact with APIs of different platforms (AWS, Azure, GCP, Kubernetes, Databases, etc.)

---

 Q6. What are Terraform modules?


Answer:

A module is a container for multiple Terraform resources used together. It promotes reusability and standardization.


 Root module (main project)

 Child modules (reusable components)

---

 Q7. What is the difference between `terraform apply` and `terraform plan`?

Answer:

 terraform plan → Shows execution plan (what changes will happen).

 terraform apply → Executes the plan and makes changes in infrastructure.


---

 Q8. How do you handle sensitive data in Terraform?

Answer:

 Use `sensitive = true` in variables.

 Store secrets in external tools (Vault, AWS Secrets Manager, Azure Key Vault).

 Never commit `.tfstate` files to version control.


---

 Q9. What are Terraform workspaces?

Answer:

Workspaces allow multiple state files for the same configuration.

E.g., `dev`, `qa`, `prod` environments using same code but different state.

---

 Q10. What is `terraform refresh` used for?

Answer:`terraform refresh` updates the state file with the real-world resource states without changing infrastructure.


---

 Q11. What are Terraform backends?

Answer:

Backends define where Terraform stores state data.

 Local backend → stores state file locally.

 Remote backend → stores in cloud (S3, Azure Blob, GCS) for team collaboration.

---

 Q12. How do you lock Terraform state?

Answer:

State locking prevents multiple users from running Terraform at the same time.

 AWS S3 + DynamoDB (lock table)

 Terraform Cloud

 Consul backend

---

 Q13. What are `provisioners` in Terraform?

Answer:

Provisioners allow you to execute scripts or actions on resources after creation.

E.g., `local-exec`, `remote-exec`.

👉 Best practice: avoid provisioners; use configuration tools (Ansible, Chef).


---

 Q14. How do you upgrade Terraform provider versions?

Answer:

 Specify version in `required_providers`.

 Run `terraform init -upgrade` to update.


---

 Q15. What is the difference between `terraform import` and `terraform taint`?

Answer:

 terraform import → Brings an existing resource under Terraform management.

 terraform taint → Marks a resource for recreation in the next apply.


---


 Q16. How do you manage dependencies between resources?


Answer:

Terraform auto-detects dependencies via references.

Explicit dependency can be defined using `depends_on`.


 Scenario-Based Questions

 Q17. You have one Terraform config but need multiple environments (dev, qa, prod). How do you handle it?

Answer:

 Use workspaces (`terraform workspace new dev`)

 Or use separate state files with variables for each environment

 Or use Terragrunt for environment orchestration


---

 Q18. What if someone accidentally deletes the Terraform state file?

Answer:

 If using remote backend (S3, Terraform Cloud) → state is safe.

 If using local backend → recover from version control or recreate state with `terraform import`.

---

 Q19. You ran `terraform apply` and it failed midway. What happens?

Answer:

Terraform performs incremental execution → resources created before the failure remain, others are not created. Next run resumes from current state.

---

 Q20. How do you roll back in Terraform?

Answer:

 Terraform doesn’t have rollback → instead you revert configuration to the previous version and run `terraform apply`.

 State snapshots (in remote backend) can also help revert.

---

 Q21. How do you ensure team collaboration in Terraform?

Answer:

 Use remote backend (S3, Terraform Cloud) with locking

 Version control (Git) for `.tf` files

 Enforce policies with Sentinel / OPA

---

No comments:

Post a Comment