Blog
HADESS
Cyber Security Magic

Terraform Security: State Protection, Policy Scanning, and Drift Detection

Terraform Security: State Protection, Policy Scanning, and Drift Detection

Part of the Cybersecurity Skills Guide — This article is one deep-dive in our complete guide series.

By HADESS Team | February 28, 2026 | Updated: February 28, 2026 | 5 min read

Terraform manages your infrastructure. If your Terraform state file leaks, an attacker knows every resource, every IP, every connection string in your environment. If your Terraform code is insecure, you automate the deployment of vulnerable infrastructure at scale.

State File Protection

The Terraform state file contains the full truth about your infrastructure, including sensitive outputs like database passwords, API keys, and private IPs. Protecting it is non-negotiable.

Never store state locally for shared infrastructure. Use a remote backend with encryption and access controls:

hcl terraform { backend "s3" { bucket = "myorg-terraform-state" key = "production/terraform.tfstate" region = "us-east-1" encrypt = true dynamodb_table = "terraform-lock" kms_key_id = "alias/terraform-state" } } `

Key requirements:

  • Enable encryption at rest with a customer-managed KMS key
  • Enable versioning on the state bucket — you need to recover from bad applies
  • Use state locking (DynamoDB for S3, built-in for Terraform Cloud) to prevent concurrent modifications
  • Restrict access with IAM policies. Only your CI/CD pipeline and designated operators should read or write state
  • Never commit state files to Git. Add .tfstate and .tfstate.backup to .gitignore

Checkov and Policy Scanning

Writing secure Terraform requires checking configurations against security benchmarks. Checkov scans Terraform code (HCL and plan files) for misconfigurations before anything gets deployed.

`bash
checkov -d . --framework terraform
`

Common findings:

  • S3 buckets without encryption or public access blocks
  • Security groups with 0.0.0.0/0 ingress rules
  • IAM policies with wildcard permissions
  • RDS instances without encryption or backup

Integrate Checkov into your CI pipeline. Fail the build on high-severity findings. Use custom policies for org-specific rules:

`python
from checkov.terraform.checks.resource.base_resource_check import BaseResourceCheck
from checkov.common.models.enums import CheckResult

class EnsureTagging(BaseResourceCheck): def __init__(self): super().__init__(name="Ensure resources have required tags", id="CUSTOM_001", categories=[], supported_resources=["aws_instance", "aws_s3_bucket"])

def scan_resource_conf(self, conf): tags = conf.get("tags", [{}])[0] return CheckResult.PASSED if "owner" in tags else CheckResult.FAILED `

Other tools worth considering: tfsec, Terrascan, and Snyk IaC. Use what integrates best with your pipeline.

Module Pinning

Terraform modules from the public registry or Git repositories should be version-pinned:

`hcl
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.1.2" # Pin exact version
}
`

Never use version = “>= 5.0.0” for production modules. A new major version could change behavior or introduce insecure defaults. For Git-sourced modules, pin to a commit hash or tag — not a branch.

Review module source code before adoption. Public modules are community-maintained and may contain insecure defaults or unnecessary permissions.

Drift Detection

Infrastructure drift happens when someone modifies resources outside of Terraform — through the cloud console, CLI, or another tool. Drift means your state file no longer matches reality, and your security controls may no longer be applied.

Detect drift with terraform plan runs on a schedule:

`bash
terraform plan -detailed-exitcode

Exit code 2 means drift detected

Automate this in CI. Alert when drift is detected. Common drift patterns that indicate security issues:

  • Security group rules added manually
  • IAM policies modified outside Terraform
  • Encryption settings changed on storage resources
  • Public access enabled on resources that should be private

Terraform Cloud and Spacelift offer built-in drift detection with notifications.

Related Career Paths

Terraform security skills are required for DevSecOps and Cloud Security Engineer roles. Infrastructure-as-code security is a core competency in both paths. Check the skills page to map your current abilities.

Next Steps

Related Guides in This Series

Take the Next Step

Browse 80+ skills on HADESS. Go to the browse 80+ skills on hadess on HADESS.

See your certification roadmap. Check out the see your certification roadmap.

Get started freeCreate your HADESS account and access all career tools.

Frequently Asked Questions

How long does it take to learn this skill?

Most practitioners build working proficiency in 4-8 weeks of dedicated study with hands-on practice. Mastery takes longer and comes primarily through on-the-job experience.

Do I need certifications for this skill?

Certifications validate your knowledge to employers but are not strictly required. Hands-on experience and portfolio projects often carry more weight in technical interviews. Check the certification roadmap for relevant options.

What career paths use this skill?

Explore the career path explorer to see which roles require this skill and how it fits into different cybersecurity specializations.

HADESS Team consists of cybersecurity practitioners, hiring managers, and career strategists who have collectively spent 50+ years in the field.

Leave a Reply

Your email address will not be published. Required fields are marked *