If you’re aiming for a senior DevOps or cloud architect role, you’ll likely face some tough Terraform questions. This article covers 25 advanced questions with answers written like you’d explain them in an interview. I’ve added code snippets and examples where they help. Let’s get started!
1. How do you secure a Terraform state file?
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "state/terraform.tfstate"
region = "us-east-1"
encrypt = true
dynamodb_table = "terraform-locks"
}
}
2. How do you handle Terraform state file conflicts in a team?
terraform apply, DynamoDB locks the state file so others can’t mess it up. I also set up CI/CD pipelines to run Terraform sequentially, so changes don’t overlap.3. What’s drift detection, and how do you fix it?
terraform plan to detect drift—it shows what’s different. To fix it, I either update the Terraform code to match the real state or run terraform apply to bring the infrastructure back in line. For big teams, I use tools like Terraform Cloud to monitor drift automatically.4. How do you structure Terraform modules for reusability?
modules/ folder. This makes them easy to reuse across projects.
# modules/vpc/main.tf
variable "vpc_cidr" {
default = "10.0.0.0/16"
}
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
}
5. How do you implement zero-downtime deployments with Terraform?
create_before_destroy in Terraform to make sure the new stuff is ready first.
lifecycle {
create_before_destroy = true
}
6. How do you manage multi-environment setups (dev, staging, prod)?
# environments/dev/terraform.tfvars
instance_type = "t2.micro"
7. What’s the best way to version Terraform modules?
8. How do you integrate Terraform with CI/CD pipelines?
terraform init, terraform plan, and terraform apply automatically when code is pushed. I use a plan file to review changes before applying and store the state file in a secure backend. I also add approval steps for production changes.
# GitHub Actions example
name: Terraform Apply
jobs:
terraform:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: hashicorp/setup-terraform@v2
- run: terraform init
- run: terraform plan -out=tfplan
- run: terraform apply tfplan
9. How do you handle secrets in Terraform?
data "aws_secretsmanager_secret_version" "db_password" {
secret_id = "my-db-password"
}
10. What’s the difference between `taint` and `replace` in Terraform?
terraform taint marks a resource as “broken” so Terraform recreates it on the next apply. It’s useful for forcing a refresh. terraform replace (newer in Terraform 1.0) directly tells Terraform to replace a specific resource. Replace is cleaner because it’s explicit, while taint is more of a legacy command.11. How do you debug a Terraform provider error?
terraform apply. I enable debug logging with TF_LOG=DEBUG and run it again to get detailed logs. I also verify the provider version and check the provider’s documentation or GitHub issues. If it’s a config issue, I test with a smaller setup to isolate the problem.12. How do you migrate a state file to a new backend?
terraform init, and Terraform asks if I want to copy the state file. I say yes, and it moves the state without downtime. I always back up the state file first!13. How do you use `for_each` in Terraform?
for_each lets you create multiple resources from a map or set. For example, if I need multiple EC2 instances, I define a map of instances and use for_each to loop through it. It’s cleaner than count because it ties resources to keys.
variable "instances" {
type = map
default = {
"web1" = { size = "t2.micro" }
"web2" = { size = "t2.small" }
"web3" = { size = "t3.large" }
}
}
resource "aws_instance" "web" {
for_each = var.instances
ami = "ami-12345678"
instance_type = each.value.size
}
14. How do you handle dependencies between resources?
depends_on. For example, I make sure a database is created before an app server that needs it.
resource "aws_db_instance" "db" {
# DB config
}
resource "aws_instance" "app" {
depends_on = [aws_db_instance.db]
# App config
}
15. What’s a Terraform provisioner, and when do you use it?
remote-exec provisioner to run a bash script.
provisioner "remote-exec" {
inline = ["sudo apt update", "sudo apt install nginx"]
}
16. How do you test Terraform code?
terraform fmt to enforce code style.17. How do you refactor a monolithic Terraform config?
terraform state mv to update the state file without recreating resources. I test each change with terraform plan to ensure no disruptions.18. How do you handle large-scale Terraform deployments?
19. What’s the role of `terraform import`?
terraform import brings existing resources into Terraform’s state file. For example, if someone manually created an EC2 instance, I write a resource block for it and run terraform import to manage it with Terraform. It’s handy for adopting legacy infrastructure.
terraform import aws_instance.web i-1234567890abcdef0
20. How do you optimize Terraform performance?
terraform apply -target=resource_name for quick changes. I also cache providers with terraform init -get-plugins=false in CI/CD.21. How do you use Terraform with Kubernetes?
provider "kubernetes" {
host = aws_eks_cluster.my_cluster.endpoint
cluster_ca_certificate = base64decode(aws_eks_cluster.my_cluster.certificate_authority[0].data)
}
22. How do you handle Terraform upgrades?
terraform 0.12upgrade or 0.13upgrade for major updates. I test the upgrade in a dev environment first, then update provider versions and re-run terraform init.23. What’s a data source in Terraform?
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
}
24. How do you enforce governance with Terraform?
25. How do you recover from a failed `terraform apply`
These advanced questions test your deep understanding of Terraform in real-world scenarios. Practice these, and you’ll be ready to shine in senior-level interviews. Good luck!
How did it make you feel?
