Securing Terraform with Checkov: My First Governance Hardening Pass
Securing Terraform with Checkov: My First Governance Hardening Pass
๐ Context: As part of my DevSecOps learning journey (Epic 21: Governance & Policy as Code), I wanted to enforce security best practices in a live Terraform project. The goal was to use Checkov to scan my Infrastructure as Code (IaC) and apply necessary fixes to improve compliance, visibility, and confidence in what I was deploying.
๐งฑ Scenario Before Scan
I began with a basic Terraform setup that provisioned:
- A VPC
- A public subnet
- A security group allowing SSH
- A single EC2 instance with default settings
๐จ Initial Risks Detected by Checkov
1
checkov -d .
Results:
- โ Passed Checks: 9
- โ Failed Checks: 11
Examples:
- SSH open to
0.0.0.0/0
- Egress open to all traffic
- No
description
fields - EBS not encrypted
- No CloudWatch monitoring
- IMDSv1 enabled
- No IAM role for EC2
- Public IP assigned automatically
- VPC Flow Logs disabled
๐ง Fixes Made (Batch-Wise)
๐ Batch 1: Security Group Tightening
- Allowed SSH only from trusted IP
- Restricted egress to port 443 (HTTPS)
- Added
description
fields to rules
๐พ Batch 2: EC2 Instance Hardening
- Enabled CloudWatch monitoring
- Encrypted EBS root volume
- Enforced IMDSv2
- EBS optimization enabled
- Commented
iam_instance_profile
(will add later)
๐ Batch 3: Network & VPC Adjustments
- Disabled
map_public_ip_on_launch
- Deferred VPC Flow Logs setup
๐งช Terraform Workflow Used
1
2
terraform plan -out secure.plan
terraform apply secure.plan
โ Avoided blind changes โ DevSecOps habit formed!
๐ After Fixes: Checkov Re-scan
1
checkov -d .
Result:
- โ Passed: 17
- โ Failed: 3 (deferred by design)
Remaining issues:
- IAM role missing โ planned in Task 21.5
- VPC flow logs โ Task 21.7
- Default security group โ not used
๐ง Key Learnings
- Policy-as-Code reveals hidden risks
- IMDSv2 + encryption = huge upgrade
- Organizing fixes into batches = clarity
terraform plan -out
= must-use- DevSecOps = awareness + tooling + design
๐ Whatโs Next?
- Write custom OPA policy for EBS encryption
- Checkov scan for S3 bucket policies
- Add IAM role with least privilege
- Enable VPC Flow Logs with IAM
- Blog each fix as a security pattern
Thanks for reading. May your pipelines be green, your infra be tagged, and your security posture be strong!
๐ Explore my other blogs at opsbygandal.dev
๐ Check out the GitHub repo [https://github.com/gandalops/portfolio-chirpy]
๐ Letโs connect on LinkedIn
This post is licensed under
CC BY 4.0
by the author.