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 --diff
4. 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 is Infrastructure as Code, and what problems does it solve over click-ops?
- What is the difference between declarative and imperative IaC, and where do Terraform and Ansible fall?
- What is the difference between configuration management and provisioning?
- What is Terraform, and what is the core plan/apply workflow?
- What does terraform init do?
- What is the Terraform state file, and why is it critical?
- Why should state be stored remotely, and what backend would you use on AWS?
- What is state locking, and why does it matter for teams?