IaC Security
Securing Infrastructure as Code - automated security scanning, policy as code, and GitOps principles for safe and auditable infrastructure.

What is IaC Security?
Infrastructure as Code (IaC) Security encompasses the processes and tools for securing your infrastructure defined in code. When you write infrastructure as code (e.g., Terraform, CloudFormation), you automatically create documentation of your infrastructure and can use tools to validate security configurations before they are ever deployed.
Why is IaC Important for Security?
Imagine your infrastructure was larger - you had development, staging, and production environments, multiple GitLab runners, database servers, and more. If everything were configured manually, problems would arise:
- Slow configuration: Manually creating resources is time-consuming and difficult to scale
- Inconsistency: Difficult to ensure the same configuration across environments
- Missing documentation: Without IaC you have no record of how infrastructure was created
- Misconfiguration risk: People make mistakes - they forget to close a port, remove an unnecessary user
- No audit trail: Without version control you don't know who changed what and why
- Difficult recovery: After an attack you don't know how to restore the original state
Benefits of Infrastructure as Code
- The code itself serves as documentation of your infrastructure
- You can use tools to validate security issues
- Peer reviews and security audits of the code
- Version control and audit trail of all changes
- Fast recovery after an incident - just apply the code
GitOps Principles for IaC
GitOps is an approach where a Git repository serves as the single source of truth for your infrastructure. All changes go through pull requests, code reviews, and automated tests.
GitOps Workflow for IaC
1. Code & Version Control
All infrastructure lives in a Git repository
- Terraform/CloudFormation/Pulumi code
- Branch protection rules
- Required reviews before merge
2. Automated Validation
CI pipeline automatically validates changes
- terraform validate - syntax and structure
- Trivy/Checkov - security scanning
- terraform plan - preview of changes
3. Review & Approval
Human review before deployment
- Code review from colleagues
- Security review for critical changes
- Approval gates in the pipeline
4. Automated Deployment
Centralized deployment through the pipeline
- terraform apply only from the pipeline
- Remote state management
- Audit log of all deployments
IaC Security Scanning Tools
Trivy (formerly tfsec)
Aqua Security moved tfsec into Trivy and directs its engineering work there. The trivy config command scans Terraform and other IaC formats.
- Terraform, CloudFormation, Kubernetes, Dockerfile
- Custom checks in Rego
- CI/CD integration
- JSON and SARIF output
Checkov
Policy-as-code tool for IaC scanning - Terraform, CloudFormation, Kubernetes, and more.
- Terraform, OpenTofu, CloudFormation, Kubernetes, Helm and more
- 1000+ built-in policies
- Custom policies in Python and YAML
- Graph-based analysis
Conftest (OPA)
Configuration tests written in Rego from Open Policy Agent. Terrascan, which also used OPA policies, was archived by Tenable in November 2025 and is no longer maintained.
- Rego policies
- Terraform (HCL2), YAML, JSON, Dockerfile
- Custom rules for internal standards
- Easy to run in CI
terraform validate
Built-in Terraform command for syntax and structure validation.
- Syntax validation
- Variable references
- Module validation
- Provider requirements
Licensing: in August 2023 HashiCorp moved new Terraform releases from MPL 2.0 to BSL 1.1. The community fork OpenTofu, a Linux Foundation project, stays under MPL 2.0 and shipped its first stable release in January 2024. The scanners above work on the same HCL code; Checkov lists OpenTofu support explicitly.
Practical Example: CI/CD Pipeline for Terraform
The jobs use rules instead of the deprecated only/except and override the entrypoint of the official images. Scanner images are pinned to a fixed version: in March 2026 attackers with stolen credentials published compromised Trivy releases and rewrote trivy-action tags (CVE-2026-33634). Checkov writes its results to the directory given by --output-file-path.
Example Trivy Output
Secure Terraform Code - Examples
Security Group - Right vs Wrong
S3 Bucket - Encryption and Access Control
IaC Security Best Practices - Summary
GitOps & Version Control
- All infrastructure in a Git repository
- Branch protection - no direct pushes to main
- Required code reviews before merge
- Signed commits for audit trail
- Semantic versioning for releases
Automated Security Scanning
- Trivy or Checkov in every PR
- Pin scanner images and GitHub Actions to a version or commit SHA
- Block merge on critical findings
- Generate reports for audit
- Regularly update rules
- Custom policies for your requirements
Secure Terraform Practices
- Remote state with encryption and locking (S3 backend with
use_lockfile; DynamoDB locking is deprecated) - Sensitive values in variables, not in code
- Use data sources instead of hardcoded values
- Modules for reusable infrastructure
- terraform plan review before apply
What NOT to Do
- Hardcoded secrets in Terraform code
- terraform apply from a local machine
- Ignoring security scan results
- Wildcard permissions (0.0.0.0/0 on all ports)
- Disabled encryption for storage
- Missing tags for audit and cost tracking
Sources
- Aqua Security (GitHub): tfsec, tfsec to Trivy Migration (August 2023)
- Trivy: Misconfiguration Scanning (November 2025)
- Aqua Security (GitHub Advisory): GHSA-69fq-xp46-6x23 / CVE-2026-33634 (March 2026)
- Bridgecrew / Prisma Cloud (GitHub): Checkov (October 2025)
- Checkov: CLI Command Reference (October 2026)
- Tenable (GitHub): Terrascan (archived) (November 2025)
- Open Policy Agent: Conftest (September 2026)
- HashiCorp: Terraform Backend Type s3 (August 2026)
- HashiCorp: HashiCorp adopts Business Source License (August 2023)
- OpenTofu: OpenTofu is going GA (January 2024)
- GitLab Docs: Deprecated keywords (only / except) (October 2026)
- GitLab Docs: Run your CI/CD jobs in Docker containers (October 2026)