fmt, validate and TFLint
Three fast checks that need no cloud account: terraform fmt for style, terraform validate for correctness, and TFLint for mistakes the other two miss, such as an invalid AWS instance type.
By the end of this lesson you'll be able to
0 of 4 completeVerified lab: terraform fmt, both validate errors and TFLint 0.65.0 with the AWS ruleset. Run on Terraform 1.17.1; the output shown is real.
Code walkthrough: none.
Cloud lab: none; nothing here needs a cloud account.
A terraform plan against a real cloud takes time and needs credentials. Before it, three checks
run in seconds and catch most silly mistakes: formatting, invalid code and suspicious values. They
need no cloud credentials and no live infrastructure, which makes them the first jobs in every
Terraform pipeline.
What they do need, to be precise: terraform fmt needs nothing at all. terraform validate needs the
providers installed, so on a clean machine terraform init downloads them from the registry first,
and tflint --init downloads its rule plugins. After that one download (or with a provider cache),
all three run without network access.
terraform fmt
terraform fmt rewrites .tf files into the one official style: two-space indentation and aligned
equals signs. There's nothing to configure, which ends every style argument in code review.
$ cat main.tf resource "local_file" "note" { filename="${path.module}/note.txt" content = "hello" } $ terraform fmt -check; echo "exit code $?" main.tf exit code 3 $ terraform fmt -diff main.tf --- old/main.tf +++ new/main.tf @@ -1,4 +1,4 @@ resource "local_file" "note" { -filename="${path.module}/note.txt" - content = "hello" + filename = "${path.module}/note.txt" + content = "hello" } $ terraform fmt -check; echo "exit code $?" exit code 0
cat main.tfcatprint a file
terraform fmt -check; echo "exit code $?"terraforminfrastructure as codefmtformat the files
terraform fmt(no flags) fixes the files in place and lists the ones it changed.-checkchanges nothing and exits non-zero (here 3) if anything needs formatting: use it in CI.-diffshows what it changed.-recursivealso covers subfolders such asmodules/.
Most editors run fmt on save with the official HashiCorp extension. Do that, and CI never fails on style.
terraform validate
validate checks that the configuration is valid Terraform: correct syntax, real argument names
for each resource type, references that point at something that exists, and matching types. It
reads the provider schemas, so run terraform init first. It doesn't contact your cloud.
A typo in an argument name:
$ terraform validate Error: Unsupported argument on main.tf line 3, in resource "local_file" "note": 3: contents = "hello" An argument named "contents" is not expected here. Did you mean "content"?
terraform validateterraforminfrastructure as codevalidatecheck the configuration
A variable that was never declared:
$ terraform validate Error: Reference to undeclared input variable on main.tf line 3, in resource "local_file" "note": 3: content = var.message An input variable with the name "message" has not been declared. This variable can be declared with a variable "message" {} block.
terraform validateterraforminfrastructure as codevalidatecheck the configuration
After declaring variable "message":
$ terraform validate Success! The configuration is valid.
terraform validateterraforminfrastructure as codevalidatecheck the configuration
What validate can't know: whether t3.mirco is a real instance type, whether a bucket name is
free, or whether you have permission. Those are values, not structure, and need a linter or a real
plan.
TFLint
TFLint is a separate open-source linter. It adds rules about values and good practice, and plugins with rules per cloud. The lab used TFLint 0.65.0 with the AWS ruleset on this deliberately flawed file:
# .tflint.hcl plugin "aws" { enabled = true version = "0.50.0" source = "github.com/terraform-linters/tflint-ruleset-aws" }
# main.tf (excerpt) variable "unused_region" { type = string } resource "aws_instance" "web" { ami = "ami-0f58b397bc5c1f2e8" instance_type = "t3.mirco" # typo }
$ tflint --init Installing "aws" plugin... Installed "aws" (source: github.com/terraform-linters/tflint-ruleset-aws, version: 0.50.0) $ tflint --format=compact 3 issue(s) found: main.tf:16:19: Error - "t3.mirco" is an invalid value as instance_type (aws_instance_invalid_type) main.tf:1:1: Warning - terraform "required_version" attribute is required (terraform_required_version) main.tf:10:1: Warning - variable "unused_region" is declared but not used (terraform_unused_declarations)
terraform validate passes this file: instance_type is a correctly named argument holding a
string. Only TFLint knows t3.mirco isn't a real type. Without it you'd find out at apply time,
halfway through creating other resources. The two warnings come from TFLint's built-in Terraform
rules. Exit code 2 means issues were found, so CI fails.
Trivy and Checkov look at the same files for security problems, such as a public S3 bucket or SSH open to the world. They're covered in the Security lesson and run in the same CI job.
Write one script that runs every pre-plan check
Write check.sh that runs terraform fmt -check -recursive, terraform init -backend=false, terraform validate and tflint in order, and stops at the first failure. Run it on a project with a formatting problem, then fix it and run again.
check.shReveal one safe solution
$ cat > check.sh <<'EOF' #!/usr/bin/env bash set -euo pipefail terraform fmt -check -recursive terraform init -backend=false -input=false >/dev/null terraform validate tflint --init >/dev/null && tflint --format=compact EOF $ chmod +x check.sh && ./check.sh; echo "exit code $?"
cat > check.sh <<'EOF'catprint a file>write output to a file, replacing it
chmod +x check.sh && ./check.sh; echo "exit code $?"chmodchange permissions&&run the next command only if this one worked
-backend=false lets validate run without access to the real state, which is what CI wants for a
quick check. The script should stop at fmt with exit code 3 until the files are formatted, then pass
through to TFLint.
Try it yourself
0 of 5 steps doneRun each task in your own account or terminal, then tick it off.
Quick check
terraform fmt -check exits with code 3. What does it mean?
Official reading for this lesson
Or get every quiz answer right and it completes itself.
Something unclear, wrong or out of date in this lesson? Tell me, and I'll fix it.