0
0
Terraformcloud~5 mins

File provisioner in Terraform - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: File provisioner
O(n)
Understanding Time 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.

Scenario Under Consideration

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.

Identify Repeating Operations

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.
How Execution Grows With Input

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
1010 file copy operations
100100 file copy operations
10001000 file copy operations

Pattern observation: The operations increase one-to-one with the number of files.

Final Time Complexity

Time Complexity: O(n)

This means the time to copy files grows linearly as you add more files.

Common Mistake

[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.

Interview Connect

Understanding how file copying scales helps you plan deployments and avoid surprises in real projects.

Self-Check

"What if we copied all files in one batch instead of one by one? How would the time complexity change?"