Infrastructure as code · Intermediate · 25 min
Lint Terraform with TFLint and scan it for security problems with Checkov
By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · Terraform 1.17.2 · TFLint 0.66.0 · tflint-ruleset-aws 0.50.0 · Checkov 3.4.11 · pre-commit 4.7.0
You will take a deliberately flawed Terraform 1.17 configuration through terraform fmt and validate, TFLint 0.66 with the AWS ruleset and Checkov 3.4, fix what they find, suppress one finding with a written reason, and leave with a pre-commit hook that runs all three before every commit.
On this page
terraform validate tells you a configuration is valid Terraform. It does not tell you that t3.mirco is not an EC2 instance type, that a variable nobody reads is lying around, or that the security group you wrote lets the whole internet try passwords on port 22. Those mistakes surface at apply time, or worse, months later in an audit.
Two free tools close most of that gap. TFLint is a linter: it catches provider-specific errors and Terraform language smells, using plugins such as the AWS ruleset. Checkov is a policy scanner: it checks resources against hundreds of security rules, such as "no S3 bucket without a public access block" and "no unencrypted volumes".
In this tutorial you write a small AWS configuration with six deliberate problems, run terraform fmt and validate, then TFLint, then Checkov, and fix what each one finds. You suppress one Checkov finding the right way, with a reason in the code, record the remaining hardening backlog in a baseline file, and wire everything into a pre-commit hook so the problems cannot come back. Nothing is ever applied, so no AWS account is needed and nothing bills.
Everything lives in ~/iac-lint-demo, a throwaway folder you delete at the end.
Step 1 · Install TFLint, Checkov and pre-commit
You need Git for the pre-commit step, plus curl and unzip for the downloads:
sudo apt-get update
sudo apt-get install -y git curl unzip ca-certificates
terraform versionTFLint is a single Go binary. Download the release zip for your CPU, check it against the published checksums and install it:
TFLINT_VERSION=0.66.0
ARCH=$(dpkg --print-architecture)
cd "$(mktemp -d)"
curl -fsSLO "https://github.com/terraform-linters/tflint/releases/download/v${TFLINT_VERSION}/tflint_linux_${ARCH}.zip"
curl -fsSLO "https://github.com/terraform-linters/tflint/releases/download/v${TFLINT_VERSION}/checksums.txt"
sha256sum --ignore-missing -c checksums.txt
unzip -o "tflint_linux_${ARCH}.zip" tflint
sudo install -m 0755 tflint /usr/local/bin/tflint
tflint --versionA binary installed this way gets no security updates from apt. To upgrade, run the same block again with a newer TFLINT_VERSION from the TFLint releases page.
Checkov and pre-commit are Python tools. Checkov 3.4.11 lists Python 3.10 to 3.13 as supported, and Ubuntu 26.04 ships a newer Python, so install both with uv, which can fetch a matching Python for each tool and keeps each one in its own environment:
curl -LsSf https://astral.sh/uv/0.13.10/install.sh | sh
export PATH="$HOME/.local/bin:$PATH"
uv tool install --python 3.13 checkov==3.4.11
uv tool install pre-commit==4.7.0
checkov --version
pre-commit --versionThe uv installer adds ~/.local/bin to your shell profile for new terminals; the export line covers the terminal you are in. Upgrade later with uv tool upgrade checkov pre-commit.
Check it worked
terraform version prints Terraform v1.17.2 or a later 1.17 release. sha256sum prints tflint_linux_amd64.zip: OK (or arm64), and tflint --version reports TFLint version 0.66.0. checkov --version prints 3.4.11 and pre-commit --version prints pre-commit 4.7.0.
Step 2 · Write a flawed configuration and run fmt and validate
The configuration describes one web server: an S3 bucket for assets, a security group, an EC2 instance and a data volume. It contains six problems on purpose, and one of the files is badly formatted. Do not copy these patterns into real work.
Start with the version pins and the variables:
mkdir -p ~/iac-lint-demo
cd ~/iac-lint-demo
cat > versions.tf <<'HCL'
terraform {
required_version = "~> 1.17"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.74"
}
}
}
provider "aws" {
region = var.region
}
HCL
cat > variables.tf <<'HCL'
variable "region" {
type = string
description = "AWS Region to deploy into."
default = "eu-west-2"
}
variable "ami_id" {
type = string
description = "AMI ID for the web server."
}
variable "environment" {
type = string
description = "Environment name, such as staging or production."
default = "staging"
}
HCLNow the resources. storage.tf is the badly formatted file, and its public access block turns every protection off:
cat > storage.tf <<'HCL'
resource "aws_s3_bucket" "assets" {
bucket = "lint-demo-assets-replace-me"
}
resource "aws_s3_bucket_public_access_block" "assets" {
bucket = aws_s3_bucket.assets.id
block_public_acls = false
block_public_policy = false
ignore_public_acls = false
restrict_public_buckets = false
}
HCL
cat > network.tf <<'HCL'
resource "aws_security_group" "web" {
name = "lint-demo-web"
description = "Web server for the lint demo"
}
resource "aws_vpc_security_group_ingress_rule" "ssh" {
security_group_id = aws_security_group.web.id
description = "SSH"
ip_protocol = "tcp"
from_port = 22
to_port = 22
cidr_ipv4 = "0.0.0.0/0"
}
resource "aws_vpc_security_group_ingress_rule" "http" {
security_group_id = aws_security_group.web.id
description = "HTTP from anywhere"
ip_protocol = "tcp"
from_port = 80
to_port = 80
cidr_ipv4 = "0.0.0.0/0"
}
HCL
cat > compute.tf <<'HCL'
resource "aws_instance" "web" {
ami = var.ami_id
instance_type = "t3.mirco"
vpc_security_group_ids = [aws_security_group.web.id]
root_block_device {
encrypted = false
}
}
resource "aws_ebs_volume" "data" {
availability_zone = aws_instance.web.availability_zone
size = 8
encrypted = false
}
resource "aws_volume_attachment" "data" {
device_name = "/dev/sdf"
volume_id = aws_ebs_volume.data.id
instance_id = aws_instance.web.id
}
HCLThe problems: an instance type with a typo, an unused environment variable, a bucket with public access allowed, SSH open to 0.0.0.0/0, an unencrypted root volume and an unencrypted data volume.
Check the formatting first. -check reports files that need changes without touching them, and -diff shows what would change:
terraform fmt -check -diff || echo "terraform fmt exit status: $?"It names storage.tf and shows the indentation and alignment it wants. Let it fix the file, then initialise and validate. terraform init downloads the AWS provider, which is well over 100 MB; on metered or prepaid mobile data, set TF_PLUGIN_CACHE_DIR to a folder such as ~/.terraform.d/plugin-cache (create it first) so later projects reuse the download.
terraform fmt
terraform fmt -check
terraform init -backend=false
terraform validateCheck it worked
The first terraform fmt -check -diff lists storage.tf and ends with a non-zero exit status. After terraform fmt, the second -check prints nothing. terraform validate prints Success! The configuration is valid. That is the point: all six problems are still there, and Terraform's own check passes. validate confirms the configuration is internally consistent; it does not know which instance types AWS sells or what a safe security group looks like.
Step 3 · Configure TFLint and run it
TFLint reads .tflint.hcl in the current folder. The Terraform language ruleset is built into TFLint and uses its recommended preset unless you say otherwise; the AWS ruleset is a plugin you name and pin. Write the configuration:
cat > .tflint.hcl <<'HCL'
tflint {
required_version = ">= 0.66"
}
plugin "terraform" {
enabled = true
preset = "recommended"
}
plugin "aws" {
enabled = true
version = "0.50.0"
source = "github.com/terraform-linters/tflint-ruleset-aws"
}
# Not in the recommended preset: every variable must say what it is for.
rule "terraform_documented_variables" {
enabled = true
}
HCLPinning version means everyone on the team, and CI, runs the same rules. Download the plugin, then lint:
tflint --init
tflint || echo "tflint exit status: $?"tflint --init asks the GitHub API for the release, which allows 60 unauthenticated requests an hour per IP address. On a shared network or in CI, set a GITHUB_TOKEN environment variable to avoid that limit. After the first download the plugin sits in ~/.tflint.d/plugins and works offline.
Read the two findings:
aws_instance_invalid_typeis an error. The AWS ruleset carries the list of instance types from the AWS SDK, andt3.mircois not on it.applywould have failed on this after Terraform had already created the bucket and security group.terraform_unused_declarationsis a warning. Nothing readsvar.environment. Unused variables mislead the next person, who sets them and wonders why nothing changes.
TFLint exits with status 2 when it finds issues, 1 when it cannot run, and 0 when the configuration is clean, so a script or hook can stop on it.
Fix both. Rewrite compute.tf with the right instance type, keeping the volume problems for Checkov:
cat > compute.tf <<'HCL'
resource "aws_instance" "web" {
ami = var.ami_id
instance_type = "t3.micro"
vpc_security_group_ids = [aws_security_group.web.id]
root_block_device {
encrypted = false
}
}
resource "aws_ebs_volume" "data" {
availability_zone = aws_instance.web.availability_zone
size = 8
encrypted = false
}
resource "aws_volume_attachment" "data" {
device_name = "/dev/sdf"
volume_id = aws_ebs_volume.data.id
instance_id = aws_instance.web.id
}
HCL
cat > variables.tf <<'HCL'
variable "region" {
type = string
description = "AWS Region to deploy into."
default = "eu-west-2"
}
variable "ami_id" {
type = string
description = "AMI ID for the web server."
}
HCL
tflint
echo "tflint is clean"If TFLint is wrong about a line, for example a brand-new instance type the ruleset does not know yet, add an annotation on the line above it. TFLint reads everything after tflint-ignore: as a comma-separated list of rule names, so a reason written on the same line becomes a rule name that matches nothing and the issue is not suppressed. Put the reason on its own comment line instead:
# New instance type; remove when tflint-ruleset-aws knows it
# tflint-ignore: aws_instance_invalid_type
instance_type = "m9g.large"Here m9g.large stands in for whatever new type you need. Remove both comment lines when the next ruleset release adds the type.
Check it worked
The first tflint run reports two issues, something like "t3.mirco" is an invalid value as instance_type (aws_instance_invalid_type) in compute.tf and variable "environment" is declared but not used (terraform_unused_declarations) in variables.tf, and prints tflint exit status: 2. After the fix, tflint prints nothing and the shell prints tflint is clean.
Step 4 · Scan with Checkov and read the findings one by one
Checkov does not need terraform init or credentials; it parses the .tf files directly and skips the .terraform folder. Run a full scan, limited to the Terraform framework so it does not also try to scan other file types in the folder:
checkov -d . --framework terraform --compact --quiet || echo "checkov exit status: $?"The output starts with a summary line of passed, failed and skipped counts, then one block per failed check. Expect a long list: Checkov's rules cover hardening as well as outright mistakes, so a first scan of any real configuration is noisy. Do not try to read it top to bottom. Pick the findings that match real risk first. Re-run with only the checks that cover the deliberate problems:
checkov -d . --framework terraform --compact \
--check CKV_AWS_3,CKV_AWS_8,CKV_AWS_24,CKV_AWS_53,CKV_AWS_54,CKV_AWS_55,CKV_AWS_56,CKV_AWS_79,CKV_AWS_260 \
|| echo "checkov exit status: $?"Each block names the check ID and title, the resource, the file and line range, and a link to a guide page. Read them in groups:
- CKV_AWS_53, 54, 55 and 56 on
aws_s3_bucket_public_access_block.assets: each of the four settings that keep a bucket private isfalse. With all four off, one mistaken ACL or bucket policy makes the bucket world-readable. - CKV_AWS_24 on
aws_vpc_security_group_ingress_rule.ssh: port 22 is open to0.0.0.0/0. Every SSH scanner on the internet can reach the server. - CKV_AWS_8 on
aws_instance.weband CKV_AWS_3 onaws_ebs_volume.data: the root and data volumes are not encrypted at rest, and snapshots of them are not either. - CKV_AWS_79 on
aws_instance.web: the instance allows the old version of the metadata service, which a server-side request forgery bug can use to steal the instance's credentials. You did not plant this one; Checkov found it because the instance has nometadata_optionsblock. - CKV_AWS_260 on
aws_vpc_security_group_ingress_rule.http: port 80 is open to0.0.0.0/0. For a public website that is the intended behaviour, so this one is a decision, not a bug.
Check it worked
The first scan ends with checkov exit status: 1 and a summary showing many failed checks. The filtered scan shows FAILED for resource: lines for all nine checks listed above, against the resources named in the list, and also exits with status 1.
Step 5 · Fix the findings and suppress one with a reason
Fix the bucket by turning all four protections on:
cat > storage.tf <<'HCL'
resource "aws_s3_bucket" "assets" {
bucket = "lint-demo-assets-replace-me"
}
resource "aws_s3_bucket_public_access_block" "assets" {
bucket = aws_s3_bucket.assets.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
HCLEncrypt both volumes and require the newer metadata service (IMDSv2) on the instance:
cat > compute.tf <<'HCL'
resource "aws_instance" "web" {
ami = var.ami_id
instance_type = "t3.micro"
vpc_security_group_ids = [aws_security_group.web.id]
metadata_options {
http_tokens = "required"
}
root_block_device {
encrypted = true
}
}
resource "aws_ebs_volume" "data" {
availability_zone = aws_instance.web.availability_zone
size = 8
encrypted = true
}
resource "aws_volume_attachment" "data" {
device_name = "/dev/sdf"
volume_id = aws_ebs_volume.data.id
instance_id = aws_instance.web.id
}
HCLFor SSH, take the allowed address range from a variable that refuses 0.0.0.0/0:
cat > variables.tf <<'HCL'
variable "region" {
type = string
description = "AWS Region to deploy into."
default = "eu-west-2"
}
variable "ami_id" {
type = string
description = "AMI ID for the web server."
}
variable "admin_cidr" {
type = string
description = "The one address range allowed to reach SSH, such as your office IP as a /32."
validation {
condition = can(cidrhost(var.admin_cidr, 0)) && var.admin_cidr != "0.0.0.0/0"
error_message = "admin_cidr must be a valid CIDR range and must not be 0.0.0.0/0."
}
}
HCLThe HTTP rule stays open on purpose, so suppress CKV_AWS_260 on that one resource. The comment goes inside the resource block, and the text after the second colon is the reason, which Checkov prints in its report. Write a reason a reviewer can check, not "false positive":
cat > network.tf <<'HCL'
resource "aws_security_group" "web" {
name = "lint-demo-web"
description = "Web server for the lint demo"
}
resource "aws_vpc_security_group_ingress_rule" "ssh" {
security_group_id = aws_security_group.web.id
description = "SSH from the admin range only"
ip_protocol = "tcp"
from_port = 22
to_port = 22
cidr_ipv4 = var.admin_cidr
}
resource "aws_vpc_security_group_ingress_rule" "http" {
#checkov:skip=CKV_AWS_260:Public website. Nginx on port 80 only redirects to HTTPS.
security_group_id = aws_security_group.web.id
description = "HTTP from anywhere, redirected to HTTPS"
ip_protocol = "tcp"
from_port = 80
to_port = 80
cidr_ipv4 = "0.0.0.0/0"
}
HCLThe skip covers one check on one resource. If someone later adds a second rule that opens port 80, Checkov flags it again. A --skip-check CKV_AWS_260 flag on the command line would silence it everywhere, which is why the inline comment is the better tool here.
Give admin_cidr a value so a plan would work. 203.0.113.10/32 is a documentation address; replace it with your own:
cat > terraform.tfvars <<'HCL'
# Replace these with your own values before running plan.
ami_id = "ami-0123456789abcdef0"
admin_cidr = "203.0.113.10/32"
HCL
terraform fmt -check
terraform validate
tflint
checkov -d . --framework terraform --compact \
--check CKV_AWS_3,CKV_AWS_8,CKV_AWS_24,CKV_AWS_53,CKV_AWS_54,CKV_AWS_55,CKV_AWS_56,CKV_AWS_79,CKV_AWS_260Check it worked
terraform validate still prints Success! and tflint prints nothing. The filtered Checkov scan exits with status 0: the eight fixed checks show PASSED for resource: lines, and CKV_AWS_260 shows SKIPPED for resource: aws_vpc_security_group_ingress_rule.http with your reason on a Suppress comment: line under it.
Step 6 · Record the remaining findings in a baseline
Run the full scan again. The six planted problems and the metadata service finding are gone, but a list remains: things like S3 access logging and versioning, customer-managed KMS keys, and detailed monitoring on the instance. These are worth doing, and some cost money, so they belong in a planned piece of work, not in this change.
A baseline file lets you start enforcing Checkov today without fixing that backlog first. It records every current failure by file, resource and check ID; later scans with --baseline report only failures that are not in it:
checkov -d . --framework terraform --compact --quiet || echo "checkov exit status: $?"
checkov -d . --framework terraform --quiet --create-baseline > /dev/null || echo "baseline written, checkov exit status: $?"
head -n 20 .checkov.baseline
checkov -d . --framework terraform --compact --quiet --baseline .checkov.baseline
echo "no new findings"Commit .checkov.baseline with the code and treat it as a to-do list: when you fix an item, regenerate the file so it shrinks. Never regenerate it to make a new finding go away; that hides the finding with no reason attached, which is the problem the inline skip comment avoids.
Check it worked
The first full scan still exits with status 1. .checkov.baseline is JSON with a failed_checks list, grouped by file, naming each resource and its check IDs. The scan with --baseline prints no failed checks and the shell prints no new findings.
Step 7 · Run all three before every commit with pre-commit
pre-commit runs a list of hooks when you run git commit and stops the commit if any of them fails. Because every tool here is already installed, use local hooks with language: unsupported, the newer name for system in pre-commit 4: pre-commit runs the command as it is on your PATH and downloads nothing.
cat > .pre-commit-config.yaml <<'YAML'
repos:
- repo: local
hooks:
- id: terraform-fmt
name: terraform fmt
entry: terraform fmt -check -diff
language: unsupported
files: \.tf$
pass_filenames: false
- id: terraform-validate
name: terraform validate
entry: terraform validate -no-color
language: unsupported
files: \.tf$
pass_filenames: false
- id: tflint
name: tflint
entry: tflint --format compact
language: unsupported
files: \.tf$
pass_filenames: false
- id: checkov
name: checkov
entry: checkov -d . --framework terraform --compact --quiet --baseline .checkov.baseline
language: unsupported
files: \.tf$
pass_filenames: false
YAML
cat > .gitignore <<'TXT'
.terraform/
*.tfstate
*.tfstate.*
TXTfiles: \.tf$ means a hook runs only when a commit touches a .tf file, and pass_filenames: false makes each tool check the whole folder, because Terraform validates a module as a whole, not file by file. The validate hook needs the .terraform folder, so anyone who clones the repository runs terraform init -backend=false once first.
Make the folder a Git repository with a throwaway identity, install the hook and run it over everything once:
git init -q -b main
git config user.name "Lint Demo"
git config user.email "lint-demo@example.com"
pre-commit install
git add .
pre-commit run --all-files
git commit -q -m "Add web server configuration with lint checks"
git log --onelineNow try to sneak a new problem in. Someone opens Remote Desktop to the world "just for a support call":
cat > rdp.tf <<'HCL'
resource "aws_vpc_security_group_ingress_rule" "rdp" {
security_group_id = aws_security_group.web.id
description = "Temporary RDP for support"
ip_protocol = "tcp"
from_port = 3389
to_port = 3389
cidr_ipv4 = "0.0.0.0/0"
}
HCL
git add rdp.tf
git commit -m "Open RDP for support" || echo "commit refused, exit status: $?"Checkov flags CKV_AWS_25 (port 3389 open to 0.0.0.0/0) on the new resource, which is not in the baseline, and the commit does not happen. Remove the file:
git rm -q -f rdp.tf
git status --short
pre-commit run --all-filesCheck it worked
pre-commit run --all-files prints four lines, terraform fmt, terraform validate, tflint and checkov, each ending in Passed, and git log --oneline shows one commit. The RDP commit prints the checkov hook as Failed, with a FAILED for resource: aws_vpc_security_group_ingress_rule.rdp block for CKV_AWS_25, then commit refused, exit status: 1. After git rm, git status --short prints nothing and all four hooks pass again.
Next steps
Nothing here was applied, so there is nothing to destroy in AWS. Remove the demo folder and only the AWS ruleset this tutorial downloaded, which leaves any other TFLint plugins you have in ~/.tflint.d alone:
cd ~
rm -rf ~/iac-lint-demo
rm -rf ~/.tflint.d/plugins/github.com/terraform-linters/tflint-ruleset-awsOptional, and only if you installed these tools for this tutorial. The next block removes TFLint, Checkov, pre-commit and uv itself. Skip it, or delete the lines you need, if you already used any of them before, for example pre-commit from Run linters and checks before every commit with pre-commit. pre-commit clean empties pre-commit's hook store in ~/.cache/pre-commit, uv python uninstall 3.13 removes the Python that uv fetched for Checkov, and uv cache clean deletes the downloaded wheels, often several hundred MB, which is worth reclaiming on a metered or prepaid connection:
pre-commit clean
uv tool uninstall checkov pre-commit
uv python uninstall 3.13
uv cache clean
rm -f ~/.local/bin/uv ~/.local/bin/uvx ~/.local/bin/env ~/.local/bin/env.fish
sudo rm /usr/local/bin/tflintThe uv installer also added a line like . "$HOME/.local/bin/env" to your shell profile (~/.profile, ~/.bashrc or ~/.zshrc). Open that file and delete the line by hand; it is left out of the block above because the right file depends on your shell. Terraform, installed as a prerequisite, is left in place on purpose.
Run the same three commands in CI as well as in the hook, because a hook only protects people who installed it, and anyone can skip it with git commit --no-verify. Then:
- Plan Relay changes in pull requests with GitHub Actions is the natural place to add a TFLint and Checkov job next to the plan.
- Run linters and checks before every commit with pre-commit covers pre-commit itself in depth: pinned remote hooks,
autoupdateand running it in CI. - Lay out the Relay OpenTofu project builds a real AWS project you can point both linters at; TFLint and Checkov read OpenTofu files the same way.
- Validate Kubernetes manifests before they reach a cluster applies the same idea to Kubernetes YAML.
Related tutorials
- Turn the Relay network into a reusable OpenTofu moduleYou will move Relay's VPC, subnets, routes and web security group into a local modules/network module with its own inputs, outputs and README, and adopt the 18 running objects with moved blocks so the plan destroys nothing.
- Test the Relay network module with tofu test and mock providersYou will have a tofu test suite for Relay's network module that checks subnet ranges, input validation and outputs against a mocked AWS provider, and passes in seconds with no credentials.
- Store the Relay state in S3 with locking and OpenTofu state encryptionYou will create a state bucket from a separate bootstrap configuration, move the Relay state into it with S3 locking, and encrypt that state and every saved plan with OpenTofu's own encryption, ready to switch to AWS KMS.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials