Local-exec provisioner in Terraform - Time & Space Complexity
We want to understand how the time to run local commands grows when using Terraform's local-exec provisioner.
Specifically, how does running commands on multiple resources affect total execution time?
Analyze the time complexity of the following operation sequence.
resource "null_resource" "example" {
count = var.resource_count
provisioner "local-exec" {
command = "echo Resource ${count.index} created"
}
}
This creates multiple resources, each running a local command after creation.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Running the local-exec command for each resource.
- How many times: Once per resource, so equal to the number of resources created.
Each resource triggers one local command execution, so total commands grow directly with resource count.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 local commands run |
| 100 | 100 local commands run |
| 1000 | 1000 local commands run |
Pattern observation: The number of local commands grows linearly as the number of resources increases.
Time Complexity: O(n)
This means the total time to run local commands grows directly in proportion to the number of resources.
[X] Wrong: "Running local-exec commands happens all at once, so time stays the same no matter how many resources."
[OK] Correct: Each local-exec runs separately by default, so more resources mean more commands and more total time.
Understanding how local-exec scales helps you design Terraform configurations that run efficiently and predictably as your infrastructure grows.
"What if we changed local-exec to run commands in parallel? How would the time complexity change?"
Practice
local-exec provisioner do in Terraform?Solution
Step 1: Understand provisioner types
Terraform provisioners run scripts or commands either locally or remotely after resource creation.Step 2: Identify local-exec purpose
Thelocal-execprovisioner specifically runs commands on your local machine, not inside the cloud resource.Final Answer:
Runs commands on your local machine after resource creation -> Option DQuick Check:
local-exec runs commands locally [OK]
- Confusing local-exec with remote-exec
- Thinking it runs inside the cloud resource
- Assuming it manages Terraform state files
local-exec provisioner in a Terraform resource?Solution
Step 1: Recall Terraform provisioner syntax
Provisioners use the keywordprovisionerfollowed by the provisioner type in quotes.Step 2: Check correct attribute names
Thelocal-execprovisioner usescommandto specify the shell command to run.Final Answer:
provisioner "local-exec" { command = "echo Hello" } -> Option AQuick Check:
Correct syntax uses provisioner "local-exec" and command [OK]
- Omitting quotes around local-exec
- Using wrong attribute names like run or execute
- Using underscore instead of dash in local-exec
resource "null_resource" "example" {
provisioner "local-exec" {
command = "echo Resource created"
}
}Solution
Step 1: Understand null_resource and local-exec
Thenull_resourceis a Terraform resource that supports provisioners for running commands.Step 2: Analyze the command execution
Thelocal-execprovisioner runs the command on your local machine, so it will print the message locally.Final Answer:
Terraform will print 'Resource created' on your local machine -> Option CQuick Check:
local-exec runs commands locally, so output appears locally [OK]
- Thinking output appears inside cloud resource
- Believing null_resource disallows provisioners
- Assuming provisioners are ignored silently
resource "null_resource" "fail_example" {
provisioner "local-exec" {
command = echo "Hello"
}
}
What is the cause of the error?Solution
Step 1: Check command syntax
The command value must be a string enclosed in quotes in Terraform configuration.Step 2: Identify missing quotes
Here,command = echo "Hello"lacks quotes around the entire command string, causing a syntax error.Final Answer:
Missing quotes around the command string -> Option AQuick Check:
Command must be a quoted string [OK]
- Forgetting quotes around command
- Using wrong attribute names
- Assuming null_resource disallows provisioners
setup.sh after creating an AWS EC2 instance using Terraform. Which is the best way to use local-exec provisioner to achieve this safely?Solution
Step 1: Understand local-exec vs remote-exec
Thelocal-execprovisioner runs commands on your local machine, which is not suitable for running scripts on the EC2 instance itself.Step 2: Use remote-exec for remote commands
To run a script on the EC2 instance, use theremote-execprovisioner which executes commands on the remote resource.Final Answer:
Useremote-execprovisioner to runsetup.shon the EC2 instance -> Option BQuick Check:
remote-exec runs commands on the remote resource [OK]
- Using remote-exec for a local script
- Running scripts manually instead of automating
- Adding unnecessary depends_on inside the resource
Ready to prove it?
Pass the test and this pattern moves unknown
