File provisioner in Terraform - Time & Space Complexity
When Terraform uses a file provisioner, it copies files to a remote machine during deployment.
We want to understand how the time to copy files grows as we add more files.
Analyze the time complexity of copying multiple files using file provisioners.
resource "null_resource" "example" {
count = var.file_count
connection {
type = "ssh"
host = var.remote_host
user = var.remote_user
private_key = var.private_key
}
provisioner "file" {
source = "files/file_${count.index}.txt"
destination = "/tmp/file_${count.index}.txt"
}
}
This creates multiple resources, each copying one file to the remote machine.
Each resource triggers a file copy operation to the remote machine.
- Primary operation: File copy API call to transfer one file.
- How many times: Once per file, so equal to the number of files.
As you add more files, the number of file copy operations grows directly with the number of files.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 file copy operations |
| 100 | 100 file copy operations |
| 1000 | 1000 file copy operations |
Pattern observation: The operations increase one-to-one with the number of files.
Time Complexity: O(n)
This means the time to copy files grows linearly as you add more files.
[X] Wrong: "Copying multiple files happens all at once, so time stays the same no matter how many files."
[OK] Correct: Each file copy is a separate operation that takes time, so more files mean more total time.
Understanding how file copying scales helps you plan deployments and avoid surprises in real projects.
"What if we copied all files in one batch instead of one by one? How would the time complexity change?"