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?"
Practice
file provisioner in Terraform?Solution
Step 1: Understand the role of the file provisioner
The file provisioner is designed to transfer files from your local computer to a remote machine during Terraform resource setup.Step 2: Differentiate from other provisioners
Unlike remote-exec which runs commands, file provisioner only copies files, not executes commands or manages resource lifecycle.Final Answer:
To copy files from your local machine to a remote server during resource creation -> Option DQuick Check:
File provisioner copies files = B [OK]
- Confusing file provisioner with remote-exec
- Thinking it creates or destroys resources
- Assuming it runs commands on the server
file provisioner in a Terraform resource block?Solution
Step 1: Recall correct provisioner block syntax
The provisioner block requires the provisioner type in quotes, e.g., "file", and usessourceanddestinationkeys.Step 2: Identify correct attribute names
The file provisioner usessourceanddestination, notsrc/destor unquoted provisioner type.Final Answer:
provisioner "file" { source = "./app.conf" destination = "/etc/app.conf" } -> Option BQuick Check:
Use quotes and source/destination keys = A [OK]
- Omitting quotes around provisioner type
- Using incorrect attribute names like src/dest
- Using wrong provisioner type like "copy"
provisioner "file" {
source = "./setup.sh"
destination = "/tmp/setup.sh"
}
provisioner "remote-exec" {
inline = ["chmod +x /tmp/setup.sh", "/tmp/setup.sh"]
}What will happen after Terraform applies this resource?
Solution
Step 1: Understand file provisioner behavior
The file provisioner copies the local filesetup.shto the remote path/tmp/setup.sh.Step 2: Understand remote-exec provisioner behavior
The remote-exec provisioner runs commands on the remote server, here changing permissions and executing the script.Final Answer:
The setup.sh script is copied to /tmp and then executed with execute permission -> Option AQuick Check:
File copies + remote-exec runs commands = A [OK]
- Thinking file provisioner runs commands
- Believing file and remote-exec cannot be combined
- Assuming script runs locally instead of remotely
provisioner "file" {
source = "./config.yaml"
destination = "/etc/config.yaml"
connection {
type = "ssh"
user = "ubuntu"
password = "mypassword"
}
}Terraform apply fails with a connection error. What is the most likely cause?
Solution
Step 1: Check connection block placement
In Terraform, theconnectionblock must be defined at the resource level, not inside the provisioner block.Step 2: Understand connection block scope
Placing connection inside provisioner causes Terraform to not find connection details, leading to connection errors.Final Answer:
The connection block is inside the provisioner block, but it should be at the resource level -> Option CQuick Check:
Connection block at resource level, not inside provisioner = D [OK]
- Putting connection block inside provisioner
- Assuming password auth is always unsupported
- Mistaking file path or destination format as cause
./scripts folder to the remote server's /usr/local/bin directory using the file provisioner. Which approach is best to achieve this efficiently?Solution
Step 1: Understand file provisioner folder copying
The file provisioner natively supports copying entire folders recursively by specifying a folder as source and a directory as destination.Step 2: Differentiate efficiency of approaches
Use a single file provisioner withsource = "./scripts"anddestination = "/usr/local/bin"to copy the whole folder directly copies the directory contents to /usr/local/bin efficiently without extra compression or multiple blocks.Final Answer:
Use a single file provisioner withsource = "./scripts"anddestination = "/usr/local/bin"to copy the whole folder -> Option AQuick Check:
File provisioner copies dir recursively = A [OK]
- Thinking file provisioner cannot copy folders recursively
- Using multiple provisioner blocks unnecessarily
- Resorting to compression when direct recursive copy works
Ready to prove it?
Pass the test and this pattern moves unknown
