How do you scan Terraform code with Checkov?

BeginnerHands-onCheckov
Answer

Short answer

Run checkov -d <terraform-folder> to scan the .tf files, or checkov -f <file> for a single file. Filter with --check/--skip-check or --framework terraform, and choose output formats like CLI, JSON, JUnit or SARIF.

Example

# main.tf
resource "aws_s3_bucket" "logs" { bucket = "shop-logs-prod" }

resource "aws_security_group" "web" {
  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]     # ❌ SSH open to the world
  }
}
checkov -d ./terraform
checkov -d ./terraform --framework terraform --compact --quiet    # failures only, short output
checkov -d ./terraform -o junitxml > checkov.xml
checkov -d ./terraform --check CKV_AWS_24,CKV_AWS_18               # only these checks
FAILED CKV_AWS_24: Ensure no security groups allow ingress from 0.0.0.0:0 to port 22
FAILED CKV_AWS_18: Ensure the S3 bucket has access logging enabled
FAILED CKV2_AWS_6: Ensure that S3 bucket has a Public Access block

Fixing

cidr_blocks = ["10.0.0.0/16"]      # or use SSM Session Manager instead of SSH
resource "aws_s3_bucket_public_access_block" "logs" {
  bucket = aws_s3_bucket.logs.id
  block_public_acls = true
  block_public_policy = true
  ignore_public_acls = true
  restrict_public_buckets = true
}

What to say in the interview

"checkov -d on the Terraform folder, with --compact and --quiet for readable CI output and JUnit or SARIF for reports. Typical fixes are restricting CIDRs, adding public access blocks, encryption and logging."

All Security Tools questions Roadmap: Checkov — IaC security

More on Checkov