Terraform Error Guide: 'Missing newline after argument' — Fix HCL Syntax and Run terraform fmt
Fix Terraform 'Missing newline after argument': two arguments share a line, a comma is missing in a list or map, or a stray token slipped in. Correct the HCL.
- #terraform
- #iac
- #troubleshooting
- #errors
Fixing errors like this? Get 500 free DevOps AI prompts
500 copy-paste AI prompts for the stack you actually run — one PDF, free.
Overview
Missing newline after argument is a pure HCL syntax error raised by Terraform’s parser during init, validate, fmt, or plan. In HCL, each argument assignment inside a block must end with a newline. When the parser finishes reading one argument and then sees more tokens on the same line — a second argument, a stray word, or an unexpected symbol — instead of the expected line break, it stops and reports that a newline was required.
The literal message points at the column where the line should have ended:
Error: Missing newline after argument
on main.tf line 5, in resource "aws_instance" "app":
5: instance_type = "t3.micro" ami = "ami-0abc123"
An argument definition must end with a newline.
Despite the name, the real problem is rarely a missing blank line — it is almost always a structural mistake that leaves extra tokens on a line: two arguments written on one line without a break, a missing comma between elements of a list or map, an unquoted value, or a stray character. The parser reports the symptom (“expected a newline here”) at the point where the surplus tokens begin.
Symptoms
terraform validate/plan/fmtfails withMissing newline after argumentand cites a file, line, and column.- The flagged line has two assignments on it, or a list/map literal that is missing a comma between elements.
terraform fmtrefuses to format the file (it cannot format code that does not parse) and may itself print the error.- The error sometimes appears alongside or instead of related parser errors like
Missing item separator,Invalid expression, orUnclosed configuration block, depending on exactly what tokens confused the parser.
Common Root Causes
- Two arguments on one line:
instance_type = "t3.micro" ami = "ami-0abc123"— the second assignment has no newline before it. - Missing comma in a list:
tags = ["a" "b"]— HCL needs["a", "b"]; without the comma the parser sees a stray token after"a". - Missing comma in a map/object literal:
{ Name = "web" Env = "prod" }— needs a comma or newline between pairs. - Stray or unquoted token: a leftover word, an unquoted string value, or a mismatched quote leaving text where a newline was expected.
- A trailing token after a value: an accidental character (extra
}, a stray=) pasted at the end of a line. - Merge artifacts: conflict markers or joined lines from a bad merge that collapsed two lines into one.
Diagnostic Workflow
Let the formatter and validator localise the syntax fault — fmt is the fastest signal because it parses the whole file:
terraform fmt # reformats valid files; prints the parse error on invalid ones
terraform validate # reports the file, line, and column of the error
Jump straight to the cited line and look for two assignments or a missing separator. Here is a resource that triggers the error in three different ways:
resource "aws_instance" "app" {
instance_type = "t3.micro" ami = "ami-0abc123" # two args on one line
tags = {
Name = "web" Env = "prod" # missing comma/newline in map
}
vpc_security_group_ids = ["sg-111" "sg-222"] # missing comma in list
}
The corrected version puts each argument on its own line and adds the missing separators:
resource "aws_instance" "app" {
instance_type = "t3.micro"
ami = "ami-0abc123"
tags = {
Name = "web"
Env = "prod"
}
vpc_security_group_ids = ["sg-111", "sg-222"]
}
Once the file parses, let the formatter normalise spacing and alignment across the whole tree:
terraform fmt -recursive
terraform validate
Example Root Cause Analysis
While hand-editing a resource, an engineer added an ami argument by typing it onto the end of the existing instance_type line instead of a new line. terraform plan failed immediately:
Error: Missing newline after argument
on main.tf line 5, in resource "aws_instance" "app":
5: instance_type = "t3.micro" ami = "ami-0abc123"
An argument definition must end with a newline.
Running terraform fmt could not fix it — the formatter only works on parseable code, and this file did not parse, so fmt re-printed the same error and pointed at the same column. Reading line 5 made the cause obvious: two arguments (instance_type and ami) shared one line, so after reading the "t3.micro" value the parser expected a newline and instead found ami. Splitting the two assignments onto separate lines resolved it, and terraform fmt -recursive then aligned the block. Root cause: two arguments written on a single line without a newline between them — a mechanical HCL syntax slip, not a provider or logic error.
Prevention Best Practices
- Put exactly one argument per line; never chain two assignments on the same line.
- Separate every element of a list, and every pair in a map/object literal, with a comma or a newline.
- Run
terraform fmt -recursive(andterraform validate) in a pre-commit hook so syntax slips are caught and auto-corrected before commit. - Use an editor with an HCL/Terraform extension that highlights syntax errors and formats on save.
- After a merge, scan for conflict markers and joined lines that can collapse two statements onto one.
- Remember that
terraform fmtcannot repair a file that does not parse — fix the structural error first, then letfmttidy formatting.
Quick Command Reference
# Fastest way to surface the parse error and format valid files
terraform fmt
terraform fmt -recursive
# Report the exact file/line/column of the syntax error
terraform validate
# Format-check in CI without modifying files
terraform fmt -check -recursive
# Re-plan once the HCL parses cleanly
terraform plan
Conclusion
Missing newline after argument is HCL’s way of saying it found extra tokens where a line should have ended. The literal cause is almost never a missing blank line — it is two arguments on one line, a missing comma in a list or map, or a stray token. Go to the cited line, give each argument its own line, add the missing separators, and then run terraform fmt -recursive to normalise the rest. Because fmt cannot format code that does not parse, fix the structure first. Wiring terraform fmt -check and terraform validate into pre-commit and CI turns these syntax slips into instant, local feedback.
Get 500 Battle-Tested DevOps AI Prompts — Free
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.