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?"
Practice
remote-exec provisioner in Terraform?Solution
Step 1: Understand the role of provisioners
Provisioners in Terraform run scripts or commands on resources after creation.Step 2: Identify what remote-exec does
The remote-exec provisioner connects to the remote machine and runs commands there.Final Answer:
To run commands on a remote machine after it is created -> Option AQuick Check:
remote-exec runs remote commands = D [OK]
- Confusing remote-exec with resource creation
- Thinking remote-exec manages local files
- Mixing remote-exec with state management
remote-exec provisioner inside a Terraform resource?Solution
Step 1: Recall Terraform provisioner syntax
Provisioners are defined with the keywordprovisionerfollowed by the provisioner type in quotes.Step 2: Check the correct block structure for remote-exec
The remote-exec provisioner usesinlineto run commands directly.Final Answer:
provisioner "remote-exec" { inline = ["echo Hello"] } -> Option CQuick Check:
Correct provisioner block syntax = B [OK]
- Using underscores instead of hyphens in provisioner name
- Omitting quotes around provisioner type
- Using wrong block names like exec_remote
provisioner "remote-exec" {
inline = ["sudo apt-get update", "sudo apt-get install -y nginx"]
connection {
type = "ssh"
user = "ubuntu"
private_key = file("~/.ssh/id_rsa")
host = self.public_ip
}
}What will happen when this runs successfully?
Solution
Step 1: Understand remote-exec with connection block
The provisioner connects via SSH using the given user, key, and host to run commands remotely.Step 2: Analyze the inline commands
The commands update package lists and install nginx on the remote EC2 instance.Final Answer:
The EC2 instance will update packages and install nginx via SSH -> Option BQuick Check:
remote-exec runs commands remotely = C [OK]
- Thinking commands run locally
- Assuming private_key cannot be a file
- Expecting automatic reboot after install
provisioner "remote-exec" {
inline = ["sudo apt-get update", "sudo apt-get install -y nginx"]
connection {
type = "ssh"
user = "ubuntu"
password = "mypassword"
host = self.public_ip
}
}After running Terraform apply, you get a connection timeout error. What is the most likely cause?
Solution
Step 1: Understand connection timeout causes
Timeout usually means network connection to SSH port 22 is blocked or unreachable.Step 2: Check common network issues
If the EC2 security group lacks inbound SSH rules, connection fails despite correct credentials.Final Answer:
The EC2 instance's security group does not allow SSH (port 22) inbound -> Option AQuick Check:
Timeout = network block on SSH port 22 [OK]
- Blaming inline command syntax for connection errors
- Assuming password auth is always unsupported
- Confusing public and private IP for SSH host
remote-exec to run a script on multiple newly created instances in a loop. Which approach is best to ensure commands run on each instance correctly?Solution
Step 1: Understand resource loops with provisioners
Usingfor_eachcreates multiple resource instances, each with its own provisioner execution.Step 2: Why per-resource provisioner is best
Provisioners inside each resource run commands on that specific instance, ensuring correct targeting.Final Answer:
Use a for_each on the resource and include a remote-exec provisioner inside each resource block -> Option DQuick Check:
Provisioner per resource instance = A [OK]
- Trying to run remote-exec on all IPs in one provisioner
- Using null_resource without proper triggers
- Using local-exec instead of remote-exec for remote commands
Ready to prove it?
Pass the test and this pattern moves unknown
