Why provisioners are a last resort in Terraform - Performance Analysis
We want to understand how using provisioners affects the time it takes to apply Terraform changes.
Specifically, how the number of provisioner actions grows as we add more resources.
Analyze the time complexity of using provisioners in Terraform resource creation.
resource "aws_instance" "example" {
count = var.instance_count
ami = var.ami_id
instance_type = var.instance_type
provisioner "remote-exec" {
inline = ["echo Hello World"]
}
}
This code creates multiple instances, each running a provisioner command after creation.
Each instance triggers a remote-exec provisioner after creation.
- Primary operation: Running the provisioner command on each instance.
- How many times: Once per instance, so as many times as the number of instances.
As the number of instances increases, the number of provisioner runs increases directly.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 provisioner runs |
| 100 | 100 provisioner runs |
| 1000 | 1000 provisioner runs |
Pattern observation: The number of provisioner executions grows linearly with the number of instances.
Time Complexity: O(n)
This means the total time to complete all provisioners grows directly in proportion to the number of resources.
[X] Wrong: "Provisioners run instantly and don't add noticeable time."
[OK] Correct: Each provisioner runs after resource creation and can take time, so many provisioners add up and slow down the whole process.
Understanding how provisioners scale helps you design Terraform code that runs efficiently and predictably in real projects.
"What if we replaced provisioners with configuration management tools outside Terraform? How would the time complexity change?"