Interview › Infrastructure as Code (Terraform, Ansible)
What is the difference between local-exec and remote-exec provisioners?
Infrastructure as Code (Terraform, Ansible) · Basic level
Answer
local-exec runs a command on the machine executing Terraform. remote-exec connects to the created resource and runs commands there. local-exec is useful for local notifications or external tooling; remote-exec is usually fragile and should be replaced with baked images, cloud-init, SSM, or configuration management when possible.
Technical explanation
local-exec depends on the runner environment, installed tools, and credentials.
remote-exec depends on network reachability, SSH/WinRM readiness, host keys, and bootstrap timing.
Both should be isolated, logged, and avoided for core lifecycle management.
Keep Terraform's ownership boundary clear: one state should own a resource or field, and other tools should consume published outputs instead of modifying it.
Use fmt, validate, linting, policy checks, plan review, and state locking before production applies.
Design for small blast radius by splitting state around lifecycle, permissions, and recovery boundaries.
Hands-on example
1. Demonstrate the risk and replacement for: What is the difference between local-exec and remote-exec provisioners?
2. Avoid this as a default pattern:
provisioner "remote-exec" {
inline = ["sudo yum install -y nginx", "sudo systemctl enable --now nginx"]
}3. Replace it with a baked AMI, user_data, SSM document, or Ansible role. For example, put bootstrap in cloud-init and detailed config in Ansible:
ansible-playbook -i inventory/aws_ec2.yml site.yml --limit tag_Role_web --check --diff4. If a provisioner remains necessary, make it small, retry-safe, logged, and documented as a temporary bridge.
Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Infrastructure as Code (Terraform, Ansible) interview questions
- What are provisioners, and why are they considered a last resort?
- What is the lifecycle block (create_before_destroy, prevent_destroy, ignore_changes)?
- What does terraform refresh do, and how has its behaviour changed?
- When would you use ignore_changes, and what is the risk?
- What is the purpose of terraform plan -out and applying a saved plan?
- What is a tainted resource, and what replaced the taint command?
- How do you structure Terraform code for multiple environments (dev/staging/prod)?
- How do you target a specific resource with terraform apply, and why is it discouraged?