Skip to content
DG David Galiata
cybersecurity terraform iac devops

A Guide to Terraform Security

A simple, practical guide to security considerations for Terraform

D

David Galiata

1 min read
A Guide to Terraform Security

Terraform is a powerhouse for Infrastructure as Code (IaC), but with great power comes the responsibility of not accidentally leaving your “digital front door” wide open. Security in Terraform isn’t just a single checkbox, it’s a multi-layered approach that spans from your local machine to your CI/CD pipeline.

If you’re looking to harden your infrastructure, here are the essential pillars of Terraform security you need to know.


Protect Your State File

The .tfstate file is the brain of your deployment. It maps your code to real-world resources and often contains sensitive data in plaintext, such as resource IDs, generated passwords, or private keys.

  • Remote Storage: Never store state files locally and never commit them to version control (Git). Use remote backends like AWS S3, GCS, or Terraform Cloud.
  • Encryption & Locking: Ensure your backend has encryption at rest enabled and uses state locking (e.g., via DynamoDB for S3) to prevent concurrent modifications that could lead to corruption.

Master Secrets Management

Hardcoding a password in a .tf file or a terraform.tfvars file is the fastest way to suffer a credential leak.

  • Use Secrets Managers: Integrate with tools like HashiCorp Vault, AWS Secrets Manager, or Azure Key Vault.
  • The sensitive Flag: Mark variables as sensitive = true. This tells Terraform to redact the value from CLI output, ensuring your passwords don’t end up in plain sight in your CI/CD logs.
  • Environment Variables: Use TF_VAR_ environment variables for passing secrets into your configuration at runtime.

Identity and Least Privilege

Whether a developer is running a command or a GitHub Actions runner is deploying code, follow the Principle of Least Privilege.

  • Avoid AdministratorAccess: Don’t give Terraform “Full Admin” rights. Scope IAM policies to only the specific resources and actions your configuration requires.
  • Short-Lived Credentials: Favor IAM Roles, OIDC federation, or Workload Identity over long-lived, static access keys that can be stolen or leaked.

Policy as Code (The Safety Net)

Treat your infrastructure code like application code. You wouldn’t merge a feature without a test, so don’t deploy infrastructure without a security scan.

  • Automated Scanning: Use tools like Checkov, tfsec, Trivy, or Open Policy Agent (OPA) to scan for “low-hanging fruit” vulnerabilities, such as open security groups (0.0.0.0/0) or unencrypted storage buckets.
  • Code Review: Use Pull Requests (PRs) for every change. A peer review is one of the most effective ways to catch logic errors or architectural security risks.

The “Plan and Apply” Workflow

In a secure environment, terraform apply should never be a mystery.

  • Manual Approval Gates: In your CI/CD pipeline, separate the plan and apply stages. This allows a human to review exactly what Terraform intends to do before any changes are made to production.
  • Drift Detection: Infrastructure “drifts” when someone manually changes a setting in the cloud console. Regular runs or automated drift detection help identify unauthorized modifications that might weaken your security posture.

Module Integrity and Hardening

The Terraform Registry is a great resource, but be cautious about what you invite into your environment.

  • Pin Versions: Always pin modules to a specific version (e.g., version = "2.5.0"). This prevents an upstream update from introducing breaking changes or malicious code.
  • Audit Third-Party Code: If you use a community module, audit the source code to ensure it isn’t creating unnecessary public endpoints or exfiltrating data.

Final Thoughts

Terraform security is about reducing your attack surface and creating a repeatable, audited process for infrastructure changes. By securing your state file, managing secrets properly, and enforcing policy-as-code, you can move fast without breaking things.

Back to Blog
Share:

Follow along

Stay in the loop — new articles, thoughts, and updates.