Remote-exec provisioner in Terraform - Time & Space Complexity
When using the remote-exec provisioner in Terraform, it runs commands on remote machines after they are created.
We want to understand how the time to run these commands grows as we create more machines.
Analyze the time complexity of running remote commands on multiple instances.
resource "aws_instance" "example" {
count = var.instance_count
ami = "ami-123456"
instance_type = "t2.micro"
provisioner "remote-exec" {
inline = ["echo Hello World"]
}
}
This creates multiple instances and runs a simple command on each using remote-exec.
Each instance triggers a remote command execution.
- Primary operation: Running remote-exec commands on each instance.
- How many times: Once per instance, so equal to the number of instances.
As you add more instances, the number of remote commands grows the same way.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 remote-exec commands |
| 100 | 100 remote-exec commands |
| 1000 | 1000 remote-exec commands |
Pattern observation: The number of remote commands grows directly with the number of instances.
Time Complexity: O(n)
This means the total time to run remote commands grows linearly as you add more instances.
[X] Wrong: "Running remote-exec on multiple instances happens all at once, so time stays the same no matter how many instances."
[OK] Correct: Even if commands run in parallel, each still takes time and resources. The total work grows with the number of instances.
Understanding how remote commands scale helps you design infrastructure that runs smoothly as it grows.
This skill shows you can think about how automation impacts deployment time in real projects.
"What if we changed remote-exec to run a script that itself launches multiple commands? How would the time complexity change?"