TerraformPilot

DevOps

Fix Terraform Error: Too Many Command Line Arguments

Fix terraform too many command line arguments errors. Correct -var syntax, quote values with spaces, and learn proper Terraform CLI argument format for plan

LLuca Berton1 min read

Quick Answer

#
# ❌ Missing -var flag
terraform plan instance_type=t3.micro
 
# ✅ Use -var flag
terraform plan -var="instance_type=t3.micro"

The Error

#
Error: Too many command line arguments
 
Expected at most one positional argument (the directory to use),
but got extra arguments: "instance_type=t3.micro"

Or:

Error: Too many command line arguments
 
Usage: terraform apply [options] [PLAN]

What Causes This

#
  1. Missing flag prefixterraform plan key=value instead of terraform plan -var="key=value"
  2. Unquoted values with spaces — shell splits us-east-1 region into two arguments
  3. Extra argumentsterraform plan main.tf (Terraform doesn't take file arguments)
  4. Shell variable expansion$VAR contains spaces, splits into multiple args

Solution 1: Common Syntax Fixes

#
# ❌ Wrong: no -var flag
terraform apply instance_type=t3.micro
 
# ✅ Correct
terraform apply -var="instance_type=t3.micro"
 
# ❌ Wrong: specifying .tf files (Terraform reads all .tf files in directory)
terraform plan main.tf variables.tf
 
# ✅ Correct: just run in the directory
terraform plan
 
# ❌ Wrong: space in value without quotes
terraform apply -var=name=hello world
 
# ✅ Correct: quote the entire -var argument
terraform apply -var='name=hello world'

Solution 2: Terraform Commands That Accept Positional Arguments

#

Most Terraform commands accept zero or one positional argument:

CommandPositional ArgExample
terraform planDirectory (optional)terraform plan ./infra
terraform applyPlan file or directoryterraform apply tfplan
terraform destroyDirectory (optional)terraform destroy ./infra
terraform importAddress + IDterraform import aws_instance.web i-123
terraform state mvSource + Destinationterraform state mv aws_instance.a aws_instance.b
terraform state rmAddress(es)terraform state rm aws_instance.web

Solution 3: Multiple Variables

#
# ❌ Wrong: one -var, multiple values
terraform apply -var instance_type=t3.micro region=us-east-1
 
# ✅ Correct: separate -var for each
terraform apply \
  -var="instance_type=t3.micro" \
  -var="region=us-east-1" \
  -var="environment=prod"
 
# ✅ Better: use a var file
terraform apply -var-file="prod.tfvars"

Solution 4: Shell Quoting

#
# ❌ Shell expands $VAR before Terraform sees it
export MY_ARGS="instance_type=t3.micro"
terraform plan -var=$MY_ARGS    # Might split on spaces
 
# ✅ Quote the variable
terraform plan -var="$MY_ARGS"
 
# ❌ Complex values without proper quoting
terraform apply -var='tags={Environment=prod Team=platform}'
 
# ✅ Use HCL syntax in the value
terraform apply -var='tags={"Environment":"prod","Team":"platform"}'

Solution 5: Directory Argument

#
# Run against a different directory
terraform plan ./environments/prod
 
# ❌ Wrong: multiple directories
terraform plan ./environments/prod ./environments/staging
 
# ✅ Run them separately
terraform plan ./environments/prod
terraform plan ./environments/staging

Quick Reference: Common Commands

#
# Plan
terraform plan
terraform plan -var-file="prod.tfvars"
terraform plan -out=tfplan
 
# Apply
terraform apply
terraform apply tfplan                      # Apply saved plan
terraform apply -auto-approve               # Skip confirmation
terraform apply -var="key=value"
 
# Destroy
terraform destroy
terraform destroy -target=aws_instance.web  # Single resource
 
# Import
terraform import aws_instance.web i-0abc123
 
# State
terraform state list
terraform state show aws_instance.web
terraform state mv aws_instance.old aws_instance.new
terraform state rm aws_instance.orphan

Hands-On Courses

#

Conclusion

#

"Too many command line arguments" means Terraform received extra positional arguments it doesn't expect. Use -var= for variables (not bare key=value), don't pass .tf filenames (Terraform reads all files in the directory), and quote values with spaces.

#Terraform#Troubleshooting#DevOps#Error Fix#Infrastructure as Code

Share this article