Fix Terraform Error: CloudWatch Log Group Already Exists
Fix terraform CloudWatch Log Group ResourceAlreadyExistsException. Import orphaned log groups, prevent Lambda auto-creation
DevOps
Fix terraform too many command line arguments errors. Correct -var syntax, quote values with spaces, and learn proper Terraform CLI argument format for plan
# ❌ Missing -var flag
terraform plan instance_type=t3.micro
# ✅ Use -var flag
terraform plan -var="instance_type=t3.micro"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]terraform plan key=value instead of terraform plan -var="key=value"us-east-1 region into two argumentsterraform plan main.tf (Terraform doesn't take file arguments)$VAR contains spaces, splits into multiple args# ❌ 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'Most Terraform commands accept zero or one positional argument:
| Command | Positional Arg | Example |
|---|---|---|
terraform plan | Directory (optional) | terraform plan ./infra |
terraform apply | Plan file or directory | terraform apply tfplan |
terraform destroy | Directory (optional) | terraform destroy ./infra |
terraform import | Address + ID | terraform import aws_instance.web i-123 |
terraform state mv | Source + Destination | terraform state mv aws_instance.a aws_instance.b |
terraform state rm | Address(es) | terraform state rm aws_instance.web |
# ❌ 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"# ❌ 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"}'# 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# 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"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.
Fix terraform CloudWatch Log Group ResourceAlreadyExistsException. Import orphaned log groups, prevent Lambda auto-creation
Fix terraform import errors when a resource already exists in state. Covers state rm, state show, reimport workflow, import blocks
Fix terraform invalid escape sequence errors. Double backslashes for Windows paths, use heredocs for regex, and learn all valid HCL escape sequences.
Fix terraform module version constraint errors. Run terraform init -upgrade, understand version constraint syntax, pin versions for production