Security skill · Find and fix
Terraform and OpenTofu security review.
Review Terraform and OpenTofu projects for security issues that are specific to the tool, namely state file and backend exposure, secrets in variables and outputs, provider and module supply chain, plan and apply permissions, drift, and policy-as-code gates, then fix and verify with plan output and test pipelines. Use when auditing a Terraform repository, its state backend and pipeline, or a pull request that changes modules, providers, or backends. For resource-level misconfigurations see iac-misconfig-review.
Facts
- Category
- Find and fix
- License
- MIT
- Tags
- Misconfiguration
- Version
- 1.0.0
- Last updated
- 2026-12-02
- Folder
terraform-iac-security-review/- MCP resource
skill://terraform-iac-security-review/SKILL.md- Files
- SKILL.md
- LICENSE
- references/terraform-checks.md
Original Saasment content. Saasment is in early access; this skill does not require joining it.
Install via MCP or as files
Connect your client to https://www.saasment.com/api/mcp (see client setup) and call get_skill:
{
"name": "get_skill",
"arguments": {
"slug": "terraform-iac-security-review"
}
}Or download the .zip and unpack it into your agent's skills directory:
Terminal, from your project root (user-level: ~/.cursor/skills)
mkdir -p .cursor/skills
curl -fsSL https://www.saasment.com/ai-security/skills/terraform-iac-security-review/download.zip -o /tmp/terraform-iac-security-review.zip
unzip -o /tmp/terraform-iac-security-review.zip -d .cursor/skills
# result: .cursor/skills/terraform-iac-security-review/SKILL.mdReview SKILL.md before enabling a skill. Skills are instructions your agent will follow.
SKILL.md
--- name: terraform-iac-security-review description: Review Terraform and OpenTofu projects for security issues that are specific to the tool, namely state file and backend exposure, secrets in variables and outputs, provider and module supply chain, plan and apply permissions, drift, and policy-as-code gates, then fix and verify with plan output and test pipelines. Use when auditing a Terraform repository, its state backend and pipeline, or a pull request that changes modules, providers, or backends. For resource-level misconfigurations see iac-misconfig-review. license: MIT metadata: author: Saasment version: "1.0.0" category: both tags: "misconfiguration" --- # Terraform and OpenTofu security review (find and fix) License: MIT. Copyright (c) 2026 Saasment. Source: https://www.saasment.com/ai-security/skills/terraform-iac-security-review This skill covers the part of infrastructure as code that belongs to the tool itself: where state lives and who can read it, how secrets move through variables and outputs, which providers and modules run in your pipeline, what identity runs `apply`, and how changes are gated. It is the Terraform and OpenTofu companion to `iac-misconfig-review`. That skill covers the resources you declare (public storage, open security groups, wide IAM, privileged containers) across all IaC languages. Use both for a full review: this skill for the tooling, that one for the resources. ## When to use - You audit a Terraform or OpenTofu repository, its remote backend, or its CI pipeline. - A pull request adds a provider, a module source, a backend change, or an `apply` workflow. - You want policy-as-code and plan review in place before more teams adopt the repository. ## Safe by default - Review only repositories, state, and cloud accounts you own or are authorized to assess. - Run `terraform init -backend=false`, `validate`, and `plan` for review. Do not run `apply`, `destroy`, `import`, `state rm`, or `state push` as part of a review, and never against production outside your change process. - State files can contain secrets in plain text. Do not copy state into tickets, prompts, or third-party tools. Read it only through the backend with an authorized identity, and redact values in notes. - Run `plan` with a read-only identity where the providers allow it, and save the plan to a protected location. - Do not upload plans or code containing secrets to external scanners without approval. ## Procedure 1. **Inventory.** Root modules and the environments they target, Terraform or OpenTofu version constraints, backend configuration, providers and versions, module sources, workspaces, and the pipeline that runs plan and apply. Commands are in `references/terraform-checks.md`. 2. **Review the backend and state.** Remote backend with encryption at rest, versioning, and locking; no state committed to Git (check history too); read access limited to the pipeline and a small group; separate state per environment so a development identity cannot read production state; backups protected. 3. **Review secrets handling.** Variables that hold secrets are marked `sensitive`, with values sourced from a secret manager or the pipeline's secret store, not `.tfvars` files in Git. Understand that `sensitive` hides values in output but they still land in state. Prefer data sources that fetch at apply time, ephemeral values where the tool version supports them, and provider-managed secret rotation. Check outputs, `local-exec` and `remote-exec` provisioners, and user-data scripts for embedded credentials (see `secrets-in-repo-scan-and-rotation`). 4. **Review provider and module supply chain.** Provider versions constrained and the dependency lock file (`.terraform.lock.hcl`) committed; module sources pinned to a version or commit, not a moving branch; registries and Git sources trusted; modules from outside your organization read before use; no `external` data sources or provisioners running unreviewed scripts. 5. **Review the apply identity.** The credentials that run `apply` are short-lived (workload identity or OIDC federation), scoped to the environment, and not administrator-level where a narrower role works; plan runs use a read-only role; no long-lived keys in pipeline variables; cross-account roles list a specific trusted pipeline identity (see `cloud-iam-storage-exposure-review`). 6. **Review change control.** Plans are generated on pull requests and read by a reviewer; `apply` runs only from the protected branch or an approved run, from the saved plan; branch protection and code owners cover the Terraform directories; destructive changes to stateful resources need a named approver; `prevent_destroy` is set on critical resources. 7. **Review policy-as-code.** A scanner (for example Checkov, Trivy, or tfsec-compatible rules) and, where used, a policy engine (Open Policy Agent with Conftest, or Sentinel) run on the plan JSON in CI and fail the build on high-severity findings, with documented, expiring exceptions. 8. **Review drift and imports.** A scheduled plan detects manual changes; imported resources are reviewed for settings the code never declared; resources created outside Terraform (click-ops) are inventoried. 9. **Fix.** Move state to a protected remote backend, purge secrets from Git history after rotation, mark and re-source secrets, pin providers and modules, switch to federated credentials, add plan review and policy gates, and add drift detection. 10. **Hand off resource findings.** For misconfigured resources found while reading plans, continue with `iac-misconfig-review`. ## Verification - Confirm state protection by checking the backend settings (encryption, versioning, lock table or native locking) and by attempting to read the state with an identity that should be denied, in a test environment; expect an access error. - Confirm no state or secrets are in Git: search the working tree and history for `terraform.tfstate`, `.tfvars` files with secret-looking keys, and run secret scanning. - Confirm pinning: run `terraform init` in a clean checkout and assert it succeeds from the lock file without upgrading; confirm module sources show a version or commit. - Confirm plan review: open a test pull request that adds a deliberately insecure resource in a scratch environment and confirm the pipeline fails and shows the policy message. - Confirm apply scoping: check from pipeline logs and the cloud's audit trail that `apply` used the intended identity, and that the plan-only identity cannot create resources. - Confirm drift detection by making a harmless tag change by hand in a test account and checking the scheduled plan reports it. ## Reporting Table: finding id, location (backend, module, workflow, variable), issue, risk, fix, verification result. Add pinned versions, backend settings, and the identity used for plan and apply. ## References - Terraform: managing sensitive data in state: https://developer.hashicorp.com/terraform/language/state/sensitive-data - Terraform dependency lock file: https://developer.hashicorp.com/terraform/language/files/dependency-lock - OWASP Infrastructure as Code Security Cheat Sheet: https://cheatsheetseries.owasp.org/cheatsheets/Infrastructure_as_Code_Security_Cheat_Sheet.html - Open Policy Agent: https://www.openpolicyagent.org/docs/latest/ - Companion skills: `iac-misconfig-review`, `cloud-iam-storage-exposure-review`, `secrets-in-repo-scan-and-rotation`, `cicd-github-actions-security-review`.
Helper files
LICENSE
MIT License Copyright (c) 2026 Saasment Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
references/terraform-checks.md
# Terraform and OpenTofu checks
License: MIT. Copyright (c) 2026 Saasment.
Use `tofu` in place of `terraform` for OpenTofu. Tool flags change between versions; check `--help`. The commands below do not apply or destroy anything.
## Inventory and static checks
```bash
# Versions, providers, and module sources
rg -n 'required_version|required_providers|source\s*=|version\s*=' --glob '*.tf'
rg -n 'module "' -A4 --glob '*.tf'
# Backend configuration
rg -n 'backend "' -A8 --glob '*.tf'
# Lock file committed?
git ls-files | rg '\.terraform\.lock\.hcl$'
# State or variable files in the repository and its history
git ls-files | rg '\.tfstate|\.tfvars'
git log --all --name-only --pretty=format: | rg '\.tfstate|\.tfvars' | sort -u
# Provisioners and external scripts
rg -n 'local-exec|remote-exec|data "external"|user_data' --glob '*.tf'
# Variables and outputs that may carry secrets
rg -n -i 'password|secret|token|api_key|private_key' --glob '*.tf' --glob '*.tfvars'
# Validate syntax without touching a backend
terraform init -backend=false
terraform validate
terraform fmt -check -recursive
```
## Plan review (read-only identity)
```bash
terraform plan -out=/tmp/tfplan
terraform show -json /tmp/tfplan > /tmp/tfplan.json
# Resources the plan would replace or delete (stateful ones need an approver)
jq -r '.resource_changes[] | select(.change.actions | index("delete")) | "\(.change.actions|join(",")) \(.address)"' /tmp/tfplan.json
# Policy scan on the plan
checkov -f /tmp/tfplan.json --framework terraform_plan
conftest test /tmp/tfplan.json --policy ./policy
```
## Secure patterns
```hcl
# Pin providers and Terraform itself
terraform {
required_version = "~> 1.10"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
# Remote backend with encryption and locking (S3 example; use the equivalent for your cloud)
backend "s3" {
bucket = "example-terraform-state" # placeholder
key = "prod/network.tfstate"
region = "us-east-1"
encrypt = true
use_lockfile = true
}
}
# Pin modules to a version or commit, not a moving branch
module "network" {
source = "git::https://example.test/infra/network.git?ref=3f2a9c1" # placeholder commit
}
# Sensitive inputs: marked, and supplied by the pipeline's secret store at run time
variable "db_password" {
type = string
sensitive = true
}
# Protect stateful resources from accidental replacement
resource "aws_db_instance" "main" {
# ...
lifecycle {
prevent_destroy = true
}
}
```
## Pipeline identity pattern
| Stage | Identity | Permissions |
| --- | --- | --- |
| Pull request plan | Federated, read-only role per environment | Read resources, read state |
| Apply from protected branch | Federated, per-environment role | Create and change only what the stack manages |
| State admin | Separate, small group | Backend maintenance and recovery |
No long-lived access keys in repository or pipeline variables.
## Review checklist
| Area | Question |
| --- | --- |
| State | Encrypted, versioned, locked, private, one per environment? |
| Secrets | Sensitive inputs from a secret store? Any in `.tfvars` or state in Git? |
| Supply chain | Providers and modules pinned, lock file committed, sources trusted? |
| Identity | Short-lived, scoped credentials for plan and apply? |
| Change control | Plan reviewed, apply from saved plan on protected branch, destructive changes approved? |
| Policy | Scanner and policy engine on plan JSON, exceptions with owner and expiry? |
| Drift | Scheduled plan, unmanaged resources inventoried? |
Related
Companion skills
Questions
How do I install the Terraform and OpenTofu security review skill?
Download https://www.saasment.com/ai-security/skills/terraform-iac-security-review/download.zip and unzip it into your agent's skills directory (for example .cursor/skills, .claude/skills, .agents/skills, or .github/skills) so that terraform-iac-security-review/SKILL.md exists. Or connect to https://www.saasment.com/api/mcp and call the get_skill tool with the slug terraform-iac-security-review.
What license is the Terraform and OpenTofu security review skill under?
It is original Saasment content under the MIT license, stated in the SKILL.md frontmatter and in the LICENSE file in the skill folder.
Is it safe to run this skill?
A skill is a set of instructions your agent follows, so read SKILL.md first. It tells the agent to test only systems you own or are authorized to test, to use test environments and synthetic data, and to avoid destructive probes. Downloading it does not run any code on your machine.