Security Scanning & Linting
DevOps Terraform Sprint β Day 6
Integrate tflint and checkov static analysis rules to catch provider version bugs and container security flaws before deployment
Standard terraform validate only checks basic syntax, missing security vulnerabilities such as unpinned provider versions, privileged container execution, or missing encryption parameters. Today on Day 6, you will master Shift-Left Security using TFLint (linter) and Checkov (Policy-as-Code scanner) to catch flaws before code is committed or merged.
An interactive 20-minute video walkthrough for Day 6 is currently in production. Follow the step-by-step interactive playbook below!
The official reference codebase for Day 6 is hosted in the repository. Clone the repository and navigate to day-6-security-scanning to run the working solution.
git clone https://github.com/letstrnsfrm-ai/devops-terraform-roadmap.git
cd devops-terraform-roadmap/day-6-security-scanningStatic Linter Setup & Configuration (.tflint.hcl)
# Install TFLint & Checkov via Homebrew or direct binary release
brew install tflint checkov
# Fallback direct TFLint binary download (if Homebrew tap is unavailable):
curl -sL -o /tmp/tflint.zip https://github.com/terraform-linters/tflint/releases/download/v0.55.0/tflint_darwin_arm64.zip
unzip -o /tmp/tflint.zip -d /tmp/tflint-bin && sudo cp /tmp/tflint-bin/tflint /usr/local/bin/# Install TFLint via official install script
curl -s https://raw.githubusercontent.com/terraform-linters/tflint/master/install_linux.sh | bash
# Install Checkov via pip / pipx
pip3 install checkov# Install TFLint via Chocolatey or Winget
choco install tflint
# OR
winget install terraform-linters.tflint
# Install Checkov via Python pip
pip install checkov.tflint.hcl configuration:config {
format = "compact"
plugin_dir = "~/.tflint.d/plugins"
}
# Rule: Enforce module source version pinning
rule "terraform_module_pinned_source" {
enabled = true
}
# Rule: Enforce provider version constraints
rule "terraform_required_version" {
enabled = true
}
# Rule: Detect unused variable declarations
rule "terraform_unused_declarations" {
enabled = true
}# 1. Initialize TFLint plugins
tflint --init
# 2. Execute static linter checks
tflintStatic Security Scanning with Checkov
# Execute Checkov scan on current directory
checkov -d . --framework terraformCKV_DOCKER_1: Ensure container is not running as root user and CKV_TF_1: Ensure Terraform module sources are pinned).Hardening HCL Blueprints
main.tf with pinned versions and non-root security context:terraform {
required_version = ">= 1.5.0"
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "3.0.2"
}
}
}
provider "docker" {}
resource "docker_image" "hardened_nginx" {
name = "nginx:alpine"
keep_locally = false
}
resource "docker_container" "secure_app" {
name = "qe_secure_web_app"
image = docker_image.hardened_nginx.image_id
user = "1001" # Non-root container user ID
ports {
internal = 80
external = 8090
}
}Security Gate Pass Verification
# 1. Re-run TFLint (Expect 0 warnings)
tflint
# 2. Test local apply and cleanup
terraform init
terraform apply -auto-approve
curl -I http://localhost:8090
terraform destroy -auto-approve0 errors, 0 warnings returned by static security linter.Hardened container deploys cleanly with non-root user execution context.