0
0
Terraformcloud~5 mins

Why provisioners run scripts on resources in Terraform - Performance Analysis

Choose your learning style9 modes available
Time Complexity: Why provisioners run scripts on resources
O(n)
Understanding Time Complexity

We want to understand how the time to run scripts on resources grows as we add more resources.

How does running scripts on many resources affect the total time?

Scenario Under Consideration

Analyze the time complexity of running provisioner scripts on multiple resources.

resource "aws_instance" "example" {
  count         = var.instance_count
  ami           = "ami-123456"
  instance_type = "t2.micro"

  provisioner "remote-exec" {
    inline = ["sudo apt-get update", "sudo apt-get install -y nginx"]
  }
}

This code creates multiple instances and runs setup scripts on each one after creation.

Identify Repeating Operations

Identify the API calls, resource provisioning, data transfers that repeat.

  • Primary operation: Running the provisioner script on each instance after it is created.
  • How many times: Once per instance, so equal to the number of instances.
How Execution Grows With Input

Each new instance adds one more script run, so the total time grows directly with the number of instances.

Input Size (n)Approx. API Calls/Operations
1010 script runs
100100 script runs
10001000 script runs

Pattern observation: The total work increases evenly as you add more instances.

Final Time Complexity

Time Complexity: O(n)

This means the time to run all scripts grows in direct proportion to the number of resources.

Common Mistake

[X] Wrong: "Running scripts on multiple resources happens all at once, so time stays the same no matter how many resources."

[OK] Correct: Each script runs on its own resource, usually one after another or in limited parallel, so total time adds up with more resources.

Interview Connect

Understanding how script runs scale helps you plan deployments and explain delays clearly in real projects.

Self-Check

"What if the provisioner scripts ran in parallel on all resources? How would the time complexity change?"