Provisioner Types
Remote-Exec Provisioner
Useremote-exec to run shell commands on a newly created EC2 instance. Place the provisioner block inside your resource:
- A security group allowing SSH (
22) or WinRM (5986for Windows). - An SSH key pair created via
aws_key_pairor your preferred key management. - Correct user name for your AMI (e.g.,
ubuntu,ec2-user,admin).
Example Resources
tofu apply, you’ll see:
Local-Exec Provisioner
Thelocal-exec provisioner runs commands on your workstation or CI/CD runner where OpenTofu is executed:
tofu apply, verify the file:
Create-Time and Destroy-Time Hooks
By default, provisioners run after creation. You can also run them before destruction:Handling Provisioner Failures
By default, a failed provisioner aborts the apply and marks the resource as tainted:on_failure = "continue":
Overusing
on_failure = "continue" can hide critical bootstrap errors. Use it only when failures are non-fatal.Best Practices
- Use provisioners only as a last resort.
- Prefer native options when available:
- AWS:
user_data - Azure:
custom_data - GCP:
metadata.startup-script
- AWS:
user_data:
Using
user_data or cloud-init reduces complexity and maintains idempotency compared to provisioners.