Explore our new Cybersecurity Terms Glossary, explained by our experts.
Cybersecurity Terms Glossary
Terraform security covers the code, the state file that holds your secrets in plain text, the modules you import, and the pipeline credentials that can rebuild your entire estate.

What Is Terraform Security?
Terraform security is the practice of protecting an infrastructure-as-code workflow at every point where it can be subverted: the configuration itself, the state file that records what exists, the modules and providers pulled in from elsewhere, and the pipeline identity that holds enough privilege to build or destroy your estate. The configuration is usually the part teams scan. The other three are where the serious incidents come from.
Terraform records every managed resource in a state file, and it records attribute values as it received them. That includes database passwords, generated private keys, access keys, and certificate material — in plain text, regardless of whether the variable was marked sensitive, because sensitivity controls console output rather than state content.
A state file committed to a repository is therefore a credential dump. So is one left on a laptop, or stored in a bucket with broad read access. The minimum configuration is a remote backend with encryption at rest, versioning, state locking, and an access policy limited to the pipeline role and a small break-glass group. A plan file deserves the same treatment, since it contains the same values and is frequently uploaded as a build artefact where anyone with read access to the pipeline can download it.
Printing a plan into a pull request comment is a related and very common mistake. It is convenient, it is readable, and on a public repository it is a disclosure.
A module block pointing at a public registry or a Git reference executes somebody else’s configuration inside your credentials, which makes it a supply chain dependency like any other. Pin every module and provider to an exact version, and prefer a digest or commit hash over a tag, which can be moved. Mirror the modules you depend on into an internal registry so that an upstream deletion or takeover does not alter what you deploy, and review the diff when you upgrade rather than accepting a range constraint that updates silently.
Provisioners deserve a specific warning. A local-exec provisioner runs arbitrary commands on whatever machine executes Terraform, which in practice is your CI runner, holding deployment credentials. An imported module containing one is a straightforward path to code execution in your pipeline. Treat any module using provisioners as requiring manual review, and disallow them in your own code where there is an alternative.
The role Terraform assumes is typically among the most privileged in the organisation, because creating IAM roles, security groups, and storage requires administrative rights. Long-lived access keys stored as CI secrets are the wrong way to supply it: they are copyable, they rarely rotate, and they appear in forks and logs. All three major providers support OpenID Connect federation with GitHub Actions, GitLab, and similar platforms, which issues short-lived credentials bound to a specific repository, branch, and workflow. Configure the trust policy conditions tightly — a subject condition of repo:org/* grants your production role to every repository in the organisation.
Beyond the credential, apply separation. Plan runs need read-only access; only apply runs need write. Production applies should require a review, run from a protected branch, and operate under a permissions boundary that caps what the role can create even if the configuration asks for more.
Static analysis of Terraform catches a meaningful share of security misconfigurations before anything is deployed: unencrypted volumes, security groups open to 0.0.0.0/0, public storage, disabled logging, and wildcard policies. Checkov, Trivy’s configuration scanner, Terrascan, and KICS all do this well and run in seconds.
Policy as code goes further by evaluating the plan rather than the source, which matters because the plan reflects variables, conditionals, and module expansion. Open Policy Agent with Conftest, or HashiCorp Sentinel in Terraform Cloud, can block an apply that would create a resource violating your standard. Run both: source scanning for fast developer feedback, plan evaluation as the gate.
Infrastructure as code only helps if it reflects reality. Manual changes made during an incident diverge the environment from the configuration, and the next apply either reverts a fix or fails confusingly. Schedule regular plan runs purely to detect drift, alert on differences, and treat an unexplained change as a security event rather than an inconvenience — unexpected drift is exactly what an attacker modifying your environment looks like.
Two practical notes to finish. Terraform moved to the Business Source Licence in August 2023, prompting the OpenTofu fork; the security considerations here apply identically to both. And a module that creates resources correctly can still create them with weak settings, so reviewing the defaults in a module you adopt is part of reviewing the module.
Scanners confirm what the code says. If you want to know what the resulting environment actually permits, our cloud security services test the deployed estate rather than the templates that produced it.
Because state records the attributes of every managed resource as the provider returned them, and some of those attributes are secret values such as generated passwords or private keys. Marking a variable as sensitive only suppresses console output; the value is still written to state. The answer is to protect state as a credential store, and where possible to have resources generate secrets directly into a secrets manager.
It is a sensible pattern for production. Keeping the state backend in a dedicated account means that compromising a workload account does not immediately yield the state file and the credentials inside it. At minimum, use a bucket with versioning, encryption with a customer-managed key, access logging, and a policy that grants read access only to the pipeline role and a documented break-glass group.
tfsec has been folded into Trivy, which now carries its configuration scanning capability alongside vulnerability and secret scanning. Existing rules continue to work, but new projects should start with Trivy or Checkov. Whichever you choose, run it in the pipeline rather than relying on developers to run it locally, and fail builds on the findings you have agreed are blocking.
Pin modules and providers to exact versions or commit hashes, mirror them into an internal registry, and review the diff on every upgrade rather than accepting version ranges. Prohibit provisioners in imported modules, since they execute commands on your CI runner. Running plan stages with read-only credentials and requiring human approval for apply limits what an unreviewed change can achieve.
You May Be Interested In These
We use a few cookies that are needed to make this website work, and they are always on. We would also like to set analytics cookies, to see which pages are useful; functional cookies, so that our live chat works; and advertising cookies, to measure our campaigns. We will not set any of those until you say yes, and you can change your mind at any time from the Cookie Settings link in the footer.
Choose the cookies you are happy for us to use. Strictly necessary cookies are always on; everything else is off until you turn it on.
These cookies keep the website working: they hold your security checks, your form submissions and the choice you make here. The site cannot run without them, so they are always on.
These let our live chat open and remember your conversation, and let the newsletter sign-up work. Switch them off and the chat window will not appear.
These tell us which pages are read and how people find us, so that we can improve the site. The reports are about groups of visitors, never about you personally.
These let us measure our advertising and show you our adverts on other websites, such as LinkedIn, Google and Meta. Switch them off and you will still see adverts, but they will be less relevant.